建新用戶
adduser hadoop
用戶創建后切換用戶su hadoop
生成密鑰
切換后執行ssh-keygen -t rsa生成密鑰
[hadoop@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
79:0a:3d:04:3d:83:d5:24:a8:94:62:89:f3:b7:90:f8 hadoop@172.31.10.94
The key's randomart image is:
+--[ RSA 2048]----+
| . . ..=oo. |
|o + o o.+.. |
| = + . .o |
|. + o o . |
| . o .. S . |
| E . . + |
| . |
| |
| |
+-----------------+
然后把id_rsa.pub的改名成authorized_keys. id_rsa.pub里的內容最后會有 hadoop@localhost,轉成authorized_keys的時候如果不能登錄,就把最后這節刪掉.
當有多個公鑰時,在authorized_keys里追加寫就行了.另外要保證authorized_keys的權限是600,.ssh的權限是700
chmod 600 authorized_keys
chmod 700 .ssh
也可以用xshell生成密鑰,然后將公鑰上傳到服務器,寫入authorized_keys里.
ssh命令登錄遠程機
ssh -i ~/.ssh/<private_key_file> <username>@ip
