SSH不输入密码连接远程Linux主机

系统环境 : ubuntu 6.06 / ubuntu 6.10
SSH服务 : openssh-server

1) 在本地主机生成密钥对

ssh-keygen -t rsa

这个命令生成一个密钥对:id_rsa(私钥文件)和id_rsa.pub(公钥文件)。默认被保存在~/.ssh/目录下。

2) 将公钥添加到远程主机的 authorized_keys 文件中

将文件上传到远程主机中

scp ~/.ssh/id_rsa.pub root@192.168.1.23:/root/

SSH到登陆到远程主机,将公钥追加到 authorized_keys 文件中

cat /root/id_rsa.pub >> /root/.ssh/authorized_keys

或直接运行命令:

cat ~/.ssh/id_dsa.pub|ssh root@192.168.1.23 'sh -c "cat - >>~/.ssh/authorized_keys2"'

3) 重启 open-ssh 服务

/etc/init.d/ssh restart

4) 测试

ssh root@192.168.1.23
scp /home/onion/.ssh/id_rsa.pub root@192.168.1.23:/root/

呵呵,不用输入密码了:)

== 附录 ==
/etc/ssd/sshd_config 几个关键配置点

PermitRootLogin yes
PasswordAuthentication yes
# AuthorizedKeysFile .ssh/authorized_keys # 这个地方尽量使用默认,gentoo 下 ~/.ssh/authorized_keys 与 ~/.ssh/authorized_keys2 等效

You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.