一、环境
CentOS 6.4x64位
zabbix-c1:192.168.3.29
zabbix-c2:192.168.3.30
二、配置从zabbix-c1进行ssh到zabbix-c2免密码验证
在zabbix-c1上生成密钥对,整个过程回车即可。
[root@zabbix-c1 ~]# ssh-keygen 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: 7d:ca:81:d9:35:c5:ab:7b:93:f0:b8:0f:8a:e8:63:c2 root@zabbix-c1 The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | .. | | .. | | o . | | = . .. | | S + .. | | . +o | | . o .= . | | E o. . .o.= | | +o.. . .+.. | +-----------------+
在/root/.ssh目录下回生成私钥(id_rsa)和公钥(id_rsa.pub)
[root@zabbix-c1 ~]# ll /root/.ssh/ total 12 -rw------- 1 root root 1675 May 4 09:31 id_rsa #私钥 -rw-r--r-- 1 root root 396 May 4 09:31 id_rsa.pub #公钥 -rw-r--r-- 1 root root 394 Apr 30 10:29 known_hosts
三、将公钥复制到目标机器(zabbix-c2)的authorized_keys文件中
[root@zabbix-c1 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 192.168.3.30 root@192.168.3.30's password: #这里在第一次连接的时候需要输入密码 Now try logging into the machine, with "ssh '192.168.3.30'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. #使用ssh登陆到zabbix-c2机器上,整个过程不需要输入密码 [root@zabbix-c1 ~]# ssh 192.168.3.30 Last login: Mon May 4 09:24:02 2015 from 192.168.3.2 [root@zabbix-c2 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:68:fd:3e brd ff:ff:ff:ff:ff:ff inet 192.168.3.30/24 brd 192.168.3.255 scope global eth0 inet6 fe80::20c:29ff:fe68:fd3e/64 scope link valid_lft forever preferred_lft forever [root@zabbix-c2 ~]# hostname zabbix-c2
四、使用ssh协议远程执行命令
#远程查看内存信息 [root@zabbix-c1 ~]# ssh 192.168.3.30 free -m total used free shared buffers cached Mem: 988 121 867 0 8 44 -/+ buffers/cache: 68 919 Swap: 1999 0 1999 #远程查看iptables信息 [root@zabbix-c1 ~]# ssh 192.168.3.30 iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports 10050:10051 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination #远程查看IP地址信息 [root@zabbix-c1 ~]# ssh 192.168.3.30 ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:68:fd:3e brd ff:ff:ff:ff:ff:ff inet 192.168.3.30/24 brd 192.168.3.255 scope global eth0 inet6 fe80::20c:29ff:fe68:fd3e/64 scope link valid_lft forever preferred_lft forever #远程查看hostname信息 [root@zabbix-c1 ~]# ssh 192.168.3.30 hostname zabbix-c2