SSH是 Secure Shell Protocol(安全外壳协议)的简写。

服务器端口号:22

SSH有证书验证和密码验证两种方式


SSH协议在预设状态中提供了两个服务器功能:

 1、一个是类似 Telnet 的远程 shell,也就是 SSH

 2、一个是类似 FTP 的 sftp-server,也就是更安全的 FTP 服务


公钥和私钥的概念

 1、简单的说,公钥就是给别人的,而私钥只能留给自己。

 2、以自己作为客户端来说,首先你要取得服务器端的公钥,然后把自己的公钥发送给服务器端。

 3、最后的结果是

   客户端 = 客户端私钥 + 服务器端公钥

   服务器端 = 服务器端私钥 +客户端公钥


连接过程简介

服务器端如何产生新的 ssh 公私钥

[root@study ~]# rm -rf /etc/ssh/ssh_host*
[root@study ~]# systemctl restart sshd
# 重启sshd服务即可


SSH 客户端程序

 linux下直接使用 ssh 命令

[root@study ~]# ssh 192.168.1.1
# 以 root 用户登录,默认以当前用户登录对方SSH

[root@study ~]# ssh [email protected]
# 以对方 study 用户登录SSH

 windows下使用

  putty    http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

  secureCRT   属于商业软件,可以下载破解版

  x-shell    http://xshell.en.softonic.com/


SFTP

  SSH是登入远程服务器,如果你想从服务器下载或上传文件,那就必须使用 sftp 或 scp。这两个指令都是使用 ssh 的通道(port 22),只是模拟成 FTP 与复制的动作。


[root@study ~]# sftp [email protected]
  Connecting to localhost...
  student@localhost's password:    <== 输入密码
  sftp> exit   <== 输入 ftp 相关指令


针对服务器的操作
显示当前目录 pwd
改变当前目录 cd
列出当前目录下文件 ls
建立目录 mkdir
删除文件 rm
改变文件群组 chgrp
改变文件拥有者 chown
改变文件权限 chmod
改变文件名 rename



针对本地的操作
显示本地当前目录 lpwd
改变本地当前目录 lcd
列出本地当前目录下文件 lls
在本地建立目录 lmkdir


针对文件 上传/下载 的操作
将本地文件上传至服务器

put [本地目录或文件] [远程]


put [本地目录或文件]

这种格式会上传至服务器当前目录

从服务器下载文件至本地

get [远程目录或文件] [本地]


get [远程目录或文件]

这种格式会下载到本地当前目录


get *

get *.rpm

这种格式也可以


windows下工具

  下载地址:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

psftp> open 192.168.1.1
login as: root
[email protected]'s password:
Remote working directory is /root
# 该软件通过 open 打开远程主机


SCP

  通常使用 sftp 是因为可能不知道服务器上面有什么文件名的文件存在,如果已经知道服务器上的文件名称后,那么最简单的文件传输则是通过 scp 这个指令。

  windows下工具下载地址:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

[root@study ~]# scp [-pr] [-l 速率] file [帐号@]主机:目录名   《==上传
[root@study ~]# scp [-pr] [-l 速率] [帐号@]主机:file 目录名   《==下载
选项:
  -p : 保留原本文件的权限数据
  -r : 复制来源为目录时,可以包含子目录
  -l : 限制传输的速度,单位为 Kbits/s ,比如 [-l 800] 代表传输速率限制为 100Kbytes/s
  
[root@study ~]# scp /etc/hosts* [email protected]:~
[email protected]'s password:    <==输入 student 密码
# 将本机的 /etc/hosts* 全部复制到 192.168.1.1 上 student 用户的家目录下

[root@www ~]# scp [email protected]:/etc/bashrc /tmp
# 将 192.168.1.1 远程主机下的 /etc/bashrc 复制到本机的 /tmp 目录下


1、如果服务器重新安装或者公钥发生改变,假设服务器使用相同的IP,造成相同IP的服务器公钥不同,该如何解决?(操作在客户端)

[root@study ~]# ssh [email protected]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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-middleattack)!
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
a7:2e:58:51:9f:1b:02:64:56:ea:cb:9c:92:5e:79:f9.
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 <==有问题的数据行号
RSA host key for localhost has changed and you have requested strict checking.
Host key verification failed.

# 上面那句话告诉你:/root/.ssh/known_hosts 的第 1 行,上次的公钥与本次收到的公钥结果不同
# 方法:vim /root/.ssh/known_hosts,并将第 1 行删除,再重新 ssh 连接就可以了


2、如何不输入密码建立 SSH 连接?(操作在客户端)

 1.客户端生成本地公私钥

 ssh-keygen   //本地生成RSA公钥和私钥,执行成功后会在当前用户家目录 .ssh/ 下生成 id_rsa(私钥)和 id_rsa.pub(公钥)

[root@study ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  <==按enter
Enter passphrase (empty for no passphrase):   <==按enter
Enter same passphrase again:   <==按enter
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:
24:6e:f2:c7:b6:2c:b1:4c:df:8a:31:e8:07:eb:78:20 [email protected]
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|                 |
|                 |
|      . .        |
|     . o         |
|    . o S        |
|E . .=o.         |
| . ..=+++.       |
|   oo =B...      |
|  .oo...+.       |
+-----------------+


 2.将本地公钥拷贝至目标主机(服务器端)并连接

 ssh-copy-id   //将本机的公钥复制到远程机器的authorized_keys文件中,ssh-copy-id也能让你有到远程机器的home, ~./ssh , 和 ~/.ssh/authorized_keys的权利

 ssh-copy-id 10.0.0.11   //导出 id_rsa.pub(公钥),连接对方并将公钥传至对方服务器

 ssh-copy-id [email protected]   //则将公钥导出至10.0.0.11root用户.ssh目录下

 ssh-copy-id [email protected]    //则将公钥导出至10.0.0.11student用户.ssh目录下

[root@study ~]# ssh-copy-id 10.0.0.11
The authenticity of host '10.0.0.11 (10.0.0.11)' can't be established.
ECDSA key fingerprint is c9:77:ee:3f:f6:82:25:66:0b:85:d8:d1:46:59:8f:ef.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:        <==输入目标主机root用户密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.0.0.11'"
and check to make sure that only the key(s) you wanted were added.

[root@study .ssh]# ssh 10.0.0.11
Last login: Fri Feb  5 23:48:56 2016 from 10.0.0.129
[root@test ~]#
# 成功,不再需要输入密码


# 其实,当前 root 用户通过 ssh-keygen 生成的本地公私钥不仅仅 root 用户可以用,其他本地用户也可以。
# 假设本地还有一个叫做 hello 的用户,我想通过 hello 用户连接到远端的 root 用户和 student 用户也不需要密码。
[root@study /]# mkdir /home/hello/.ssh
[root@study /]# cp /root/.ssh/id_rsa /home/hello/.ssh/
[root@study /]# chown -R hello:hello /home/hello/.ssh
[root@study hello]# su - hello

[hello@study ~]$ ssh [email protected]
The authenticity of host '10.0.0.11 (10.0.0.11)' can't be established.
ECDSA key fingerprint is c9:77:ee:3f:f6:82:25:66:0b:85:d8:d1:46:59:8f:ef.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.11' (ECDSA) to the list of known hosts.
Last login: Sat Feb  6 00:00:15 2016 from 10.0.0.129
[root@test ~]#
# 成功!hello 用户登录到对方 root 用户


3、通过证书验证安装一个后门的思路

#一般来说,SSH会在用户的家目录 .ssh/ 下有以下四个文件:
[root@study .ssh]# ls
authorized_keys id_rsa  id_rsa.pub  known_hosts
# authorized_keys 服务器端上记录通过证书验证连接进来的客户端公钥
# id_rsa 客户端本地的私钥
# id_rsa.pub 客户端本地的公钥
# known_hosts 客户端记录的服务器端的公钥

# 通过以下命令即可实现无密码登录
[root@study ~]# ssh-keygen
# 生成客户端本地公私钥
[root@study ~]# ssh-copy-id [email protected]
# 输入 ssh-copy-id 命令后还是需要输入服务器端用户的密码的
# 将客户端的公钥导出到 10.0.0.11 的 root 的家目录下的 .ssh/authorized_keys 文件里

# 故可通过其他方式(比如物理接触),将生成的客户端公钥拷贝至 root 的家目录下的 .ssh/authorized_keys 文件里,这样便能实现远程无密码登录


4、如何发现当前远程登录用户剔除并去除隐患(操作在服务端)

[root@test ~]# who 或 w
root     tty1         2016-02-05 21:56
root     pts/0        2016-02-06 00:27 (10.0.0.2)
root     pts/1        2016-02-06 00:35 (10.0.0.129)
# 假设root从 pts/1 这个入口非法登录

[root@test ~]# pkill -kill -t pts/1
# 杀死pts/1远程登录进程(相当于踢掉远程登录的非法用户)

# 但是仅仅踢掉是不够的,假设非法登录是通过上面的证书验证方式
[root@test ~]# cd .ssh
[root@test .ssh]# vim authorized_keys
#  这里可能不仅仅在 root 的家目录下有 authorized_keys 文件,最好把 /home/ 目录下所有用户的家目录都检查一遍

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3WTJS0riNqh8AW06s7CiXiZ+QAdeYmdsHUNxkR+8BgYbamHuy7v27nKHdT9909zPruu/JIJLgNZx2ZgtbRQMgUUWma+gHBtE1GY7hdn/sRc5nBxEnpC+go2/RIC5oBO+Ly17Eg7TnepM7QZNA4Uq5nx+CxFsEy4eM75p9DP78nP+8ZRdJkiXsQAD95teGxQDe8ckrYokwLUfXbk9Q1mwbrfNGC2nR2i5AzxOvdo//YkYX9xydUIB2YM27KE/o9WEBR7/K6iPPFi7Cv5mvRX85+kyCN8RpkReuiLA+LDmGbB3z2OfQsVtbfFxW+2q1vhPuuhl8KJStR7zMEjDFmBfb [email protected]

# 删除该条记录即可,即为删除非法公钥


5、SSH 服务端简单设置(操作在服务端)

[root@test ~]# vim /etc/ssh/sshd_config

PermitRootLogin no   禁止root用户连入SSH
PasswordAuthentication no   禁止通过密码验证,即输入用户名后不会出现输入密码的框


6、一个更安全的 SSH 登录方式

# 假设有两台主机,第三方想要通过 SSH 登录到你的重要主机上,必须先经过跳板主机,通过跳板主机才能到达重要主机。

# 假设重要主机的 IP 地址为192.168.1.1,且存在 root 和 student 两个用户。
# 假设跳板主机有两张以上的网卡,并在一个无线局域网的环境里(与重要主机相互隔离的网段),该局域网内只有跳板主机自己。
# 第三方先通过连接至跳板主机所在的局域网,进入无线局域网,然后连接到跳板主机,通过跳板主机中转,最终到达重要主机。

# 思路如下:
1、跳板主机生成密码
ssh-keygen

2、跳板主机以 student 用户连入重要主机
ssh-copy-id [email protected]
# 这里会要求输入一次 student 用户的密码

3、修改重要主机的配置
vim /etc/ssh/sshd_config
 PermitRootLogin no
 PasswordAuthentication no
# 禁止重要主机的 root 用户登录,禁止通过密码验证

4、重启服务
systemctl restart sshd

5、结果
 1)第三方首先得连入跳板主机所在的局域网
 2)连入跳板主机所在的局域网后,还需要跳板主机的 root 用户权限
# 这里更复杂的做法是:禁止跳板主机的 root 用户登录,以其他用户登录后再 su - 切换为 root
 3)获取到跳板主机的 root 用户权限后,使用 root 用户是无法登录到重要主机的,只能通过 student 用户登录,student 用户由于已经传递了公钥,所以是不需要密码的,且这里也无法通过密码验证。
 4)获得 student 用户的权限后,再通过 su - 切换成 root 用户,这里需要重要主机的 root 密码