建新用户

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