ssh接続を鍵認証で行う際の設定
毎回ググっているので、メモしておく。
鍵作成
ssh-keygen -f ~/.ssh/example.com_rsa -t rsa Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in example.com_rsa. Your public key has been saved in example.com_rsa.pub. The key fingerprint is: 7f:83:fd:2a:20:69:a0:01:ed:76:62:ac:a5:11:7c:9d xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx The key's randomart image is: +--[ RSA 2048]----+ |.. . . | |o... E | | =. | |. O o | | B = . .S | |o . + .. o | | . . .o + | | .. o | | .... | +-----------------+
各オプションの説明は以下の通り。
-f filename Specifies the filename of the key file. -t type Specifies the type of key to create. The possible values are ``rsa1'' for protocol version 1 and ``rsa'' or ``dsa'' for protocol version 2.
リモートサーバに鍵をコピー
※ ~ には リモートサーバのディレクトリパスが入る
scp ~/.ssh/exmaple.com_rsa.pub your@server:~ your@server 's password:
ssh ログイン
ssh your.server
~/.ssh が ない場合はディレクトリ作成
mkdir ~/.ssh chmod 700 ~/.ssh
鍵設置
mv ~/example.com_rsa.pub ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
接続確認
パスワード入力を求められずログインでればOK。
できない場合は、PermissionやOwner を確認。
ssh -i ~/.ssh/example.com_rsa your@server # 22番port 以外を使用している場合は -p でポート番号を指定
パスワードログインを禁止
鍵認証のみにしたほうが安全なので、
設定が完了したらパスワードログインを禁止する。
ついでに、root ユーザでのログインも禁止。
sudo vi /etc/ssh/sshd_config -PasswordAuthentication yes +#PasswordAuthentication yes +PasswordAuthentication no +PermitRootLogin no
設定反映
sudo sshd -t # 何も表示されなければOK sudo /etc/rc.d/init.d/sshd restart