SSH

来源:互联网 发布:ubuntu 电源设置打不开 编辑:程序博客网 时间:2024/06/05 15:51

client与server建立连接的过程:

  1. 建立client和server之间的安全连接。具体见Establishing the Secure Connection

  2. 本机会主动利用接收到的服务器的Public Key去对比本机中~/.ssh/known_hosts有无相关的公匙。

  3. 如果有,则对比记录是否相同,若相同则继续登录动作。若不相同,出现警告信息,退出登录动作。

  4. 如果没有,询问用户是否记录,是则写入~/.ssh/known_hosts并执行登录动作,否则退出登录。

  5. 如果服务器IP不变但公匙发生改变,则要删除known_hosts中的旧的公匙记录,登录时再重新写入即可。

  6. 建立安全连接之后,client需向server证明自己。共有6种方法。常用的有口令认证和公匙认证


口令认证的过程是:

  1. 远程主机收到用户的登录请求,把自己的公钥发给用户。

  2. 用户使用这个公钥,将登录密码加密后,发送回来。

  3. 远程主机用自己的私钥,解密登录密码,如果密码正确,就同意用户登录。

优点:

Password authentication is quite convenient because it requires no additional setup for the user. You don't need to generate a key, create a ~/.ssh directory on the server machine, or edit any configuration files.


公钥认证(public-key authentication) 的过程是:

  1. client将自己的公钥储存在远程主机上。

  2. 远程主机收到用户的登录请求,在所要求登录的帐户中查询文件.ssh/authorized_keys。如果没有则拒绝登陆

  3. 如果有则会向client发送一段用client的public key加密的随机字符串作为挑战。

  4. client用自己的私钥解密后,再发给server。

  5. 如果两个match则认证通过,登录成功。

Public-key authentication uses public-key cryptography to verify the client's identity. To access an account on an SSH server machine, the client proves that it possesses a secret: specifically, the private counterpart of an authorized public key. A key is "authorized" if its public component is contained in the account's authorization file (e.g., ~/.ssh/authorized_keys).


The public-key method is the most secure authentication option available in SSH, generally speaking. First of all, the client needs two secrets to authenticate: the private key, and the passphrase to decrypt it. Stealing either one alone doesn't compromise the target account (assuming a strong passphrase). The private key is infeasible to guess and never leaves the client host, making it more difficult to steal than a password. A strong passphrase is difficult to guess by brute force, and if necessary, you can change your passphrase without changing the associated key. Also, public-key authentication doesn't trust any information supplied by the client host; proof of possession of the private key is the sole criterion.


这种方法要求client必须提供自己的公钥。可用ssh-keygen生成一对。公匙用来加密,私匙用来解密。其实现步骤为:

  1. 如果没有ssh,先安装ssh,

    1. sudo apt-get install ssh                        slave 安装     

    1. sudo apt-get install openssh-server     master安装

  2. client生产密钥对

    1. ssh-keygen -t rsa -f ~/.ssh/id_rsa

    2. 默认在~/.ssh/, .pub代表是公匙

  3. 将private key放在client的~/.ssh中,其权限为700

  4. client将自己的公钥储存在远程主机上。有两种方法:

    1. $ ssh-copy-id user@host

    2. $ ssh user@host 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub

      1. "$ ssh user@host",表示登录远程主机;

      2. 单引号中的mkdir .ssh && cat >> .ssh/authorized_keys,表示登录后在远程shell上执行的命令

        1. "$ mkdir -p .ssh"的作用是,如果用户主目录中的.ssh目录不存在,就创建一个;

        2. 'cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub的作用是,将本地的公钥文件~/.ssh/id_rsa.pub,重定向追加到远程文件authorized_keys的末尾。


补充:

服务器时常需要配置无密码的登录方式,最一般的设置方式如下:
# ssh-keygen
# cat ~/.ssh/id_rsa.pub | ssh user@server "cat - >> ~/.ssh/authorized_keys"

注:ssh user@server后面可以跟命令,是远程登录并执行这个命令。

       在管道命令中,如果想用前一个命令的stdout作为后一个命令的stdin, 那么这个stdout可以用-替代。


写入authorized_keys文件后,公钥登录的设置就完成了。简单的说就是如下几步:

Client:  

  1. ssh-keygen -t rsa -f ~/.ssh/id_rsa

  2. scp ~/.ssh/id_rsa.pub 

  3. scp ~/.ssh/id_rsa.pub 

Server: 

  • 确保公匙存放在用户打算连接的所有机器~/.ssh/authorized_keys 文件中

  • cat ~/.ssh/id_rsa.pub >>~/.ssh/authorized_key          

注意在这client和server上的文件权限:

  • chmod 700 ~/.ssh

  • chmod 644 ~/.ssh/authorized_keys


如果登录被拒绝

If access to the remote system is still denied you should check the permissions of the following files on the remote system:

  • the home directory itself

  • the ~/.ssh directory

  • the ~/.ssh/authorized_keys file

在client上设置.ssh 为700,在server所申请登录帐户上设置authorized_keys 为644和.ssh 为700。


在客户端来看,SSH提供两种级别的安全验证

  • 第一种级别(基于密码的安全验证),知道帐号和密码,就可以登录到远程主机,并且所有传输的数据都会被加密。但是,可能会有别的服务器在冒充真正的服务器,无法避免被“中间人”攻击。

  • 第二种级别(基于密匙的安全验证),需要依靠密匙,也就是你必须为自己创建一对密匙,并把公有密匙放在需要访问的服务器上。客户端软件会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在你在该服务器的用户根目录下寻找你的公有密匙,然后把它和你发送过来的公有密匙进行比较。如果两个密匙一致,服务器就用公有密匙加密“质询”(challenge)并把它发送给客户端软件。从而避免被“中间人”攻击。


在服务器端,SSH也提供安全验证。 

  • 在第一种方案中,主机将自己的公用密钥分发给相关的客户端,客户端在访问主机时则使用该主机的公开密钥来加密数据,主机则使用自己的私有密钥来解密数据,从而实现主机密钥认证,确定客户端的可靠身份。 

  • 在第二种方案中,存在一个密钥认证中心,所有提供服务的主机都将自己的公开密钥提交给认证中心,而任何作为客户端的主机则只要保存一份认证中心的公开密钥就可以了。在这种模式下,客户端必须访问认证中心然后才能访问服务器主机。


    Another way to do this is that:

    generate the public key and private key on the master node and send these two keys (actually only the private key is needed) to the client, following put the keys to the .ssh folder on client. Then this should be fine with client ssh to the master node.


References:

[*]http://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch03_04.htm

[*]http://docstore.mik.ua/orelly/networking_2ndEd/ssh/ch02_04.htm

http://docstore.mik.ua/orelly/networking_2ndEd/ssh/index.htm

http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html

http://www.eng.cam.ac.uk/help/jpmg/ssh/ssh-detail.html

http://kimmo.suominen.com/docs/ssh/#ssh-add