Linux安装sshd
sshd命令 是openssh软件套件中的服务器守护进程
。
# ubuntu/debian安装ssh
sudo apt install ssh openssh-server
# Centos安装ssh
sudo yum install openssh-server -y
# 启动sshd并设置sshd开机自启
sudo systemctl enable --now sshd
# 单独安装ssh-client
yum install openssh-clients -y
apt install openssh-client -y
dnf install openssh-clients -y
openssh套件
提供交互算法
使用的素数模数
(/etc/ssh/moduli
),生成主机认证公私密钥
的工具(/usr/bin/ssh- keygen
)
调整SELinux和防火墙策略
请勿照搬
# 永久关闭SELinux
# 修改SELinux的配置为disabled
vi /etc/selinux/config
## 将SELINUX=enforcing改为SELINUX=disabled
# 关闭SELinux
setenforce 0
# 清空防火墙策略(非必须)
iptables -F
# 启动sshd并设置sshd开机自启
sudo systemctl enable --now sshd
# 查看ssh服务是否启动
ps -ef|findstr sshd
## 需要依赖grep
ps -ef|grep sshd
# A和B分别测试下可用性
ssh localhost
Windows安装sshd服务
Win32-openssh
项目:https://github.com/PowerShell/Win32-OpenSSH
手动安装openssh
参考:https://github.com/Dream4ever/Knowledge-Base/issues/84
先去Releases页面下载最新版本的OpenSSH,注意要下载64位版本的,并将压缩包里的文件解压至C:\Program Files\OpenSSH目录下
# 执行安装脚本
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
# windows防火墙放行22端口
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
# 启动sshd服务
net start sshd
# 设置sshd服务自动启动
Set-Service sshd -StartupType Automatic
choco安装openssh
安装openssh套件
和ssh-copy-id
# 安装openssh和ssh-copy-id
choco install openssh ssh-copy-id -y
启动sshd服务
# 进入sshd目录
cd "C:\Program Files\OpenSSH-Win64"
# 执行install-sshd.ps1脚本
.\install-sshd.ps1
# 重启sshd服务
Restart-Service sshd
# 重启ssh-agent服务(ssh认证服务)
Restart-Service ssh-agent
# 将sshd服务设置开机自启动
Set-Service -Name sshd -StartupType Automatic
# 将ssh-agent服务设置开机自启动
Set-Service -Name ssh-agent -StartupType Automatic
# 查看sshd服务
Get-Service sshd
# 查看ssh认证服务
Get-Service ssh-agent
查看22端口是否处于监听状态
# 查看22端口是否处于监听状态
netstat -ano|findstr 22
netstat -ano|grep 22
# telnet测试本地22端口是开放
telnet 127.0.0.1 22
配置Win10的sshd服务–(必须
配置)
sshd
的配置文件在C:\ProgramData\ssh\sshd_config
vim C:\ProgramData\ssh\sshd_config
# 允许密钥登录
PubkeyAuthentication yes
# 指定授权密钥文件
# AuthorizedKeysFile .ssh/authorized_keys
# 密码认证(需要将默认的yes改为no,才能密钥登录)
PasswordAuthentication no
必须要注释
文件最后
几行
#Match Group administrators
#AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
修改完配置一定要重启
sshd服务
# 重启sshd服务
Restart-Service sshd
# 重启ssh认证服务
Restart-Service ssh-agent
设置ssh登录后默认的shell
设置ssh登录
后的shell
的主目录
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShellCommandOption -Value "/c" -PropertyType String -Force
设置ssh登录
后的shell
为powershell
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
设置ssh登录
后的shell
为pwsh7
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\PowerShell\7\pwsh.exe" -PropertyType String -Force
设置ssh登录
后的shell
为msys
的bash
# 先安装cmder--必须
choco install cmder -y
# 设置shell
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\tools\msys64\usr\bin\bash.exe" -PropertyType String -Force
配置ssh密钥免密登录
https://blog.youkuaiyun.com/omaidb/article/details/120028143
# 生成密钥对
ssh-keygen
# 将私钥加载到ssh-agent
ssh-add ~\.ssh\id_rsa
sshd服务器创建authorized_keys文件
Widnows的sshd服务默认没有authorized_keys
文件
# 上传私钥到ssh服务器
scp ~\.ssh\id_rsa.pub username@windows服务器ip:~\.ssh\authorized_keys
ssh-copy-id上传公钥到ssh服务器
ssh-copy-id
没成功,报错umask
问题
# 配置ssh免密登录
ssh-copy-id username@hostip
# ssh登录Windows-sshd服务器
ssh username@windows服务器ip
sshd服务详解
sshd
命令 是openssh软件套件中的服务器守护进程。
sshd配置文件
/etc/ssh/ssh_config
是客户端配置
文件
/etc/ssh/sshd_config
是服务端配置
文件
# 编辑sshd配置文件
vim /etc/ssh/sshd_config
只监听IPV4
# 查看AddressFamily配置
grep AddressFamily /etc/ssh/sshd_config
# 只监听IPv4
AddressFamily inet
指定ssh协议版本
https://developer.aliyun.com/article/269860
# 指定ssh协议版本为2
Protocol 2
自定义sshd端口
vim /etc/ssh/sshd_config
# 取消sshd_config第17行第注释
Port 3389 # 将22修改为3389
修改完配置后重启sshd
服务
# 修改完ssh_config配置,重启sshd服务后配置才会生效
systemctl restart sshd
sshd安全配置
禁止使用密码登陆ssh
# 大约65行 或 78行
## yes打开密钥认证 | no 关闭密钥认证
PasswordAuthentication yes
## yes开启密码认证 | no 关闭密钥认证
PubkeyAuthentication no
##输入密码次数限制 6代表输错六次就退出
MaxAuthTries 6
修改完配置后重启sshd
服务
# 修改完ssh_config配置,重启sshd服务后配置才会生效
systemctl restart sshd
不允许root登录
默认为prohibit-password(没有密码)
。建议配置为no
,即禁止root远程登录
。
如果未禁止root
账号远程登录,那么攻击者获取到root口令之后就可以从网络上远程登录服务器进行攻击行为,否则只能从服务器机房内部近端登录,可大幅减小攻击面
。
# 是否允许root登录
## no 禁止root登录
PermitRootLogin no
不允许空密码
# 允许空密码,默认为no。
## 应禁止配置该字段为yes,避免无密码用户登录。
PermitEmptyPasswords no
使用PAM策略
# 使用PAM策略,默认为no。
## PAM策略对用户认证和管理,有登录次数限制,超次锁定用户,到时解锁用户功能。
## sshd_config的MaxAuthTries仅有登录次数限制。该字段应配置为yes,使用PAM策略,可以起到防暴力破解。
UsePAM no
系统日志设施
该配置表示,可通过rsyslog
服务的设施分类规则记录日志。在 /etc/rsyslog.conf
已有规则authpriv.* /var/log/secure
用于记录安全日志
。建议对照该规则
配置SyslogFacility
为AUTHPRIV
, 这样可以记录SSH服务日志
,比如用户的认证鉴权日志
,登入登出日志
等,为安全事件提供日志支撑。
# 系统日志设施,默认为AUTH。
SyslogFacility AUTH
# 记录ssh日志到rsyslog
# SyslogFacility AUTHPRIV
忽略Rhosts
在Rlogin
协议中,Rhosts
配置表示授权远程访问
,由于Rlogin
协议已不安全
,所 以应禁止
使用Rhosts
配置。建议配置为yes
。
# 忽略Rhosts,默认为yes
IgnoreRhosts yes
指定SSH强加密算法
推荐:AES-256
和SHA-512
加密算法。
# 列出支持的密码
ssh -Q cipher
# 列出支持的 MAC
ssh -Q mac
# 列出支持的公钥类型
ssh -Q key
# 客户端获取支持密钥交换算法列表
ssh -Q kex
# 服务端获取支持算法列表
sshd -T | grep kex
修改/etc/ssh/sshd_config
文件,将下列语句
直接加到该文件后面
:
https://www.openssh.com/legacy.html
# 指定`Ciphers`采用强加密算法--SSH数据传输的加密算法
Ciphers aes256-ctr,chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
# 设置SSH数据校验的哈希算法--用于检测流量修改的消息验证码
MACs hmac-sha2-512,hmac-sha2-512-etm@openssh.com
# 用于生成每个连接密钥的密钥交换算法
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
# SSH 服务器接受的公钥算法,用于向 SSH 客户端验证自身身份
# HostkeyAlgorithms ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,ssh-rsa
当然,最后要重启ssh服务才能使配置生效。
# 客户端验证ssh加密算法
ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -v 10.0.1.220
ssh登录优化
ssh登录速度慢,可以通过下列配置优化。
判断客户端是否合法-UserDNS
# 使用dns反查客户端的主机名,会导致登陆变慢
UserDNS yes
# 一般内网时设置为no,连接速度会加快
UserDNS no
在sshd服务端上查看ssh客户端的ip
# 在sshd服务端上查看ssh客户端的ip地址
env|grep -i ssh
-vvv显示登陆详情–debug时使用
https://wangchujiang.com/linux-command/c/sshd.html
ssh客户端debug
模式
# debug模式--显示登陆时的所有信息,方便调试诊断
## -v -vv -vvv
ssh -vvv 用户名@服务器地址
ssh系统登录提示
Linux 系统通过 /etc/issue
、 /etc/issue.net
、 /etc/motd
以及 /run/motd.dynamic
来显示登录后的提示信息,通常用于向用户提示系统版本、硬件信息等,
/etc/issue
、/etc/issue.net
通常在未登录终端前
显示,/etc/motd
、/run/motd.dynamic
则是在成功登录终端后
显示。- 此外还有
/etc/profile
与/etc/profile.d/
,在登录后会运行
其中的脚本。
ssh登录后欢迎语
/etc/motd
和/run/motd.dynamic
文件里面保存的是ssh登录后欢迎语
# 插入 你好吗
echo '你好吗'> /etc/motd
# 登陆后会打印 你好吗
sshd服务无法启动错误排查
可能导致ssh错误
的原因:
- 升级
内核
- 升级
sshd
服务 - 配置
sshd
服务 - 修改
iptables
策略 - 配置
hosts.deny
- 修改
内核参数
- 修改
limits.conf
- 配置
SElinux
- 配置
PAM认证
机制 - 系统
负载过高
- 安装
第三方安全防护软件
等
sshd故障排查步骤
分析sshd日志
# 从最后500条日志中过滤sshd关键字
tail -500 /var/log/secure | grep 'sshd'
测试sshd是否可以启动
https://wangchujiang.com/linux-command/c/sshd.html
# 对sshd进行测试,并给出错误提示
sshd -t
调试模式启动sshd
# 调试模式启动sshd
sshd -d
# 详细调试模式启动sshd
sshd -ddd
查看sshd的ssh协议版本
https://developer.aliyun.com/article/269860
# 安装scanssh
## 安装epel源
yum install -y epel-release
## yum安装scanssh
yum install -y scanssh
# 扫描sshd协议版本
## -n 指定ssh服务端的端口
scanssh -s ssh -n 端口 ip或ip段
sshd常见故障
ssh证书登陆失败
错误提示: ssh Permission denied (publickey)
ssh启动错误提示:Missing privilege separation directory: /run/sshd
原因分析: openssh-Server的Bug
临时解决办法1:
# 创建缺失的目录
mkdir -p /run/sshd
# 修改权限
chmod -R 0755 /run/sshd
chown -R root:root /run/sshd
解决方法2:
修改配置文件
修改/usr/lib/tmpfiles.d/sshd.conf
,修改d /var/run/sshd 0755 root
为d /run/sshd 0755 root root
解决办法3–建议:
升级openssh
可以解决此Bug