一、建立信任关系
192.168.0.248 为管理机
192.168.0.244 192.168.0.249 为远程linux服务器
1、在管理机生成证书
[root@centos-5 ~]# 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:
d0:c6:81:a5:0a:74:5b:35:1c:b5:69:ba:5a:6f:4a:df [email protected]
2、将管理机上的公钥传送到各远程服务器(如果已经存在/root/.ssh/authorized_keys,则需要在后面追加公钥内容)
如远程服务器更改了默认的ssh端口号,就使用scp -P 17173,17173为端口号
scp .ssh/id_rsa.pub 192.168.0.244:/root/.ssh/authorized_keys
scp .ssh/id_rsa.pub 192.168.0.249:/root/.ssh/authorized_keys
管理机与远程主机信任关系建立完毕
3、测试
[root@centos-5 ~]# ssh 192.168.0.244 ifconfig #查看远程服务器ip
eth0 Link encap:Ethernet HWaddr 00:0C:29:18:B7:2A
inet addr:192.168.0.244 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe18:b72a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11326 errors:0 dropped:0 overruns:0 frame:0
TX packets:357 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1593328 (1.5 MiB) TX bytes:56869 (55.5 KiB)
远程查看多台服务器的信息箱
[root@centos-5 ~]# cat iplist.txt
192.168.0.244
192.168.0.249
[root@localhost tmp]# vi manger.sh
#!/bin/bash
for ip in `cat iplist.txt`
do
echo "$ip---------------"
ssh $ip $1
done
[root@centos-5 ~]# sh manage.sh df -h
192.168.0.244---------------
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda2 7936288 6812476 714156 91% /
/dev/sda5 20401592 13175824 6172712 69% /data
/dev/sda1 101086 11985 83882 13% /boot
tmpfs 258304 0 258304 0% /dev/shm
192.168.0.249---------------
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda2 6940516 2657024 3925244 41% /
/dev/sda5 11235340 4991868 5663528 47% /data
/dev/sda1 101086 18189 77678 19% /boot
tmpfs 512964 0 512964 0% /dev/shm
3.批量分发
#!/bin/bash
for ip in `cat iplist.txt`
do
echo "$ip---------------"
scp -r -p $1 $ip:$2
done
参考文档:http://navyaijm.blog.51cto.com/4647068/835309
其实我们也可以去192.168.0.244 192.168.0.249 远程linux服务器生成密钥对,然后把私钥192.168.0.244 和192.168.0.249的私钥拷贝到192.168.0.248,分别保存为/root/.ssh/244、/root/.ssh/249,然后通过
ssh -p 36900 -i /root/.ssh/244 192.168.0.244 ifconfig
ssh -p 36900 -i /root/.ssh/249 192.168.0.249 ifconfig
这个是今天看到别人这样搞的。关键是公钥必须要放到要登录的服务器,不管是在服务端,还是客户端生成的密钥。
今天从香港机房173登录180
1.首先去180生成密钥对文件,authorized_keys不用说了,按照上面的方法搞,然后把密钥文件
vi /root/.ssh/id_rsa 的内容拷贝到173上vi /root/.ssh/jiucang180
chmod 600 /root/.ssh/jiucang180
2. ssh -p 36009 -i /root/.ssh/jiucang180 192.168.0.180 ifconfig
搞定收工,呵呵。
以下是从20同步到39服务器:
在39服务器执行:
1.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:
cd .ssh/
mv id_rsa.pub authorized_keys
chmod 600 authorized_keys
2.到20服务器执行以下操作
cd /root/.ssh/
vim sjhl_39,拷贝39上面的私钥文件/root/.ssh/id_rsa内容到sjhl_39里面去
执行提示权限太高
ssh -p 3600 -i 39 192.168.1.39 ifconfig
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'sjhl_39' are too open.
修改为600, chmod 600 39,再执行可以了。