linuxはsshリモートホストのパスワードなしログインを実現

2882 ワード

(一)問題:
サーバMasterとサーバSlaveの2つのマシンがある場合は、サーバMasterがパスワードを入力せずにサーバSlaveにアクセスできるようにします.
(二)方法と原理:
ssh-keygenを使用してサーバMaster上でprivateとpublic鍵を生成し、生成したpublic鍵をリモートマシンサーバSlaveにコピーすると、sshコマンドを使用してパスワードなしで別のマシンサーバSlaveにログインできます.
linuxシステムでは、sshはリモートログインのデフォルトツールです.このツールのプロトコルはRSA/DSAの暗号化アルゴリズム【デフォルトはDSAアルゴリズム】を使用しているため、linuxシステムのリモート管理は非常に安全です.
(三)実験手順:
1.ServerMasterへのログイン
2.ssh-keygen -t dsaコマンドを使用すると、公開鍵と秘密鍵ファイルid_が生成されます.dsaとid_dsa.pub【ずっと車に戻ったら、最後の2つの書類は/home/$USER/.sshの下にあるはずです】
3..pubファイルをServer Slaveマシンにコピーする.sshディレクトリの下にauthorized_keysとして保存
以下を使用できます.ssh-cop-idコマンドは、指定する公開鍵ファイルをリモートコンピュータにコピーします.eg.
[iotspark@iotsparkmaster ~]$ cd .ssh
[iotspark@iotsparkmaster .ssh]$ ll
total 16
-rw-r--r--. 1 iotspark iotspark  601 Mar 23 04:37 authorized_keys
-rw-------. 1 iotspark iotspark  668 Mar 23 04:37 id_dsa
-rw-------. 1 iotspark iotspark  601 Mar 23 04:37 id_dsa.pub
-rw-r--r--. 1 iotspark iotspark 1179 Apr  3 04:56 known_hosts
[iotspark@iotsparkmaster .ssh]$ cd ..
[iotspark@iotsparkmaster ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: ERROR: No identities found
[iotspark@iotsparkmaster ~]$ ssh-copy-id -i ~/.ssh/id_dsa.pub [email protected]     
The authenticity of host '172.16.22.52 (172.16.22.52)' can't be established.
RSA key fingerprint is 81:48:42:50:3b:63:f9:96:89:f3:8c:b3:82:9c:60:3b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.22.52' (RSA) to the list of known hosts.
[email protected]'s password: 
Now try logging into the machine, with "ssh '[email protected]'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[iotspark@iotsparkmaster ~]$ ssh [email protected]
Last login: Tue Apr  3 00:36:41 2018 from 10.45.20.14
iotspark@iotsparknode2[/usr/iotspark]$

4.サーバーマスターマシンからサーバーSlaveマシンのターゲットアカウントにログインし、パスワードは不要になりました.
5.ファイルとディレクトリ権限の設定【このステップは省略できますが、セキュリティのためにも必要です~】authorized_keys権限の設定
chmod 644 authorized_keys

設定sshディレクトリ権限
chmod 700 -R .ssh
6.保証する.sshとauthorized_keysはいずれもユーザー自身だけが書く権限を持っている.それ以外の場合、検証は無効です.(今日はこの問題に遭遇して、長い間問題を探していました)、実はよく考えてみると、システムの脆弱性が現れないようにしています.
エラー:
The authenticity of host '172.16.22.52 (172.16.22.52)' can't be established.  
  • 相互のパスワードフリーログインを実現するには、対応するホストで手順2,3を継続するだけでよい.

  • 注意:
    公開鍵を追加してもアクセスできない場合は、Server Masterホスト上のファイルknown_hostsに既にサーバSlaveのip情報が存在しているので、削除して再アクセスすればよい.
    また、上記の方法を試してもだめであれば、ログイン失敗回数が多すぎるためロックされている可能性があります.このとき、私たちのシステムログを見に行く必要があります/var/log/messages、前のロック情報ログを削除すればいいです.
    次に、権限セクションおよびDSAファイルの生成については、別のブログを参照してください.linuxはssh localhostのパスワードなしログインを実現します.