SSH是一个专为远程登录会话和其他网络服务提供安全性的协议。默认状态下SSH链接是需要密码认证的,可以通过添加系统认证(即公钥-私钥)的修改,修改后系统间切换可以避免密码输入和SSH认证。
对信息的加密和解密采用不同的key,这对key分别称作private key和public key,其中,public key存放在欲登录的服务器上,而private key为特定的客户机所持有。
当客户机向服务器发出建立安全连接的请求时,首先发送自己的public key,如果这个public key是被服务器所允许的,服务器就发送一个经过public key加密的随机数据给客户机,这个数据只能通过private key解密,客户机将解密后的信息发还给服务器,服务器验证正确后即确认客户机是可信任的,从而建立起一条安全的信息通道。
通过这种方式,客户机不需要向外发送自己的身份标志“private key”即可达到校验的目的,并且private key是不能通过public key反向推断出来的。这避免了网络窃听可能造成的密码泄露。客户机需要小心的保存自己的private key,以免被其他人窃取,一旦这样的事情发生,就需要各服务器更换受信的public key列表。
如下所示:
Linux下修改主机名的完整步骤如下:
第一步:
#hostname Server1第二步:
修改/etc/sysconfig/network中的hostname第三步:
修改/etc/hosts文件
[root@Server1 ~]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key(/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: 7b:aa:08:a0:99:fc:d9:cc:d8:2e:4b:1a:c0:6b:da:e4root@Server1 The key's randomart image is: +--[ RSA 2048]----+ | | | | | | |. | |o. S | |++. . | |+=o. . . | |o+=oB. o | |..E==*... | +-----------------+输入后,会提示创建.ssh/id_rsa、id_rsa.pub的文件,其中第一个为密钥,第二个为公钥。过程中会要求输入密码,为了ssh访问过程无须密码,可以直接回车 。
(2)补充说明:
ssh-keygen:生成秘钥
其中:
-t指定算法
-f 指定生成秘钥路径
-N 指定密码
[root@Server1 ~]# ls -l .ssh 总用量 8 -rw-------. 1 root root 1675 12月 10 22:20 id_rsa -rw-r--r--. 1 root root 394 12月 10 22:20 id_rsa.pub可以发现 ssh目录下的两枚钥匙。
[root@server1 .ssh]# scp [email protected]:~/.ssh/ The authenticity of host '192.168.1.3(192.168.1.3)' can't be established. RSA key fingerprint is93:eb:f9:47:b1:f6:3f:b4:2e:21:c3:d5:ab:1d:ae:65. Are you sure you want to continueconnecting (yes/no)? yes Warning: Permanently added '192.168.1.3'(RSA) to the list of known hosts. [email protected]'s password: id_rsa.pub [root@server1 .ssh]# scp [email protected]:~/.ssh/authorized_keys The authenticity of host '192.168.1.4(192.168.1.4)' can't be established. RSA key fingerprint is93:eb:f9:47:b1:f6:3f:b4:2e:21:c3:d5:ab:1d:ae:65. Are you sure you want to continueconnecting (yes/no)? yes Warning: Permanently added '192.168.1.4'(RSA) to the list of known hosts. [email protected]'s password: id_rsa.pub到Server2和Server3目录下执行下面的命令
#cat id_dsa.pub >> ~/.ssh/authorized_keys
$ chmod 600 authorized_keys设置.ssh目录权限
$ chmod 700 -R .ssh
[root@server1 .ssh]# ssh 192.168.1.3 Last login: Tue Dec 10 22:34:02 2013 [root@Server2 ~]# [root@Server2 ~]# [root@Server2 ~]#
[root@server1 .ssh]# cat /etc/hosts 192.168.1.2 Server1 192.168.1.3 Server2 192.168.1.4 Server3我们试试下面这种ssh登录方式:
[root@server1 .ssh]# ssh Server3 The authenticity of host 'server3(192.168.1.4)' can't be established. RSA key fingerprint is93:eb:f9:47:b1:f6:3f:b4:2e:21:c3:d5:ab:1d:ae:65. Are you sure you want to continueconnecting (yes/no)? yes Warning: Permanently added 'server3' (RSA)to the list of known hosts. Last login: Tue Dec 10 22:34:08 2013