この文書の現在のバージョンと選択したバージョンの差分を表示します。
— |
ja:ssh [2018/04/28 ] (現在) N_Miya 作成 |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
+ | ==== SSH ====== | ||
+ | ===== rootログインの禁止 ===== | ||
+ | CentOSなどはデフォルトで許可されているので注意 | ||
+ | # cat /etc/ssh/sshd_config | grep Permit | ||
+ | PermitRootLogin no | ||
+ | |||
+ | ===== パスワードログインを禁止して公開鍵認証 ===== | ||
+ | |||
+ | ==== 認証用のKeyの作成 ==== | ||
+ | 認証用の秘密鍵と公開鍵のペアが作成される\\ | ||
+ | **安全性からed25519を推奨します** 2017.05.22 | ||
+ | |||
+ | $ cd | ||
+ | $ ssh-keygen -t ed25519 | ||
+ | OR | ||
+ | $ ssh-keygen -t ecdsa -b 384 | ||
+ | OR | ||
+ | $ ssh-keygen -t rsa -b 4096 | ||
+ | |||
+ | 利用可能な鍵証明書一覧 | ||
+ | $ ssh -Q key-cert | ||
+ | |||
+ | 利用可能な鍵一覧 | ||
+ | $ ssh -Q key | ||
+ | |||
+ | === 暗号強度の確認 === | ||
+ | |||
+ | $ ssh-keygen -l -f ~/.ssh/id_ed25519.pub | ||
+ | 256 SHA256:71.......................................nQ user@host (ED25519) | ||
+ | $ ssh-keygen -l -f ~/.ssh/id_ecdsa.pub | ||
+ | 384 SHA256:6z7QX....................................Ew user@host (ECDSA) | ||
+ | $ ssh-keygen -l -f ~/.ssh/id_rsa.pub | ||
+ | 4096 SHA256:/p5W9....................................IQ user@host (RSA) | ||
+ | |||
+ | |||
+ | ==== 公開鍵をリモートホストのauthorized_keysに追加 ==== | ||
+ | |||
+ | $ cat ./.ssh/id_rsa.pub | ssh user@remote_host "cat >> ./.ssh/authorized_keys" | ||
+ | .sshディレクトリがないと失敗するのでその場合には | ||
+ | $ ssh user@remote_host "mkdir ./.ssh && chmod 700 ./.ssh" | ||
+ | とかやってディレクトリを作っておく。 | ||
+ | |||
+ | CentOSならssh-copy-idが入っているのでそちらを使うも良し。\\ | ||
+ | FreeBSDにも入りました。((2017.05.22:10.3で確認))\\ | ||
+ | <del>FreeBSDはPortsのsecurity/ssh-copy-idでインストールしても良いのですがpkgngにはありません</del>。((2014/12/05現在)) | ||
+ | $ ssh-copy-id -i .ssh/id_rsa.pub user@remote_host | ||
+ | |||
+ | なお、authorized_keysはPermissionを **600**にしておかないとLogin に失敗するので注意 | ||
+ | ==== パスワードログインを禁止 ==== | ||
+ | # cat /etc/ssh/sshd_config | ||
+ | ...... | ||
+ | PasswordAuthentication no | ||
+ | ChallengeResponseAuthentication no | ||
+ | UsePAM no | ||
+ | |||
+ | FreeBSDは標準でPasswordAuthentication を禁止しているがChallengeResponseAuthentication は許可されているのでこちらも必ず「No」にしておく。 | ||
+ | ==== 公開鍵認証をON ==== | ||
+ | |||
+ | PubkeyAuthentication yes | ||
+ | <del>AuthorizedKeysFile .ssh/authorized_keys</del> | ||
+ | |||
+ | AuthorizedKeysFileを指定すると、このマシンから他へssh接続する際に//**Bad configuration option: AuthorizedKeysFile**//と表示して落ちます。((昔は生きていたはずで、その為このオプションを指定するよう説明しているサイトや書籍多数です…)) | ||
+ | |||
+ | |||
+ | ==== user's configuration file ==== | ||
+ | .ssh/configファイルで複数サーバーのSSH接続を管理 | ||
+ | <code> | ||
+ | Host example | ||
+ | HostName example.com | ||
+ | Port 10022 | ||
+ | User nanako | ||
+ | IdentityFile ~/.ssh/id_rsa.example | ||
+ | </code> | ||
+ | これで以下のようにアクセス可能 | ||
+ | $ ssh example | ||
+ | |||
+ | ===== 秘密鍵から公開鍵を生成する ===== | ||
+ | |||
+ | |||
+ | ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub | ||
+ | |||
+ | ===== 公開鍵からfinger printを生成する ===== | ||
+ | |||
+ | |||
+ | ssh-keygen -lf ~/.ssh/id_rsa.pub | ||
+ | |||
+ | ===== knownhostの更新 ===== | ||
+ | |||
+ | host="target-hostname" | ||
+ | ssh-keygen -R $host | ||
+ | ssh-keyscan -H $host >> ~/.ssh/known_hosts |