ssh
[root@rhel6 ~]# rpm -qa | grep openssh
openssh-server-5.3p1-70.el6.x86_64
openssh-clients-5.3p1-70.el6.x86_64
openssh-5.3p1-70.el6.x86_64
openssh-askpass-5.3p1-70.el6.x86_64
[root@rhel6 ~]# cat /etc/ssh/sshd_config
#Port 22 //设置ssh服务的端口
#MaxStartups 10 //设置最大连接数
#ListenAddress 0.0.0.0
#PermitRootLogin yes
Protocol 2 //只允许SSH2协议
SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding yes
Subsystem sftp /usr/libexec/openssh/sftp-server
当客户端登入远程服务器时,客户端会主动的接收到的服务器的公钥(public key) 去比对 ~/.ssh/known_hosts 有无相关的公钥, 然后进行底下的动作:
若接收的公钥尚未记录,则询问用户是否记录。若接受则写入 ~/.ssh/known_hosts 且继续登入的后续工作;若不接收则不写入该文件,并且离开登入工作;
若接收到的公钥已有记录,则比对记录是否相同,若相同则继续登入动作;若不相同,则出现警告信息,且离开登入的动作。
[root@rhel6 ~]# rm -f .ssh/known_hosts
[root@rhel6 ~]# ssh rhel6
The authenticity of host 'rhel6 (192.168.1.119)' can't be established.
RSA key fingerprint is 1a:cf:92:de:28:7d:f2:e0:e8:e6:ad:f1:7c:40:6a:67.
Are you sure you want to continue connecting (yes/no)? yes //接受并在known_hosts中创建公钥
Warning: Permanently added 'rhel6,192.168.1.119' (RSA) to the list of known hosts.
root@rhel6's password:
Last login: Mon Dec 31 11:27:22 2012 from 192.168.1.19
[root@rhel6 ~]# cat .ssh/known_hosts
rhel6,192.168.1.119 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA08gfRmTgp6wM1GPgbVBsAiL6dOaKoViS9w/aL3P/NVGjYANfKQQxx2yagOxqOIFV5wefnrutdgoEmYm9sWl+9AtIf4XgMHupGWlq3jK4LWkKrN2Lg7HdijpbKzH2XuHcI1k9sRzB6F2Xhx3YdTnQKyT8wb9spKp9hzTL4ztGXrrcRW9lXBrz7jp9m4HOwim44j6SSVPTAVrCZWho2X+I27f/6DbCHNfFXV1mi+g7ERo2c8e4KwoKComXaa+E/PsBPKWOuvJgujl1VPQ2hTAWPSVXA67eR9o+39c/cOliDPq/SGsGXtWxZei9FM7G+OZAI5RdZ/Fqmbvivzfweg7IZQ==
每一次启动sshd服务时,sshd服务端都会主动去找/etc/ssh/ssh_host*的公私钥文件,如果不存在则会重新创建公私钥
[root@rhel6 ~]# ls /etc/ssh/ssh_host_*
/etc/ssh/ssh_host_dsa_key /etc/ssh/ssh_host_key /etc/ssh/ssh_host_rsa_key //私钥
/etc/ssh/ssh_host_dsa_key.pub /etc/ssh/ssh_host_key.pub /etc/ssh/ssh_host_rsa_key.pub //公钥
[root@rhel6 ~]# rm -f /etc/ssh/ssh_host_*
[root@rhel6 ~]# ls /etc/ssh/ssh_host_*
ls: cannot access /etc/ssh/ssh_host_*: No such file or directory
[root@rhel6 ~]# /etc/init.d/sshd restart
Stopping sshd: [ OK ]
Generating SSH1 RSA host key: [ OK ] //创建SSH1的RSA公私钥
Generating SSH2 RSA host key: [ OK ] //创建SSH2的RSA公私钥
Generating SSH2 DSA host key: [ OK ] //创建SSH2的DSA公私钥
Starting sshd: [ OK ]
[root@rhel6 ~]# ls /etc/ssh/ssh_host_*
/etc/ssh/ssh_host_dsa_key /etc/ssh/ssh_host_key /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_dsa_key.pub /etc/ssh/ssh_host_key.pub /etc/ssh/ssh_host_rsa_key.pub
[root@rhel6 ~]# ssh rhel6
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
16:1b:b4:09:20:fe:8f:48:12:e1:3c:16:5e:86:67:8b.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:1 //由于更新了公私钥,故提示known_hosts文件中第1行的信息不匹配
RSA host key for rhel6 has changed and you have requested strict checking.
Host key verification failed.
[root@rhel6 ~]# sed -i '1d' .ssh/known_hosts //删除known_hosts的第一行内容
[root@rhel6 ~]# ssh rhel6
The authenticity of host 'rhel6 (192.168.1.119)' can't be established.
RSA key fingerprint is 16:1b:b4:09:20:fe:8f:48:12:e1:3c:16:5e:86:67:8b.
Are you sure you want to continue connecting (yes/no)? yes //重新更新known_hosts中的公钥
Warning: Permanently added 'rhel6,192.168.1.119' (RSA) to the list of known hosts.
root@rhel6's password:
Last login: Mon Dec 31 13:28:30 2012 from rhel6
ssh [-f] [-p port_num] [user@]IP [CMD]
-f :需要配合后面的[CMD],不登入远程主机直接发送一个指令,若不加-f参数则需等待后面的CMD指令执行完毕才会离开远程主机
-p :指定sshd监听的端口
-X :开启X11 Forwarding(X11 forwarding是基于SSH使用远程X-Windows应用,需配合xhost +)
-Y :开启X11 Forwarding
[root@rhel6 ~]# vi ssh_test.sh //创建一个用于测试的脚本
#!/bin/sh
echo '####### ssh without "-f" ############'
date
ssh rhel6 sleep 10
date
echo '####### ssh with "-f" ############'
date
ssh -f rhel6 sleep 10
date
[root@rhel6 ~]#chmod +x ssh_test.sh
[root@rhel6 ~]# ./ssh_test.sh
####### ssh without "-f" ############
Mon Dec 31 14:24:26 CST 2012
Mon Dec 31 14:24:36 CST 2012 //需等待远程主机的指令执行完毕才会离开
####### ssh with "-f" ############
Mon Dec 31 14:24:36 CST 2012
Mon Dec 31 14:24:36 CST 2012 //远程主机执行指令后立即离开
[root@rhel6 ~]# ssh rhel6
Last login: Mon Dec 31 15:13:16 2012 from rhel6
[root@rhel6 ~]# echo $DISPLAY
[root@rhel6 ~]# exit
[root@rhel6 ~]# ssh -X rhel6
Last login: Mon Dec 31 15:17:19 2012 from rhel6
[root@rhel6 ~]# echo $DISPLAY
localhost:10.0
