确认本机sshd的配置文件(需要root权限)
$ vi /etc/ssh/sshd_config
找到以下内容,并去掉注释符”#“
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
如果修改了配置文件需要重启sshd服务(需要root权限)
$ vi /sbin/service sshd restart
第一步,生成密钥对
在客户端:192.168.22.26
localhost ~ # ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):(直接回车无须输入密钥)
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
04:37:13:2a:4b:10:af:c1:2b:03:3f:6b:27:ce:b9:62 root@codfei
localhost ~ # cd .ssh
localhost .ssh # ls -l
-rw------- 1 root root 1675 Sep 13 10:46 id_rsa
-rw-r--r-- 1 root root 396 Sep 13 10:46 id_rsa.pub
-rw-r--r-- 1 root root 3386 Sep 3 13:51 known_hosts
其中:id_rsa是密钥文件,id_rsa.pub是公钥文件。
第二步 复制公钥文件到服务端: 192.168.22.27
localhost .ssh # scp id_rsa.pub 192.168.22.27:/root/.ssh/192.168.22.27
登陆到服务器端:192.168.22.27
localhost ~ # cd .ssh
localhost .ssh # cat 192.168.22.27 >> authorized_keys
目录.ssh权限需是755,文件authorized_keys的权限需是600。
然后从客户端:192.168.22.26就可以无密码ssh到服务器端:192.168.22.27