Generate public key
ssh-keygen -t rsa -P'' (Note: There are two single quotes at the end, which means no password is set)
Then distribute the public key to the target machine
ssh-copy-id -i ~/.ssh/id_rsa.pub username@the other party's machine IP
If it fails, there may be the following reasons:
1. Permission issues
The .ssh directory and /home/ current user need 700 permissions, please refer to the following operations to adjust
sudo chmod 700 ~/.ssh
sudo chmod 700 /home/current user
The authorized_keys file in the .ssh directory needs 600 or 644 permissions, please refer to the following operations to adjust
sudo chmod 600 ~/.ssh/authorized_keys
2. StrictModes problem
edit
sudo vi /etc/ssh/sshd_config
turn up
#StrictModes yes
Change to
StrictModes no
3. There are multiple authentication key files in the system
多个认证密钥文件,我们可以使用参数 -i 来指定需要传输至目标机的公钥文件,如:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
Two authentication key files in the 192.168.246.170 system, the paths are as follows:
/home/jianzhihua/.ssh/id_rsa.pub
/data/jianzhihua/.ssh/id_rsa.pub
Execute at 192.168.246.170:
$ ssh-copy-id -i /data/jianzhihua/.ssh/id_rsa.pub [email protected]
Log in to 192.168.246.171 from 192.168.246.170, the terminal executes:
$ ssh [email protected]
Enter passphrase for key '/home/jianzhihua/.ssh/id_rsa.pub': ##系统提示输入私钥,可不论输与不输都不能直接登录
Approach:
在192.168.246.170终端执行:
$ eval $(ssh-agent)
$ ssh-add
Identity added: /data/jianzhihua/.ssh/id_rsa (/data/jianzhihua/.ssh/id_rsa)
因为:
$ grep jianzhihua /etc/passwd
jianzhihua:x:1039:1039::/data/jianzhihua:/bin/bash
当然你也可以使用/home/jianzhihua/.ssh/id_rsa.pub,在192.168.246.170终端操作如下:
$ ssh-add -k /home/jianzhihua/.ssh/id_rsa
Identity added: /home/jianzhihua/.ssh/id_rsa (/home/jianzhihua/.ssh/id_rsa)
登录时:
$ ssh -i /home/jianzhihua/.ssh/id_rsa [email protected]