sshd服务及服务管理

一、sshd简介

1.ssh协议

      SSH 为 Secure Shell 的缩写 , 是应用层的安全协议。 SSH是目前较可靠 , 专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题

2.连接方式

    ssh      [email protected]          ##文本模式的链接
    ssh   -X     [email protected]    ##可以在链接成功后开机图形

    注意:
        第一次链接陌生主机是要建立认证文件, 所以会询问是否建立,需要树入yes
        在次链接此台主机时,因为已经生成~/.ssh/know_hosts文件所以不需要再次输入yes

远程复制:
    scp    file       [email protected]:dir                 ##上传
    scp  [email protected]:file       dir        ##下载

3.设定ip

  sshd服务及服务管理

    sshd服务及服务管理

如果设客户端ip为172.25.254.100,服务端ip为172.25.254.200

4.查看本台虚拟机的ip

    sshd服务及服务管理

二、sshd的key认证

1.生成锁和钥匙
[[email protected] Desktop]# ssh-******            ##生成**的命令
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:
aa:1c:25:78:7d:40:57:64:7e:5f:17:da:45:03:34:e6 [email protected]
The key's randomart image is:

+--[ RSA 2048]----+
|      o   .      |
|     o o o o     |
|      + * . =    |
|     . = = . =   |
|      . S . o .  |
|       o ..  .E  |
|          .+ .   |
|          ..o    |
|            ..   |
+-----------------+

2.加密ssh用户的认证
在服务端
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
                           ssh-copy-id                            ##加密命令
                              -i                                          ##指定**
                          /root/.ssh/id_rsa.pub            ##**

                          root                                      ##加密用户
 

                            172.25.254.200        ##主机ip

[[email protected] .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]   ##加密sshd服务
The authenticity of host '172.25.254.200 (172.25.254.200)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
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:

Number of key(s) added: 1

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

[[email protected] Desktop]# ls -l /root/.ssh             ##查看/root/.ssh中有没有生成**

total 16
-rw-------. 1 root root  396 4月  12 05:21 authorized_keys(此文件出现表示加密完成)
-rw-------. 1 root root 1679 4月  12 05:20 id_rsa(钥匙)
-rw-r--r--. 1 root root  396 4月  12 05:20 id_rsa.pub(锁)
-rw-r--r--. 1 root root  176 4月  12 05:20 known_hosts(历史记录)

3.分发钥匙
scp /root/.ssh/id_rsa [email protected]:/root/.ssh/

[[email protected] Desktop]# scp /root/.ssh/id_rsa [email protected]:/root/.ssh
[email protected]'s password:
id_rsa                                          100% 1679     1.6KB/s   00:00 
4.测试
在客户主机中(172.25.254.100)
ssh [email protected]        ##连接时发现直接登陆不需要root登陆系统的密码认证

sshd服务及服务管理

5.在服务端删除加密的文件

rm -fr /root/.ssh/authorized_keys    ##当此文件被删除,客户端解密文件失效

[[email protected] Desktop]# rm -fr /root/.ssh/authorized_keys
[[email protected] Desktop]# systemctl restart sshd.service    ##一定要刷新服务器
[[email protected] Desktop]# ll /root/.ssh
total 12
-rw-------. 1 root root 1679 4月  12 05:20 id_rsa
-rw-r--r--. 1 root root  396 4月  12 05:20 id_rsa.pub

-rw-r--r--. 1 root root  352 4月  12 05:24 known_hosts

此时,在客户端连接需要密码

sshd服务及服务管理

cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys    ##从新生成锁文件,解密文件功能恢复

三、sshd的安全配置

vim /etc/ssh/sshd_config              ##更改
systemctl restart sshd.service     ##刷新
1.禁止原始认证方式

78 PasswordAuthentication no|yes    ##开启或关闭ssh的默认认证方式

-sshd服务及服务管理

sshd服务及服务管理

sshd服务及服务管理

客户端,在服务端关闭ssh的默认认证方式后,任何用户都不能登录服务端。

48 PermitRootLogin yes|no     ##是否允许root用户通过sshd服务的认证
52 Allowusers student westos     ##设定用户白名单,白名单出现默认不在名单中的用户不能使用sshd

    sshd服务及服务管理

    sshd服务及服务管理
53 Denyusers    westos         ##设定用户黑名单,黑名单出现默认不在名单中的用户可以使用sshd

     sshd服务及服务管理

     sshd服务及服务管理

     注意:1.在做这个黑白名单的实验时,黑白名单不能同时出现

                2.改完文件一定一定!要用“systemctl  restart  ssh.service”刷新文件

                 3.登录完一个用户要先退出才能进行下一个用户的登录

四、添加sshd登陆登陆信息
vim /etc/motd                        ##文件内容就是登陆后显示的信息

sshd服务及服务管理

五、用户的登陆审计

1.w                   ##查看正在使用当前系统的用户
         -f    ##查看使用来源
         -i    ##显示IP
         /var/run/utmp    
2.last                ##查看使用过并退出的用户信息
          /var/log/wtmp
3.lastb                   ##试图登陆但没成功的用户
             /var/log/btmp