所有实验在centos7下完成,使用xshell6远程连接工具
SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录和远程复制的等功能。SSH协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令。
大多数企业服务器是通过远程登录的方式来进行管理的当需要从一个工作站管理数以百计的服务器主机时,远程维护的方式将更占优势。
SSH协议为客户机提供安全的shell环境,用于远程管理,默认端口:TCP 22
[root@client ~]# ssh [email protected]
[email protected]'s password:
Last login: Thu Jul 9 16:48:46 2020 from 20.0.0.1
[root@server ~]#
[root@client ~]# scp [email protected]:/etc/passwd /opt/passwd.31
[email protected]'s password:
passwd 100% 2332 1.5MB/s 00:00
[root@client ~]#
复制本地文件到远程
[root@client opt]# scp /etc/passwd [email protected]:/opt/passwd.41
[email protected]'s password:
passwd 100% 2331 1.8MB/s 00:00
[root@client opt]#
[root@client ~]# sftp [email protected]
[email protected]'s password:
Connected to 20.0.0.31.
sftp> ls
anaconda-ks.cfg initial-setup-ks.cfg 下载 公共 图片
文档 桌面 模板 视频 音乐
sftp> put /opt/passwd.31 ##上传
Uploading /opt/passwd.31 to /root/passwd.31
/opt/passwd.31 100% 2332 1.8MB/s 00:00
sftp> ls ##查看
anaconda-ks.cfg initial-setup-ks.cfg passwd.31 下载 公共
图片 文档 桌面 模板 视频
音乐
sftp> get passwd.31 /mnt ##下载
Fetching /root/passwd.31 to /mnt/passwd.31
/root/passwd.31 100% 2332 2.2MB/s 00:00
sftp> ls
anaconda-ks.cfg initial-setup-ks.cfg passwd.31 下载 公共
图片 文档 桌面 模板 视频
音乐
sftp>
服务器配置文件在/etc/ssh/目录下的sshd_config
[root@localhost ~]# vim /etc/ssh/sshd_config
...
#Port 22 ##端口号可以修改,不可与其他端口冲突,一般不做修改
#AddressFamily any
#ListenAddress 0.0.0.0 ##监听地址可修改
#ListenAddress ::
...
#UseDNS no ##禁用反向解析
[root@localhost ~]# vim /etc/ssh/sshd_config
...
#LoginGraceTime 2m ##登录验证超时时间,验证超时则断开连接
#PermitRootLogin no ##禁止root用户远程登录
#StrictModes yes
#MaxAuthTries 6 ##最大登录尝试次数
#MaxSessions 10 ##最多允许远程的终端数
...
#PermitEmptyPasswords no ##不允许空密码登录
..
AllowUsers lisi [email protected] ##白名单,需要自己添加,允许lisi从任何终端登录,只允许admin从192.168.20.30登录
实验环境
两台centos7,一台为服务器,一台为客户端,在客户端上以root身份登录服务器
以下操作会在服务器和客户端之间切换,注意看主机名称server和client
[root@localhost ~]# hostnamectl set-hostname server ##服务器端改名为server,便于辨认
[root@localhost ~]# su
[root@server ~]# rpm -q openssh ##查看ssh软件包
openssh-7.4p1-16.el7.x86_64
[root@server ~]# grep "bash$" /etc/passwd ##查看本地可登录的用户
root:x:0:0:root:/root:/bin/bash
z:x:1000:1000:Z:/home/z:/bin/bash
[root@server ~]#
远程登录和传输文件操作
[root@localhost ~]# hostnamectl set-hostname client ##客户端改名
[root@localhost ~]# su
[root@client ~]# rpm -q openssh
openssh-7.4p1-16.el7.x86_64
[root@client ~]# ssh [email protected] ##远程登录服务器
The authenticity of host '20.0.0.31 (20.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:tuIxsEYM3jES8aAuwOzK4AjZhZFrbFvSK5I06KRFw+o.
ECDSA key fingerprint is MD5:c7:27:cf:8f:c3:44:ac:fc:3b:26:75:9f:f0:85:15:f6.
Are you sure you want to continue connecting (yes/no)? yes ##yes
Warning: Permanently added '20.0.0.31' (ECDSA) to the list of known hosts.
[email protected]'s password: ##输入服务器root密码
Last login: Thu Jul 9 08:53:29 2020
[root@server ~]# ##已登上服务器
修改服务器ssh配置文件,禁止root用户登录
[root@server ~]# vim /etc/ssh/sshd_config
...
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no ## 禁止root用户远程登录
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
....
保存退出
[root@server ~]# service sshd reload ##重载配置
Redirecting to /bin/systemctl reload sshd.service
去客户端尝试登录root
[root@client ~]# ssh [email protected]
[email protected]'s password:
Permission denied, please try again. ##已经不能用root身份登了
虽不能用root用户直接登录,但可以登录其他用户后使用su来切换身份,可以使用PAM认证模块来限制用户使用su命令切换身份。具体操作可见博客系统安全su和PAM 认证模块
验证尝试登录次数
系统中默认登录尝试次数为3,所以配置文件中MaxAuthTries 6字段,失去了它所存在的意义,我们提升尝试次数,来验证这个配置字段的作用。
[root@server ~]# ssh [email protected]
The authenticity of host '20.0.0.31 (20.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:tuIxsEYM3jES8aAuwOzK4AjZhZFrbFvSK5I06KRFw+o.
ECDSA key fingerprint is MD5:c7:27:cf:8f:c3:44:ac:fc:3b:26:75:9f:f0:85:15:f6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '20.0.0.31' (ECDSA) to the list of known hosts.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password: ##尝试三次后退出
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@server ~]#
[root@server ~]# ssh -o NumberOFPasswordPrompts=8 [email protected] ##可将尝试次数提升至8次,但受ssh配置文件MaxAuthTries 6限制,只能输6次
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Received disconnect from 20.0.0.31 port 22:2: Too many authentication failures
Authentication failed.
[root@server ~]#
在配文件中自己添加AllowUsers或DenyUsers
[root@server ~]# vi /etc/ssh/sshd_config
#Authentication:
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
MaxAuthTries 6
#MaxSessions 10
AllowUsers z [email protected] ##允许用户z在任何终端登录,用户tom在20.0.0.61终端登录
tom在20.0.0.41上不可登录
[root@client ~]# ssh [email protected]
[email protected]'s password:
Permission denied, please try again.
配置文件sshd_config中的秘钥对验证字段
[root@server ~]# vi /etc/ssh/sshd_config
...
PasswordAuthentication yes ##启用密码验证
PubkeyAuthentication yes ##启用秘钥验证
AuthorizedKeysFile .ssh/authorized_keys ##指定公钥库
...
客户端创建秘钥对
[root@client ~]# su - lisi ##创建用户lisi,并切换
[lisi@client ~]$ ssh-keygen -t ecdsa ##生成秘钥对,ecdsa为加密算法
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/lisi/.ssh/id_ecdsa): ##指定秘钥保存位置
Created directory '/home/lisi/.ssh'.
Enter passphrase (empty for no passphrase): ##输入验证密码
Enter same passphrase again: ##再次输入
Your identification has been saved in /home/lisi/.ssh/id_ecdsa.
Your public key has been saved in /home/lisi/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:0vm3eayEBcyt3YCfuxboCtbgFFCvPvn7Iwohb5WnxsI lisi@client
The key's randomart image is:
+---[ECDSA 256]---+
| ... |
| . . o o |
| . . = o |
| = . = + |
| . . B S ..= . |
| + B B ..o.. |
| E @ ..o +o |
| . = +. oo.+o |
| ..+=.o=o |
+----[SHA256]-----+
[lisi@client ~]$ ls -a
. .. .bash_logout .bash_profile .bashrc .cache .config .mozilla .ssh
[lisi@client ~]$ cd .ssh
[lisi@client .ssh]$ ls
id_ecdsa id_ecdsa.pub ##id_ecdsa为私钥,id_ecdsa.pub为公钥
[lisi@client .ssh]$
上传秘钥
[lisi@client .ssh]$ ssh-copy-id -i ~/.ssh/id_ecdsa.pub [email protected] ##上传公钥
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/lisi/.ssh/id_ecdsa.pub"
The authenticity of host '20.0.0.31 (20.0.0.31)' can't be established.
ECDSA key fingerprint is SHA256:tuIxsEYM3jES8aAuwOzK4AjZhZFrbFvSK5I06KRFw+o.
ECDSA key fingerprint is MD5:c7:27:cf:8f:c3:44:ac:fc:3b:26:75:9f:f0:85:15:f6.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/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.
登录验证
当我们以本地lisi身份登录远程用户tom时,启用秘钥对验证
[lisi@client .ssh]$ ssh [email protected]
Enter passphrase for key '/home/lisi/.ssh/id_ecdsa': ##密码为设置,生成秘钥对时设置的密码
Last failed login: Thu Jul 9 12:14:36 CST 2020 from 20.0.0.41 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Jul 9 10:54:00 2020 from server
[tom@server ~]$
TCP Wrappers将其他的TCP服务程序“包裹起来”,增加了一个安全检测过程,外来的连接请求必须先通过这层安全检测,获得许可后才可以访问真正的服务程序。
策略文件
TCP Wrapper机制的保护对象为各种网络服务程序,针对访问服务的客户机的客户机地址进行访问控制。对应两个策略配置文件/etc/hosts.allow和/etc/hosts.deny,分别用来设置允许和拒绝的策略。
两个文件的配置格式相同为:<服务程序列表>:<客户机地址列表>
访问控制原则为:首先检查/etc/hosts.allow文件,如果匹配到策略,则允许访问,否则继续检查/etc/hosts.deny文件,如果找到相匹配的策略,则拒绝访问;如果都匹配不到,则允许访问。
仅允许20.0.0.41访问sshd服务,禁止其他所有地址访问受保护的服务
[root@server ~]# vi /etc/hosts.allow
...
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
sshd:20.0.0.41
[root@server ~]# vi /etc/hosts.deny
# The rules in this file can also be set up in
# /etc/hosts.allow with a 'deny' option instead.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
sshd:ALL
'优先读取allow,然后再读取deny'
'如果做黑名单,name白名单就不用写'
到20.0.0.41和20.0.0.61去使用ssh
[root@client ~]# ssh [email protected]
[email protected]'s password:
Last login: Thu Jul 9 18:58:28 2020 from 20.0.0.1
[root@server ~]#
[root@localhost ~]# ssh [email protected]
ssh_exchange_identification: read: Connection reset by peer
[root@localhost ~]#
做实验测试的时候,要是搞得自己不能远程连接了,去虚拟机里把配置文件/etc/ssh/sshd_config里面添加的不允许root用户远程登录改成允许和自己添加的白名单和黑名单删掉。