《SSH无需密码自动登录》由会员分享,可在线阅读,更多相关《SSH无需密码自动登录(8页珍藏版)》请在金锄头文库上搜索。
1、SSH 无需密码登录 SSHD 服务器上段时间,需要在将一台主 CVS 服务器上的文件自动备份到另一台备份CVS 服务上,其中需要用到 SSH 的加密传输,所以,按照我的操作方式整理一下了,供大家参考。此方式在 HA、多服务器 ssh 远程管理等方面都可以用到。0、引言两台服务器,分别是服务器 A:192.168.3.212,服务器 B:192.168.3.213。服务器 A ssh 到服务器 B,通过用户证书,无需输入口令直接登录服务器 B 的 sshd。具体方法如下。首先查看系统是否安装了 ssh 服务,服务名是 openssh# rpm -qa | grep openssh 然后需要配置
2、 sshd_config 使用 key 认证,禁止 passwd 认证#vi /etc/ssh/sshd_config找到#RSAAuthentication yes#PubkeyAuthentication yes#AuthorizedKeysFile .ssh/authorized_keys,把注释符号#去掉,保存退出。修改/etc/ssh/sshd_config 之后,重新启动 sshd 是:/etc/rc.d/init.d/sshd restart1、采用 root 用户登录1.1、在 A 服务器上用 ssh-keygen 产生 root 用户的公钥和私钥。rootFC8Server0
3、1 /# ssh-keygen -t rsa (注意:必须执行此命令才能生成.ssh/目录和相关文件)Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): 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
4、 saved in /root/.ssh/id_rsa.pub.The key fingerprint is:49:3a:87:11:11:15:1f:3d:21:c0:cf:e2:db:3b:d4:48 rootFC8Server01注意:Enter passphrase (empty for no passphrase):和 Enter same passphrase again:提示处直接回车1.2、进入/root/.ssh/目录,并列举文件rootFC8Server01 /# cd /root/.ssh/;ll-rw- 1 root root 1671 11-12 00:15 id_r
5、sa-rw-r-r- 1 root root 398 11-12 00:15 id_rsa.pub然后,在 B 服务器/root/目录先创建.ssh 目录,在该目录下再创建authorized_keys 文件并修改它们的权限,此处非常重要,如果权限不正确,则 ssh 时,提示还需要密码rootFC8Server02 #mkdir .sshrootFC8Server01 #touch .ssh/authorized_keysrootFC8Server01 # chmod 0700 .sshrootFC8Server01 # chmod 0600 .ssh/authorized_keys1.3、将
6、/root/.ssh/id_rsa.pub 文件拷贝为 B 服务器的/root/.ssh/authorized_keys rootFC8Server01.ssh#scp id_rsa.pub 192.168.3.213:/root/.ssh/authorized_keysThe authenticity of host 192.168.3.213 (192.168.3.213) cant be established.RSA key fingerprint is 73:42:09:61:ec:7a:49:8a:d2:5b:63:c0:70:cf:dd:6b.Are you sure you w
7、ant to continue connecting (yes/no)? yesWarning: Permanently added 192.168.3.213 (RSA) to the list of known hosts.root192.168.3.213s password:(这里输入 192.168.3.213 主机上 root 用户的口令) id_rsa.pub100% 3980.4KB/s00:001.4、测试rootFC8Server01 .ssh# ssh 192.168.3.213Last login: Tue Nov 10 23:40:50 2009 from 192.1
8、68.3.212如果 ssh 192.168.3.213,不再提示输入密码,则表示成功,否则失败。2、采用非 root 用户登录2.1、在 A 服务器上创建登录用户 testuser,并设置密码为 testuserrootFC8Server01 # useradd -d “/home/testuser“ -s “/bin/bash“ -c “testuser File Owner“ testuserrootFC8Server01 # passwd testuserChanging password for user testuser.新的 UNIX 口令:无效的口令: 它基于字典单词重新输入新
9、的 UNIX 口令:passwd: all authentication tokens updated successfully.2.2、在 B 服务器上创建登录用户 testuser,并设置密码为 testuserrootFC8Server02 # useradd -d “/home/testuser“ -s “/bin/bash“ -c “testuser File Owner“ testuserrootFC8Server02 # passwd testuserChanging password for user testuser.新的 UNIX 口令:无效的口令: 它基于字典单词重新输入
10、新的 UNIX 口令:passwd: all authentication tokens updated successfully.2.3、用 testuser 用户登录 A 服务器testuserFC8Server01 $ pwd/home/testusertestuserFC8Server01 $ ssh-keygen -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/home/testuser/.ssh/id_rsa): Created directory /home/te
11、stuser/.ssh.Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/testuser/.ssh/id_rsa.Your public key has been saved in /home/testuser/.ssh/id_rsa.pub.The key fingerprint is:36:02:61:f5:a7:9f:c8:d6:99:4f:ed:d1:d1:97:eb:64 testuserFC8Ser
12、ver012.4、用 testuser 用户登录 B 服务器先在当前用户主目录创建.ssh 目录,在该目录下再创建 authorized_keys 文件并修改它们的权限,此处非常重要,如果权限不正确,则 ssh 时,提示还需要密码testuserFC8Server02 $ mkdir .ssh/testuserFC8Server02 $ mkdir testuserFC8Server02 $ ll -a总计 36drwx- 4 testuser testuser 4096 11-11 00:32 .drwxr-xr-x 5 rootroot4096 11-11 00:28 .-rw-r-r-
13、1 testuser testuser33 11-11 00:28 .bash_logout-rw-r-r- 1 testuser testuser176 11-11 00:28 .bash_profile-rw-r-r- 1 testuser testuser124 11-11 00:28 .bashrcdrwxr-xr-x 2 testuser testuser 4096 11-11 00:28 .gnome2drwxrwxr-x 2 testuser testuser 4096 11-11 00:32 .ssh-rw-r-r- 1 testuser testuser 658 11-11
14、00:28 .zshrctestuserFC8Server02 $ chmod 0700 .ssh testuserFC8Server02 $ chmod 0600 .ssh/authorized_keys2.5、将/home/testuser/.ssh/id_rsa.pub 文件拷贝为 B 服务器的/home/testuser/.ssh/authorized_keystestuserFC8Server01$scp .ssh/id_rsa.pub 192.168.3.213:/home/testuser/.ssh/authorized_keystestuser192.168.3.213s pa
15、ssword: (注意此处的密码为 192.168.3.213 主机上testuser 的密码)id_rsa.pub100%4020.4KB/s00:002.6、调整 B 服务器上的.ssh 和 authorized_keys 权限testuserFC8Server02 $ chmod 0700 .sshtestuserFC8Server02 $ chmod 0600 .ssh/authorized_keys此处非常重要,如果权限不正确,则 ssh 时,提示还需要密码2.7、测试testuserFC8Server01 $ ssh 192.168.3.213Last login: Wed Nov 11 00:32:49 2009 from 192.168.3.2如果 ssh 192.168.3.213,不再提示输入密码,则表示成功,否则失败