1、ssh服务监听选项
[root@ns1 ~]# vi /etc/ssh/sshd_config
Port 22                             //监听端口是22
ListenAddress 20.0.0.66                //监听地址为20.0.0.66
Protocol 2                           //使用的是SSH V2 协议
....
UseDNS no                          //禁用DNS反向解析 (一般默认)
重启systemctl reatart sshd   可以修改监听端口号这样当监听号不对的时候就没有办法远程登录了
2、用户远程登录控制
sshd服务默认允许root用户登录,但在Internet 中使用时是非常不安全的。普遍的做
法如下:先以普通用户远程登入,进入安全Shell 环境后,根据实际需要使用su 命令切换
为root用户。
关于sshd服务的用户登录控制,通常应禁止root 用户或密码为空的用户登录。另外,
可以限制登录验证的时间(默认为2分钟)及最大重试次数,若超过限制后仍未能登录则
断开连接。
[root@localhost ~]# vim /etc/ssh/sshd_ config
LoginGraceTime 2m               //登录验证时间为2分钟
PermitRootL ogin no              //禁止root用户登录
MaxAuthTries 2                  //最大重试次数为6
PermitEmptyPasswords no         //禁止空密码用户登录
[root@localhost ~]# systemctl restart sshd
 
输入两次错密码就会出现上面的情况
 
当希望只允许或禁止某些用户登录时,可以使用AllowUsers 或DenyUsers配置,两者
用法类似(注意不要同时使用)。例如,若只允许jerry、 tsengyia和admin用户登录,且其
中admin用户仅能够从IP地址为61.23.24.25的主机远程登录,则可以在
/etc/ssh/sshd_ config 配置文件中添加以下配置。
 
[root@localhost ~]# vim/etc/ssh/sshd config
AllowUsers root@20.0.0.66   //省略部分内容
[root@localhost ~]#systemctl restart sshd       //重启一下
上面说明 用CRT登虚拟机的时候需要经过主机这个跳板
3、登录验证方式
对于服务器的远程管理,除了用户账号的安全控制以外,登录验证的方式也非常重要。
sshd服务支持两种验证方式一密码验证、密钥对验证,可以设置只使用其中-“一种方式,
也可以两种方式都启用。
➢密码验证: 对服务器中本地系统用户的登录名称、密码进行验证。这种方式使用最为简
便,但从客户端角度来看,正在连接的服务器有可能被假冒:从服务器角度来看,当遭遇密码穷举(暴力破解)攻击时防御能力比较弱。
➢密钥对验证: 要求提供相匹配的密钥信息才能通过验证。通常先在客户端中创建一-对密钥
文件(公钥、私钥),然后将公钥文件放到服务器中的指定位置。远程登录时,系统将使用公钥、私钥进行加密/解密关联验证,大大增强了远程管理的安全性。该方式不易被假冒,且可以免交互登录,在Shell中被广泛使用。
当密码验证、密钥对验证都启用时,服务器将优先使用密钥对验证。对于安全性要求较高的
服务器,建议将密码验证方式禁用,只允许启用密钥对验证方式:若没有特殊要求,则两种方式都可启用。
[root@ns2 ~]# ls -a
anaconda-ks.cfg  .bash_history  .bash_logout  
.bash_profile  .bashrc  .cshrc  .ssh  .tcshrc             //没采用的时候没ssh
[root@localhost ~]# vi /etc/ssh/sshd_ config
PasswordAuthentication yes               //启用密码验证
PubkeyAuthentication yes                 //启用密钥对验证
AuthorizedKeysFile ssh/authorized_ keys     //指定公钥库文件
[root@localhost ~# systemctl restart sshd
其中,公钥库文件用来保存多个客户端上传的公钥文本,以便与客户端本地的私钥文件进行
匹配。
4、使用ssh客户端远程
[root@ns2 ~]# ssh root@20.0.0.66
root@20.0.0.66's password:
Last failed login: Thu Jul 30 04:56:43 EDT 2020 from 20.0.0.67 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Thu Jul 30 04:56:02 2020 from 20.0.0.1
[root@ns1 ~]# exit            //小退到自己的客户端
logout
Connection to 20.0.0.66 closed.
[root@ns2 ~]#
5、远程端口复制文件
[root@ns2 opt]# scp 1.txt root@20.0.0.66                    // 将本端的文件拷贝到对端(20.0.0.66)
[root@ns2 opt]# scp root@20.0.0.66:/opt/2.txt /opt/       //将对端(20.0.0.66)的文件拷贝到本端
6、sftp 安全FTP
通过sftp命令可以利用SSH安全连接与远程主机上传、下载文件, 采用了与FTP类似的登录过程和交互式环境,便于目录资源管理。例如,以下操作依次演示了sftp 登录、浏览、文件上传等过程。
[root@ns2 opt]# sftp root@20.0.0.66
root@20.0.0.66's password:
Connected to 20.0.0.66.
sftp> ls                        //   浏览
anaconda-ks.cfg     
sftp> touch 3.txt                   //在对端touch一个新文件
Invalid command.
sftp> put /opt/3.txt                      
stat /opt/3.txt: No such file or directory
sftp> put /opt/1.txt                         // 上传本端的文件给对端(一般在home目录)
Uploading /opt/1.txt to /root/1.txt
/opt/1.txt                                    100%   12    23.8KB/s   00:00    
sftp> ls
1.txt               anaconda-ks.cfg     
sftp> get anaconda-ks.cfg                      //下载文件
Fetching /root/anaconda-ks.cfg to anaconda-ks.cfg
/root/anaconda-ks.cfg                         100% 1550   117.4KB/s   00:00    
sftp> put /opt/1.txt /opt/                     // 指定上传到对端的opt目录下
Uploading /opt/1.txt to /opt/1.txt
/opt/1.txt                                    100%   12     9.7KB/s   00:00    
sftp> ls                                    
1.txt               anaconda-ks.cfg     
sftp> bye                                 //  退出
[root@ns2 opt]#
7、构建 密钥对验证的SSH体系
正如前面所提及的,密钥对验证方式可以为远程登录提供更好的安全性。下 面将介绍
在CentOS7.3服务器、客户端中构建密钥对验证SSH体系的基本过程。以RSA加密算法为例,整个过程包括四步,首先要在SSH客户端以zhangsan用户身份创建密钥对,并且要将创建的公钥文件上传至SSH服务器端,然后要将公钥信息导入服务器端的目标用户lisi 的公钥数据库,最后以服务器端用户lisi 的身份登录验证。
第一步:创建密钥对
第三步:导入公钥信息
私钥文件: id_ _rsa
数据库文件: ~/.ssh/authorized_ keys
公钥文件: id_ rsa pub
第二步:上传公钥文件id_ rsa pub
第四步:使用密钥对验证方式
SSH客户端
SSH服务器
[root@ns1 opt]# ssh-keygen -t rsa
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:
SHA256:q1yiq9boq0iN5EH42bYDdG+H35gXimv2sOgqPLwOsTc root@ns1
The key's randomart image is:                  // 下面是乱码公钥
+---[RSA 2048]----+
|                 |
|.                |
|... .            |
|.o + . .         |
|.o+ o + S .      |
|oo+o o + * .     |
|=+E+o + B o      |
|==o..=oB .       |
|+BB==+=..        |
+----[SHA256]-----+
[root@ns1 opt]# cd /root
[root@ns1 ~]# ls -a
.   1.txt            .bash_history  .bash_profile  .cshrc  .tcshrc
..  anaconda-ks.cfg  .bash_logout   .bashrc        .ssh
[root@ns1 ~]# cd .ssh/
[root@ns1 .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@ns1 .ssh]# ll -a
total 12
drwx------  2 root root   57 Jul 30 07:05 .
dr-xr-x---. 3 root root  160 Jul 30 06:50 ..
-rw-------  1 root root 1679 Jul 30 07:05 id_rsa           // 生成的秘钥文件
-rw-r--r--  1 root root  390 Jul 30 07:05 id_rsa.pub        //生成的秘钥文件
-rw-r--r--  1 root root  171 Jul 30 01:47 known_hosts
[root@ns1 .ssh]# ssh-copy-id root@20.0.0.67       //将公钥文件上传至服务器
下面是在服务器上查看有没有上传过来
[root@ns2 ~]# ll -a
drwx------   2 root root   48 Jul 30 07:09 .ssh
-rw-r--r--.  1 root root  129 Dec 28  2013 .tcshrc
[root@ns2 ~]# cd .ssh
[root@ns2 .ssh]# ls
authorized_keys  known_hosts               //确认已经传过来(authoruzed_keys)
8、TCP Wrappers配置实例
实际使用TCP Wrappers机制时,较宽松的策略可以是“允许所有,拒绝个别”,较严格
的策略是“允许个别,拒绝所有"。前者只需在hosts.deny文件中添加相应的拒绝策略就可以
了;后者则除了在hostsallow中添加允许策略之外,还需要在hosts.deny文件中设置
"ALL:ALL"的拒绝策略。
例如,若只希望从IP 地址为61.63.65.67的主机或者位于192.168.2.0/24 网段的主机
访问sshd服务,其他地址被拒绝,可以执行以下操作。
[root@localhost ~# vi /etc/hosts. alow
sshd:20.0.0.*                           // sshd是服务类型   后面是地址段或地址列表
[root@localhost ~# vi letc/hosts. deny
sshd:ALL