有时候在运维过程中,需要连接N多机器进行故障排查,即使密码都相同,敲来敲去也很麻烦,所以有了下文。
A机器上的操作:
- [root@localhost ~]# cd .ssh/
- [root@localhost .ssh]# ll
- 总用量 4
- -rw-r--r-- 1 root root 1179 3月 27 10:14 known_hosts
- [root@localhost .ssh]# ssh-keygen -t rsa ---生成密钥文件
- 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 saved in /root/.ssh/id_rsa.pub.
- The key fingerprint is:
- 04:e8:26:15:76:ff:18:79:b5:4c:09:b7:19:5b:5f:47 root@localhost
- The key's randomart p_w_picpath is:
- +--[ RSA 2048]----+
- | ooo ..=...E|
- | .o. o . =.B .o|
- | o = . * .|
- | . o . = |
- | o S . |
- | |
- | |
- | |
- | |
- +-----------------+
- [root@localhost .ssh]# ll
- 总用量 12
- -rw------- 1 root root 1675 9月 25 14:35 id_rsa
- -rw-r--r-- 1 root root 396 9月 25 14:35 id_rsa.pub
- -rw-r--r-- 1 root root 1179 3月 27 10:14 known_hosts
- [root@localhost .ssh]# scp id_rsa.pub root@192.168.7.7:/root/.ssh/
- The authenticity of host '192.168.7.7 (192.168.7.7)' can't be established.
- RSA key fingerprint is 9d:95:85:d6:77:9b:d8:56:23:9b:0d:5c:95:22:c9:84.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '192.168.7.7' (RSA) to the list of known hosts.
- root@192.168.7.7's password:
- scp: /root/.ssh/: Is a directory ---B机器没有.ssh目录
- [root@localhost .ssh]# scp id_rsa.pub root@192.168.7.7:/root/.ssh/
- root@192.168.7.7's password:
- id_rsa.pub 100% 396 0.4KB/s
00:00 ---向B机器拷贝生成的密钥文件
B机器上的操作:
- [root@Cacti ~]# ls -lart
- total 40
- -rw-r--r--. 1 root root 100 Sep 23 2004 .cshrc
- -rw-r--r--. 1 root root 129 Dec 4 2004 .tcshrc
- -rw-r--r--. 1 root root 176 May 20 2009 .bash_profile
- -rw-r--r--. 1 root root 18 May 20 2009 .bash_logout
- -rw------- 1 root root 568 Sep 19 14:51 .mysql_history
- -rw-r--r-- 1 root root 191 Sep 19 14:52 .bashrc
- -rw------- 1 root root 1039 Sep 21 16:54 .viminfo
- dr-xr-xr-x. 22 root root 4096 Sep 24 14:37 ..
- -rw------- 1 root root 1947 Sep 25 15:24 .bash_history
- dr-xr-x---. 2 root root 4096 Sep 25 15:25 .
- [root@Cacti ~]# mkdir .ssh
- [root@Cacti ~]# cd .ssh/ ----A机器向B机器拷贝密钥文件
- [root@Cacti .ssh]# ll
- total 4
- -rw-r--r-- 1 root root 396 Sep 25 15:26 id_rsa.pub
- [root@Cacti .ssh]# mv id_rsa.pub authorized_keys
- [root@Cacti .ssh]# chmod 600 /root/.ssh/authorized_keys
- [root@Cacti .ssh]#
操作完以上后,再来从A机器登陆B机器试试看:
- 操作完成后,做下免密码登陆测试:
- [root@localhost .ssh]# ssh root@192.168.7.7
- Last login: Tue Sep 25 14:32:47 2012 from 192.168.7.36
- [root@Cacti ~]# ----A机器登陆B已经不需要再输入密码了!
转载于:https://blog.51cto.com/xlogin/1006201