ssh: secure shell, protocol, 22/tcp, 安全的远程登录,实现加密通信,代替传统的 telnet 协议
具体的软件实现:
SSH 协议版本:
📑13.1.1 公钥交换原理
OpenSSH是SSH (Secure SHell) 协议的免费开源实现,一般在各种Linux版本中会默认安装,基于C/S结构
Openssh软件相关包:
服务器:/usr/sbin/sshd
Unit 文件:/usr/lib/systemd/system/sshd.service
客户端:
ssh命令是ssh客户端,允许实现对远程系统经验证地加密安全访问
当用户远程连接ssh服务器时,会复制ssh服务器/etc/ssh/ssh_host*key.pub文件中的公钥到客户机的
~/.ssh/know_hosts中。下次连接时,会自动匹配相应私钥,不能匹配,将拒绝连接
格式:
ssh [user@]host [COMMAND]
ssh [-l user] host [COMMAND]
常见选项
-p port:远程服务器监听的端口
-b 指定连接的源IP
-v 调试模式
-C 压缩方式
-X 支持x11转发
-t 强制伪tty分配,如:ssh -t remoteserver1 ssh -t remoteserver2 ssh
remoteserver3
-o option 如:-o StrictHostKeyChecking=no
-i <file> 指定私钥文件路径,实现基于key验证,默认使用文件: ~/.ssh/id_dsa,
~/.ssh/id_ecdsa, ~/.ssh/id_ed25519,~/.ssh/id_rsa等
范例:远程执行命令
[root@servera .ssh]# ssh 192.168.137.129 ls -l
root@192.168.137.129's password:
total 4
-rw-------. 1 root root 1624 Apr 2 13:51 anaconda-ks.cfg
🔖ssh登录验证方式介绍
ssh服务登录的常用验证方式
基于用户和口令登录验证
基于密钥的登录方式
🔖实现基于密钥的登录方式
在客户端生成密钥对
ssh-keygen -t rsa [-P 'password'] [-f “~/.ssh/id_rsa"]
把公钥文件传输至远程服务器对应用户的家目录
ssh-copy-id [-i [identity_file]] [user@]host
重设私钥口令:
ssh-keygen -p
服务器端:sshd
服务器端的配置文件: /etc/ssh/sshd_config
服务器端的配置文件帮助:man 5 sshd_config
常用参数:
Port #更改端口先关selinux,setenforce 0
ListenAddress ip
LoginGraceTime 2m
PermitRootLogin yes #默认ubuntu不允许root远程ssh登录
StrictModes yes #检查.ssh/文件的所有者,权限等
MaxAuthTries 6
MaxSessions 10 #同一个连接最大会话
PubkeyAuthentication yes #基于key验证
PermitEmptyPasswords no #空密码连接
PasswordAuthentication yes #基于用户名和密码连接
GatewayPorts no
ClientAliveInterval 10 #单位:秒
ClientAliveCountMax 3 #默认3
UseDNS yes #提高速度可改为no
GSSAPIAuthentication yes #提高速度可改为no
MaxStartups #未认证连接最大值,默认值10
Banner /path/file
#以下可以限制可登录用户的办法:
AllowUsers user1 user2 user3
DenyUsers
AllowGroups
DenyGroups
范例:设置ssh 空闲60s 自动注销
Vim /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 0
Service sshd restart
#注意:新开一个连接才有效
范例:解决ssh登录缓慢的问题
vim /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
systemctl restart sshd
范例:启用/禁用root 远程ssh登录
#修改sshd服务配置文件
vim /etc/ssh/sshd_config
#PermitRootLogin prohibit-password 注释掉此行
PermitRootLogin yes/no 修改为下面形式(yes启用/no禁用)
systemctl restart sshd
ssh服务的最佳实践
建议使用非默认端口
禁止使用protocol version 1
限制可登录用户
设定空闲会话超时时长
利用防火墙设置ssh访问策略
仅监听特定的IP地址
基于口令认证时,使用强密码策略,比如:tr -dc A-Za-z0-9_ < /dev/urandom | head -c 12|xargs
使用基于密钥的认证
禁止使用空密码
禁止root用户直接登录
限制ssh的访问频度和并发在线数
经常分析日志
阅读量:1289
点赞量:0
收藏量:0