sshd服务及服务管理命令

一、sshd服务

1.sshd简介
sshd=secure shell
可以通过网络在主机中开机shell的服务

修改服务端的名称,客户端的名称不变:

将其改为:server

修改两台虚拟机的ip

将客户端的ip修改为133,服务端的ip修改为233

 

客户端软件
 

客户端软件

          sshd

连接方式:
        ssh     [email protected]      ##文本模式的连接,连接成功后不能打开图形

 

  ssh -X  [email protected]      ##可以在连接成功后开启图形


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

远程复制:

             scp file [email protected]:dir                  ##上传

             scp [email protected]:file dir                   ##下载

2.sshd的key认证
1)生成认证KEY
[[email protected] ~]#ssh-keygen                                                   ##生成密钥的命令
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:                       
13:34:9d:2e:1c:9f:92:6b:5b:30:f6:d1:e0:60:dc:01 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|       .E+.o     |
|       .=.=      |
|       o.B +     |
|        O.* .    |
|       .SB .     |
|        o.o      |
|       . o       |
|        .        |
|                 |
+-----------------+


2)加密sshd服务
[[email protected] .ssh]# ssh-copy-id -i id_rsa.pub [email protected]                           ##加密sshd服务
The authenticity of host '172.25.254.133 (172.25.254.133)' 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
roo[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] .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
    ^
 此文件出现表示加密完成

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


4)测试
在客户主机中(172.25.254.133)
ssh [email protected]                               ##连接时发现直接登陆不需要root登陆系统的密码认证


3.sshd的安全设定
78  PasswordAuthentication yes|no  ##是否允许用户通过登陆系统的密码做sshd的认证

48  PermitRootLogin yes|no         ##是否允许root用户通过sshd服务的认证


52  Allowusers student westos      ##设定用户白名单,白名单出现默认不在名单中的用户不能使用sshd

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

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


5.用户的登陆审计
1.w                ##查看正在使用当前系统的用户


       -f          ##查看使用来源


       -i          ##查看IP


       /var/run/utmp


2.last                   ##查看使用过并退出的用户信息


       /var/log/wtmp
3.lastb                   ##试图登陆但没成功的用户

       /var/log/btmp