本来は最小化手順に含めてもいいのだけれど、複製で構築する場合に、全ての仮想OSで同じユーザ、鍵があるのはまずいという事で分割している。
バックアップや複製用として残す場合は、この手順の一つ前で止める事をお勧めする。
いつまでもrootユーザーでSSHログインしているわけにも行かないので、メンテナンスログインユーザーなどを追加する。
[root@labsixwish ~]# useradd rokubou -c "Fuu.Rokubou" -G wheel [root@labsixwish ~]#
主にメンテナンスを行うユーザーであるので、wheelグループにも追加しておく。
SSHでログインするための鍵を作成する。
[root@labsixwish ~]# su - rokubou [rokubou@labsixwish ~]$ ssh-keygen -t rsa -b 1024 Generating public/private rsa key pair. Enter file in which to save the key (/home/rokubou/.ssh/id_rsa):(何も入力しない) Created directory '/home/rokubou/.ssh'. Enter passphrase (empty for no passphrase):(パスワード) Enter same passphrase again:(上で入力したのと同じパスワード) Your identification has been saved in /home/rokubou/.ssh/id_rsa. Your public key has been saved in /home/rokubou/.ssh/id_rsa.pub. The key fingerprint is: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx rokubou@labsixwish [rokubou@labsixwish ~]$ cd .ssh [rokubou@labsixwish .ssh]$ ls id_rsa id_rsa.pub [rokubou@labsixwish .ssh]$ mv id_rsa.pub authorized_keys [rokubou@labsixwish .ssh]$ mv id_rsa rokubou.labsixwish.rsa.key [rokubou@labsixwish .ssh]$ ls authorized_keys rokubou.labsixwish.rsa.key [rokubou@labsixwish .ssh]$ cat rokubou.labsixwish.rsa.key -----BEGIN RSA PRIVATE KEY----- (省略) -----END RSA PRIVATE KEY----- [rokubou@labsixwish .ssh]$
表示された公開鍵の内容をファイルに保存する。
手順は省略するが、puttyで使えるように変換する。
新しくputtyを開いて、鍵を使ってログインできるかを試みる。
Using username "rokubou". Authenticating with public key "imported-openssh-key" Passphrase for key "imported-openssh-key":(パスワード) [rokubou@labsixwish ~]$ [rokubou@labsixwish ~]$ logout
ログインできたら、そのまま閉じてしまう。
rootログインの制限と、ログイン方式の制限をかけます。
[rokubou@labsixwish .ssh]$ logout [root@labsixwish ~]$ cp /etc/ssh/sshd_config /etc/ssh/sshd_config.org [root@labsixwish ~]$ vi /etc/ssh/sshd_config
*** /etc/ssh/sshd_config.org YYYY-mm-dd --:--:--.000000000 +0900 --- /etc/ssh/sshd_config YYYY-mm-dd --:--:--.000000000 +0900 *************** *** 35,47 **** # Authentication: ! #LoginGraceTime 2m ! #PermitRootLogin yes #StrictModes yes ! #MaxAuthTries 6 ! #RSAAuthentication yes ! #PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts --- 35,47 ---- # Authentication: ! LoginGraceTime 1m ! PermitRootLogin no #StrictModes yes ! MaxAuthTries 3 ! RSAAuthentication yes ! PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts *************** *** 57,63 **** # To disable tunneled clear text passwords, change to no here! #PasswordAuthentication yes #PermitEmptyPasswords no ! PasswordAuthentication yes # Change to no to disable s/key passwords #ChallengeResponseAuthentication yes --- 57,63 ---- # To disable tunneled clear text passwords, change to no here! #PasswordAuthentication yes #PermitEmptyPasswords no ! PasswordAuthentication no # Change to no to disable s/key passwords #ChallengeResponseAuthentication yes *************** *** 92,99 **** AcceptEnv LC_IDENTIFICATION LC_ALL #AllowTcpForwarding yes #GatewayPorts no ! #X11Forwarding no ! X11Forwarding yes #X11DisplayOffset 10 #X11UseLocalhost yes #PrintMotd yes --- 92,99 ---- AcceptEnv LC_IDENTIFICATION LC_ALL #AllowTcpForwarding yes #GatewayPorts no ! X11Forwarding no ! #X11Forwarding yes #X11DisplayOffset 10 #X11UseLocalhost yes #PrintMotd yes
[root@labsixwish ~]# /etc/rc.d/init.d/sshd restart sshdを停止中: [ OK ] sshdを起動中: [ OK ] [root@labsixwish ~]#
サービス再起動後にrootでログインできない事と、鍵方式でログインできる事を確認する。
上記で、wheelグループを使うことにしているのでいるので、pamの設定を変更してrootになる事のできるユーザを制限します。
[root@labsixwish ~]# cp /etc/pam.d/su /etc/pam.d/~su [root@labsixwish ~]# vi /etc/pam.d/su
*** /etc/pam.d/~su YYYY-mm-dd --:--:--.000000000 +0900 --- /etc/pam.d/su YYYY-mm-dd --:--:--.000000000 +0900 *************** *** 3,9 **** # Uncomment the following line to implicitly trust users in the "wheel" group. #auth sufficient pam_wheel.so trust use_uid # Uncomment the following line to require a user to be in the "wheel" group. ! #auth required pam_wheel.so use_uid auth include system-auth account sufficient pam_succeed_if.so uid = 0 use_uid quiet account include system-auth --- 3,9 ---- # Uncomment the following line to implicitly trust users in the "wheel" group. #auth sufficient pam_wheel.so trust use_uid # Uncomment the following line to require a user to be in the "wheel" group. ! auth required pam_wheel.so use_uid auth include system-auth account sufficient pam_succeed_if.so uid = 0 use_uid quiet account include system-auth
[root@labsixwish ~]#
とりあえず、ここまでで最小構成化が完了となります。