sshd简介
通过网络在主机中开启shell——sshd=secure shell;
有两种连接方式
- 文本连接——ssh username@ip
- 图形连接——ssh -X username@ip
##查看/root/下的隐藏文件,没有.ssh文件
[root@westos_client Desktop]# ls -a /root/
. .bashrc Documents .local Templates
… .cache Downloads .mozilla Videos
anaconda-ks.cfg .config .esd_auth Music .viminfo
.bash_history .cshrc .gnome2 Pictures .Xauthority
.bash_logout .dbus .gnome2_private Public
.bash_profile Desktop .ICEauthority .tcshrc
连接ip为108的主机,第一次建立连接时询问
[root@westos_client Desktop]# ssh -X root@172.25.254.108
The authenticity of host ‘172.25.254.108 (172.25.254.108)’ 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
Warning: Permanently added ‘172.25.254.108’ (ECDSA) to the list of known hosts.
root@172.25.254.108’s password:
Last login: Thu Jul 18 01:37:43 2019 from 172.25.254.208
##每次切换后要退出登陆
[root@westos_server ~]# exit
logout
Connection to 172.25.254.108 closed.
##查看/root/的文件,发现出现.ssh文件
[root@westos_client Desktop]# ls -a /root/
. .bashrc Documents .local .tcshrc
… .cache Downloads .mozilla Templates
anaconda-ks.cfg .config .esd_auth Music Videos
.bash_history .cshrc .gnome2 Pictures .viminfo
.bash_logout .dbus .gnome2_private Public .Xauthority
.bash_profile Desktop .ICEauthority .ssh
[root@westos_client Desktop]# ssh -X root@172.25.254.108
root@172.25.254.108’s password:
Last login: Thu Jul 18 01:40:33 2019 from 172.25.254.208
注意:1.第一次连接陌生主机时需要建立认证文件,会询问是否建立,输入yes或no,不可输入y或n;
2.在第二次再次连接该主机时不需要再次输入,因为生成~/.ssh/know_hosts文件
远程复制
- 上传——scp file root@ip:dir
- 下载——scp root@ip:file dir
##将file上传到ip为8的主机中
[root@westos_client Desktop]# touch file
##进行上传操作
[root@westos_client Desktop]# scp ‘/root/Desktop/file’ kiosk@172.25.254.8:/home/kiosk/Desktop
The authenticity of host ‘172.25.254.8 (172.25.254.8)’ can’t be established.
ECDSA key fingerprint is 9f:0c:e1:99:89:ed:dc:81:b6:92:c2:6c:98:f8:3e:d3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.25.254.8’ (ECDSA) to the list of known hosts.
kiosk@172.25.254.8’s password:
file 100% 1457 1.4KB/s 00:00
##将ip为108的主机上/mnt/cai文件下载到当前桌面
[root@westos_client Desktop]# scp root@172.25.254.108:/mnt/cai .
root@172.25.254.108’s password:
scp: /mnt/cai: not a regular file
[root@westos_client Desktop]# scp -r root@172.25.254.108:/mnt/cai .
root@172.25.254.108’s password:
file1 100% 0 0.0KB/s 00:00
file2 100% 0 0.0KB/s 00:00
file3 100% 0 0.0KB/s 00:00
file4 100% 0 0.0KB/s 00:00
file5 100% 0 0.0KB/s 00:00
file6 100% 0 0.0KB/s 00:00
file7 100% 0 0.0KB/s 00:00
file8 100% 0 0.0KB/s 00:00
file9 100% 0 0.0KB/s 00:00
file10
注意:1.在上传或下载目录时需要添加-r
sshd的key认证
##生成认证key——ssh-keygen
##生成密钥
[root@westos_server Desktop]# 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:
e3:8f:d6:a4:d1:4c:ab:11:ae:3e:d4:ce:8a:47:12:d3 root@westos_server.westos.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . |
| o E . . |
| o oS= . |
| . o.=.= |
| + +.B |
| .+ *o. |
| .oo+. . |
+-----------------+
##加密服务——ssh-copy-id -i 公钥地址 username@ip
##查看.ssh文件,里面没有authorized_keys文件
[root@westos_server Desktop]# ls /root/.ssh/
id_rsa id_rsa.pub
##对服务器进行加,服务器的ip为172.25.254.108
[root@westos_server Desktop]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.108
The authenticity of host '172.25.254.108 (172.25.254.108)' 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
root@172.25.254.108's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@172.25.254.108'"
and check to make sure that only the key(s) you wanted were added.
##再次查看/root/.ssh/文件,出现authorized_keys 证明加密成功
[root@westos_server Desktop]# ls /root/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
##分发钥匙——向你要连接的客户发放私钥
##将私匙发送给ip为108的客户,注意108客户在/root/中必须要有.shh文件
[root@westos_server Desktop]# scp /root/.ssh/id_rsa root@172.25.254.208:/root/.ssh/
The authenticity of host '172.25.254.208 (172.25.254.208)' 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
Warning: Permanently added '172.25.254.208' (ECDSA) to the list of known hosts.
root@172.25.254.208's password:
id_rsa 100% 1679 1.6KB/s 00:00
##测试——在客户端进行测试
在客户端远程进入ip为108的服务器
[root@westos_client Desktop]# ssh root@172.25.254.108
Last login: Thu Jul 18 02:41:36 2019 from 172.25.254.208
sshd的安全认证——/etc/ssh/sshd_config
在/etc/ssh/sshd_config文件中,我们可以通过修改和添加里面的内容来改变sshd的安全认证
##客户端是否可以通过登陆系统密码做sshd的认证
在/etc/ssh/sshd_config文件中找到下面一行,大概在78行左右;后面为yes时,表示可以通过sshd的安全认证,no则不可以通过sshd的安全认证
PasswordAuthentication yes /no
先将服务器中的PasswordAuthentication修改为yes,在客户端可以登陆成功;先将服务器中的PasswordAuthentication修改为no,在客户端登陆失败
在修改完/etc/ssh/sshd_config文件后一定要将服务重启一次,不然不能生效;
使用systemctl restart sshd.serve进行服务重启
##客户端是否允许root用户通过sshd服务认证
在/etc/ssh/sshd_config文件48行左右,后面为为yes时表示允许客户以root身份登陆,否则表示不可以
#PermitRootLogin yes/no
在修改完/etc/ssh/sshd_config文件后一定要将服务重启一次,不然不能生效;
使用systemctl restart sshd.serve进行服务重启
##设定用户白名单
在/etc/ssh/sshd_config文件中添加以下代码,白名单表示只有名单内的用户才可以使用sshd
AllowUsers username
在修改完/etc/ssh/sshd_config文件后一定要将服务重启一次,不然不能生效;
使用systemctl restart sshd.serve进行服务重启
##设定用户黑名单
在/etc/ssh/sshd_config文件中添加以下代码,黑名单表示在名单内的用户不可以使用sshd
Denyusers username
在修改完/etc/ssh/sshd_config文件后一定要将服务重启一次,不然不能生效;
使用systemctl restart sshd.serve进行服务重启
在文件中以#开头的文件时不会被系统读取的;黑名单与白名单相比白名单限制更大,安全性更高
添加sshd登陆信息——/etc/motd
修改/etc/motd文件就可以在使用sshd登陆时出现登陆信息或者你想说的话;在/etc/motd文件中写什么在使用shhd’登陆过程中就会出现什么
用户的登陆审计
名称 | 方法 |
---|---|
查看正在使用当前系统的用户 | w |
查看使用来源 | w -f |
显示ip | w -i |
查看使用过并退出的用户信息 | last |
试图登陆但没成功的用户 | lastb |
用户的登陆审计都存放在数据中。
查看正在使用当前系统的用户,查看使用来源,显示ip存放在/var/run/utmp数据中
查看使用过退出的用户信息存放在/var/log/wtmp数据中
试图登陆但没成功的用户信息存放在/var/log/btmp数据中