CentOS7 运维 - SSH远程访问及控制
一、SSH协议概述
它是一种安全通道协议,用于字符界面远程连接,可以对通信数据进行加密,有较高的安全性
系统安装时默认自带[最小化]
►SSH客户端:Putty、Xshell、SecureCRT
►SSH服务端:OpenSSH
二、OpenSSH
►服务名称:sshd
►服务端主程序:/usr/sbin/sshd
►服务端配置文件: /etc/ssh/sshd_config
►客户端配置文件:/etc/ssh/ssh_config
三、服务器配置
LoginGraceTime 2m
#登录验证时间为2分钟
PermitRootLogin yes
#允许以root登录
MaxAuthTries 6
#最大重试次数为6
MaxSessions 10
#同一地址的最大连接数
PermitEmptyPasswords no
#禁止空密码用户登录
UseDNS no
#禁用 DNS 反向解析[可提高服务器相应速度]
AllowUsers cat fox [email protected]
#不要将不存在的用户名加入其中
#多个用户用空格分割 以114.114.114.114的IP登录
DenyUsers cat
#DenyUsers权值大于AllowUsers
四、使用终端登录
ssh [email protected]
常用参数
-p
指定非默认端口号
ssh -p 110 [email protected]
五、SSH 服务支持的两种验证方式
①密码验证
根据用户名和密码进行验证,可能会被暴力破解
②密钥对验证
要求提供相匹配的密钥信息才能通过验证
扫描二维码关注公众号,回复:
12501704 查看本文章
密钥对优先级高于密码验证
PasswordAuthentication yes
#启用密码验证
PubkeyAuthentication yes
#启用密钥对验证
AuthorizedKeysFile .ssh/authorized_keys
#指定公钥库文件
默认登录密钥保存在 ~/.ssh/known_hosts
六、scp 远程复制
常用选项
-r
上传下载整个目录时使用-P
指定端口使用端口或者修改客户端配置文件
下行复制
scp [email protected]:/home/dog.sh /home
#将远程主机中的/home/dog.sh复制到本机的/home目录里
#源目录要使用绝对路径,相对路径会找不到文件[目的目录可以用相对]
上行复制
scp /home/cat.sh [email protected]:/home
#将本机/etc/ssh目录复制到远程主机
►客户机
►服务器
七、sftp 更安全的FTP
使用加密解密技术,有较好的安全性,但传输速率比普通FTP
要低
sftp root@IP地址
常用指令
ls
查看文件get
文件名put
文件名quit
退出
八、配置密钥对验证
①创建密钥对
ssh-keygen -t ecdsa
私钥默认位置 /home/fox/.ssh/id_ecdsa
可用的加密算法
- RSA
- ECDSA
- DSA
ls -l .ssh/id_ecdsa*
#密钥存放位置
id_ecdsa
#是私钥文件 权限默认值600
id_ecdsa.pub
#是公钥文件 用来提供给 SSH 服务器
②密钥对配对的方案一
ssh-copy-id -i id_ecdsa.pub [email protected]
►客户机
►服务器
③密钥对配对的方案二
►客户机
scp ~/.ssh/id_ecdsa.pub [email protected]:/opt
►服务器
cat /opt/id_ecdsa.pub >> .ssh/authorized_keys
cat /home/fox/.ssh/authorized_keys
④实现注入证书后无密码登录
►客户机
ssh-agent bash
ssh-add
ssh [email protected]
九、TCP Wrappers
将TCP
服务包裹起来,代为TCP
服务的端口,从而增加一个安全检测功能,外来必须通过安全检测才能访问服务程序,默认提供改功能
两种保护机制
- 使用
tcpd
对其他程序进行保护 - 由其他网络服务程序调用
libwrap.so.*
链接库 不需要允许tcpd
后者更有效率
►ldd查看libwrap.so.*
链接库
lld $(which ssh)
#含义为lld了which ssh的结果
TCP Wrappers 的访问策略
对应的两个策略文件 分别用来设置允许和拒绝的策略
/etc/hosts.allow
和 /etc/hosts.deny
格式:
<服务程序列表>:<客户端地址列表>
①服务程序列表
- ALL: 代表所有的服务器
- 单个服务程序:如"vsftpd"
- 多个服务程序组成的列表:如"vsftpd,sshd"
②客户端地址列表
- ALL: 代表所有客户端地址
- LOCAL:道标本机地址
- 多个地址以逗号分隔
- 允许使用? * 前者代表一个字符 后者代表任意长度
网段地址 192.168.1. or 192.168.1.0/255.255.255.0
区域地址 .baidu.com
TCP Wrappers 机制的基本原则
►首先检查/etc/hosts.allow
文件,如果找到相匹配的策略则允许访问
►继续检查/etc/hosts.deny
文件,如果找到相匹配的策略则拒绝访问
①允许策略
②拒绝策略
排错思路
① ssh_exchange_identification: read: Connection reset by peer
添加选项 -v
查看连接详细信息
ssh -v [email protected]
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.222.150 [192.168.222.150] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
ssh_exchange_identification: read: Connection reset by peer
查看服务器的白名单和黑名单
vim /etc/hosts.deny
vim /etc/hosts.allow
将其注释掉即可