Skip to content
Eternal Center

Eternal Center

  • Single-Node (单节点)
    • System (系统)
    • Service (服务)
    • Database (数据库)
    • Container (容器)
    • Virtualization (虚拟化)
  • Multi-Node (多节点)
    • Cluster (集群)
    • Big Data (大数据)
    • Cloud Computing (云计算)
    • Batch Processing (批量处理)
  • Approach (方式方法)
    • Languages (语言)
    • Ideas (思路)
    • Programing (编程)
    • Project (项目)
  • Eternity (永恒)
    • News (消息)
    • Creations (创作)
    • Classics (经典)
    • Legends (传说)
    • Chronicle (编年史)
    • FNIOS (宇宙公民开源学院)
Posted on June 6, 2022November 22, 2024 by Mingyu Zhu

[内容] Linux SSH 常用安全加强配置项

  • 内容一:SSH 的配置文件
    • 1.1 SSH 配置文件的位置
    • 1.2 SSH 配置文件的所属主、所属组和权限
      • 1.2.1 确保 SSH 配置文件的所属主和所属组都是 root
      • 1.2.2 确保 SSH 配置文件的权限是 640
  • 内容二:SSH 常用安全加强配置项
    • 2.1 将 SSH 设置为 v2 版本
      • 2.1.1 将 SSH 设置为 v2 版本的配置参数
      • 2.1.2 将 SSH 设置为 v2 版本的参考步骤
    • 2.2 忽略 Rhosts 认证
      • 2.2.1 忽略 Rhosts 认证的配置参数
      • 2.2.2 设置忽略 Rhosts 认证的参考步骤
    • 2.3 禁止受信任的客户端通过 .rhosts 或 /etc/hosts.equiv 进行认证
      • 2.3.1 设置禁止受信任的客户端通过 .rhosts 或 /etc/hosts.equiv 进行认证的配置参数
      • 2.3.2 设置禁止受信任的客户端通过 .rhosts 或 /etc/hosts.equiv 进行认证的参考步骤
    • 2.4 禁止使用用户提交的 SSH 环境
      • 2.4.1 设置禁止使用用户提交的 SSH 环境的配置参数
      • 2.4.2 设置禁止使用用户提交的 SSH 环境的参考步骤
    • 2.5 禁止 root 用户通过 SSH 登录
      • 2.5.1 禁止 root 用户通过 SSH 登录的配置参数
      • 2.5.2 设置禁止 root 用户通过 SSH 登录的参考步骤
    • 2.6 禁止空密码 SSH 登录
      • 2.6.1 禁止空密码 SSH 登录的配置参数
      • 2.6.2 设置禁止空密码 SSH 登录的参考步骤
    • 2.7 将 SSH 登录信息写入系统日志
      • 2.7.1 将 SSH 登录信息写入系统日志的配置参数
      • 2.7.2 设置将 SSH 登录信息写入系统日志的参看步骤
    • 2.8 记录全部 SSH 信息
      • 2.8.1 记录全部 SSH 信息的配置参数
      • 2.8.2 设置记录全部 SSH 信息的参考步骤
    • 2.9 设置最大尝试登录失败数和触发尝试登录失败日志的登录失败数
      • 2.9.1 设置最大尝试登录失败数和触发尝试登录失败日志的登录失败数的配置参数
      • 2.9.2 设置最大尝试登录失败数和触发尝试登录失败日志的登录失败数的参考步骤
    • 2.10 设置 SSH 登录时密码输入的时间
      • 2.10.1 设置 SSH 登录时密码输入的时间的配置参数
      • 2.10.2 设置 SSH 登录时密码输入的时间都参考步骤
    • 2.11 禁止 SSH 图形界面转发
      • 2.11.1 禁止 SSH 图形界面转发的配置参数
      • 2.11.1 设置禁止 SSH 图形界面转发的参考步骤
    • 2.12 禁止 SSH TCP 转发
      • 2.12.1 设置禁止 SSH TCP 转发的配置参数
      • 2.12.2 设置禁止 SSH TCP 转发的参考步骤
    • 2.13 不显示上次 SSH 登录的信息,例如时间和地点等
      • 2.13.1 设置不显示上次 SSH 登录的信息,例如时间和地点等的配置参数
      • 2.13.1 设置不显示上次 SSH 登录的信息,例如时间和地点等的参考步骤
    • 2.14 设置同时只能让几个 SSH 用户登录
      • 2.14.1 设置同时只能让几个 SSH 用户登录的配置参数
      • 2.14.2 设置同时只能让几个 SSH 用户登录的参考步骤
    • 2.15 设置每个连接可以开启会话的个数
      • 2.15.1 设置每个连接可以开启会话的个数的配置参数
      • 2.15.2 设置每个连接可以开启会话的个数的参考步骤
    • 2.16 设置客户端 SSH 空闲超时时间
      • 2.16.1 设置客户端 SSH 空闲超时时间的配置参数
      • 2.16.2 设置客户端 SSH 空闲超时时间的参考步骤
    • 2.17 禁止使用 RSA 进行 rhosts 的安全验证
      • 2.17.1 设置禁止使用 RSA 进行 rhosts 的安全验证的参数
      • 2.17.2 设置禁止使用 RSA 进行 rhosts 的安全验证的参考步骤
    • 2.18 禁止使用验证代理 (如果存在) 转发给远程服务器
      • 2.18.1 设置禁止使用验证代理 (如果存在) 转发给远程服务器的参数
      • 2.18.2 设置禁止使用验证代理 (如果存在) 转发给远程服务器的参考步骤
    • 2.19 指定安全的 SSH Ciphers 加密算法
      • 2.19.1 设置 SSH Ciphers 加密算法的配置参数
      • 2.19.2 设置 SSH Ciphers 加密算法的参考步骤
    • 2.20 指定安全的 SSH MACs 加密算法
      • 2.20.1 设置 SSH MACs 加密算法的配置参数
      • 2.20.2 设置 SSH MACs 加密算法的参考步骤
    • 2.21 指定安全的 SSH KexAlgorithms 加密算法
      • 2.21.1 设置 SSH KexAlgorithms 加密算法的配置参数
      • 2.21.2 设置 SSH KexAlgorithms 加密算法的参考步骤
  • 内容三:重启 sshd 服务
  • 补充:Linux SSH 访问的限制

内容一:SSH 的配置文件

1.1 SSH 配置文件的位置

/etc/ssh/sshd_config

1.2 SSH 配置文件的所属主、所属组和权限

1.2.1 确保 SSH 配置文件的所属主和所属组都是 root
# chown root:root /etc/ssh/sshd_config
1.2.2 确保 SSH 配置文件的权限是 640
# chmod 640 /etc/ssh/sshd_config

内容二:SSH 常用安全加强配置项

2.1 将 SSH 设置为 v2 版本

2.1.1 将 SSH 设置为 v2 版本的配置参数
Protocol 2
2.1.2 将 SSH 设置为 v2 版本的参考步骤
# sed -i 's/.*Protocol .*/Protocol 2/g' /etc/ssh/sshd_config

2.2 忽略 Rhosts 认证

2.2.1 忽略 Rhosts 认证的配置参数
IgnoreRhosts yes
2.2.2 设置忽略 Rhosts 认证的参考步骤
# sed -i 's/^.*IgnoreRhosts .*/IgnoreRhosts yes/g' /etc/ssh/sshd_config

2.3 禁止受信任的客户端通过 .rhosts 或 /etc/hosts.equiv 进行认证

2.3.1 设置禁止受信任的客户端通过 .rhosts 或 /etc/hosts.equiv 进行认证的配置参数
HostbasedAuthentication no
2.3.2 设置禁止受信任的客户端通过 .rhosts 或 /etc/hosts.equiv 进行认证的参考步骤
# sed -i 's/.*HostbasedAuthentication .*/HostbasedAuthentication no/g' /etc/ssh/sshd_config

2.4 禁止使用用户提交的 SSH 环境

2.4.1 设置禁止使用用户提交的 SSH 环境的配置参数
PermitUserEnvironment no
2.4.2 设置禁止使用用户提交的 SSH 环境的参考步骤
# sed -i 's/.*PermitUserEnvironment .*/PermitUserEnvironment no/g' /etc/ssh/sshd_config

2.5 禁止 root 用户通过 SSH 登录

2.5.1 禁止 root 用户通过 SSH 登录的配置参数
PermitRootLogin no
2.5.2 设置禁止 root 用户通过 SSH 登录的参考步骤
# sed -i 's/.*PermitRootLogin .*/PermitRootLogin no/g' /etc/ssh/sshd_config

2.6 禁止空密码 SSH 登录

2.6.1 禁止空密码 SSH 登录的配置参数
PermitEmptyPasswords no
2.6.2 设置禁止空密码 SSH 登录的参考步骤
# sed -i 's/.*PermitEmptyPasswords .*/PermitEmptyPasswords no/g' /etc/ssh/sshd_config

2.7 将 SSH 登录信息写入系统日志

2.7.1 将 SSH 登录信息写入系统日志的配置参数
SyslogFacility AUTH
2.7.2 设置将 SSH 登录信息写入系统日志的参看步骤
# sed -i 's/.*SyslogFacility .*/SyslogFacility AUTH/g' /etc/ssh/sshd_config

2.8 记录全部 SSH 信息

2.8.1 记录全部 SSH 信息的配置参数
LogLevel INFO
2.8.2 设置记录全部 SSH 信息的参考步骤
# sed -i 's/.*LogLevel .*/LogLevel INFO/' /etc/ssh/sshd_config

2.9 设置最大尝试登录失败数和触发尝试登录失败日志的登录失败数

2.9.1 设置最大尝试登录失败数和触发尝试登录失败日志的登录失败数的配置参数
MaxAuthTries 5

(补充:这里以设置最大尝试登录失败数是 5 次为例,如果此时尝试登录次数超过 2,则系统日志中会出现登录失败的记录)

2.9.2 设置最大尝试登录失败数和触发尝试登录失败日志的登录失败数的参考步骤
# sed -i 's/.*MaxAuthTries .*/MaxAuthTries 5/g' /etc/ssh/sshd_config

(补充:这里以设置最大尝试登录失败数是 5 次为例,如果此时尝试登录次数超过 2,则系统日志中会出现登录失败的记录)

2.10 设置 SSH 登录时密码输入的时间

2.10.1 设置 SSH 登录时密码输入的时间的配置参数
LoginGraceTime 600

或者:

LoginGraceTime 10m

(补充:这里以设置 SSH 登录时密码输入的时间为 600 秒为例)

2.10.2 设置 SSH 登录时密码输入的时间都参考步骤
# sed -i 's/.*LoginGraceTime .*/LoginGraceTime 600/g' /etc/ssh/sshd_config

或者:

# sed -i 's/.*LoginGraceTime .*/LoginGraceTime 10m/g' /etc/ssh/sshd_config

(补充:这里以设置 SSH 登录时密码输入的时间为 600 秒为例)

2.11 禁止 SSH 图形界面转发

2.11.1 禁止 SSH 图形界面转发的配置参数
X11Forwarding no
2.11.1 设置禁止 SSH 图形界面转发的参考步骤
# sed -i 's/.*X11Forwarding .*/X11Forwarding no/g' /etc/ssh/sshd_config

2.12 禁止 SSH TCP 转发

2.12.1 设置禁止 SSH TCP 转发的配置参数
AllowTcpForwarding no
2.12.2 设置禁止 SSH TCP 转发的参考步骤
# sed -i 's/.*AllowTcpForwarding .*/AllowTcpForwarding no/g' /etc/ssh/sshd_config

2.13 不显示上次 SSH 登录的信息,例如时间和地点等

2.13.1 设置不显示上次 SSH 登录的信息,例如时间和地点等的配置参数
PrintMotd no

或者:

PrintLastLog no
2.13.1 设置不显示上次 SSH 登录的信息,例如时间和地点等的参考步骤
# sed -i 's/.*PrintMotd .*/PrintMotd no/g' /etc/ssh/sshd_config

或者:

# sed -i 's/.*PrintLastLog .*/PrintLastLog no/g' /etc/ssh/sshd_config

2.14 设置同时只能让几个 SSH 用户登录

2.14.1 设置同时只能让几个 SSH 用户登录的配置参数
MaxStartups 10:30:60

(
补充:这里以
1) 最多同时只能让 10 个用户发起 SSH 登录请求,超过此数量的登录请求会被拒绝
2) 当 SSH 连接数超过上限时 (这里设置的是 10 个),再新发起 SSH 登录请求的用户,会有 30% 的概率被拒绝
3) 当 SSH 连接数超过 60 时,再新发起 SSH 登录请求的用户全部会被拒绝,这里的 30 参数将会无效
)

2.14.2 设置同时只能让几个 SSH 用户登录的参考步骤
# sed -i 's/.*MaxStartups .*/MaxStartups 10:30:60/g' /etc/ssh/sshd_config

(
补充:这里以
1) 最多同时只能让 10 个用户发起 SSH 登录请求,超过此数量的登录请求会被拒绝
2) 当 SSH 连接数超过上限时 (这里设置的是 10 个),再新发起 SSH 登录请求的用户,会有 30% 的概率被拒绝
3) 当 SSH 连接数超过 60 时,再新发起 SSH 登录请求的用户全部会被拒绝,这里的 30 参数将会无效
)

2.15 设置每个连接可以开启会话的个数

2.15.1 设置每个连接可以开启会话的个数的配置参数
MaxSessions 4

(补充:这里以设置每个连接可以开启 4 个会话为例,默认值为 10)

2.15.2 设置每个连接可以开启会话的个数的参考步骤
# sed -i 's/.*MaxSessions .*/MaxSessions 4/g' /etc/ssh/sshd_config

(补充:这里以设置每个连接可以开启 4 个会话为例,默认值为 10)

2.16 设置客户端 SSH 空闲超时时间

2.16.1 设置客户端 SSH 空闲超时时间的配置参数
ClientAliveInterval 100
ClientAliveCountMax 3

(补充:这里以设置客户端 SSH 空闲超时时间为 100 * 3=300 秒为例)

2.16.2 设置客户端 SSH 空闲超时时间的参考步骤
# sed -i 's/.*ClientAliveInterval .*/ClientAliveInterval 100/g' /etc/ssh/sshd_config
# sed -i 's/.*ClientAliveCountMax .*/ClientAliveCountMax 3/g' /etc/ssh/sshd_config

(补充:这里以设置客户端 SSH 空闲超时时间为 100 * 3=300 秒为例)

2.17 禁止使用 RSA 进行 rhosts 的安全验证

2.17.1 设置禁止使用 RSA 进行 rhosts 的安全验证的参数
RhostsRSAAuthentication no
2.17.2 设置禁止使用 RSA 进行 rhosts 的安全验证的参考步骤
# sed -i 's/.*RhostsRSAAuthentication .*/RhostsRSAAuthentication no/g' /etc/ssh/sshd_config

2.18 禁止使用验证代理 (如果存在) 转发给远程服务器

2.18.1 设置禁止使用验证代理 (如果存在) 转发给远程服务器的参数
ForwardAgent no
2.18.2 设置禁止使用验证代理 (如果存在) 转发给远程服务器的参考步骤
# sed -i 's/.*ForwardAgent .*/ForwardAgent no/g' /etc/ssh/sshd_config

2.19 指定安全的 SSH Ciphers 加密算法

2.19.1 设置 SSH Ciphers 加密算法的配置参数
Ciphers aes256-ctr,aes192-ctr,aes128-ctr

(补充:这里以使用 aes256-ctr、aes192-ctr 和 aes128-ctr SSH Ciphers 加密算法为例)

2.19.2 设置 SSH Ciphers 加密算法的参考步骤
# echo "Ciphers aes256-ctr,aes192-ctr,aes128-ctr" >> /etc/ssh/sshd_config

(补充:这里以使用 aes256-ctr、aes192-ctr 和 aes128-ctr SSH Ciphers 加密算法为例)

2.20 指定安全的 SSH MACs 加密算法

2.20.1 设置 SSH MACs 加密算法的配置参数
MACs hmac-sha2-512,hmac-sha2-256

(补充:这里以使用 hmac-sha2-512 和 hmac-sha2-256 SSH MACs 加密算法为例)

2.20.2 设置 SSH MACs 加密算法的参考步骤
# echo "MACs hmac-sha2-512,hmac-sha2-256" >> /etc/ssh/sshd_config

(补充:这里以使用 hmac-sha2-512 和 hmac-sha2-256 SSH MACs 加密算法为例)

2.21 指定安全的 SSH KexAlgorithms 加密算法

2.21.1 设置 SSH KexAlgorithms 加密算法的配置参数
KexAlgorithms ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256

(补充:这里以使用 ecdh-sha2-nistp521、ecdh-sha2-nistp384、ecdh-sha2-nistp256 和 diffie-hellman-group-exchange-sha256 SSH KexAlgorithms 加密算法为例)

2.21.2 设置 SSH KexAlgorithms 加密算法的参考步骤
# echo "KexAlgorithms ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256" >> /etc/ssh/sshd_config

(补充:这里以使用 ecdh-sha2-nistp521、ecdh-sha2-nistp384、ecdh-sha2-nistp256 和 diffie-hellman-group-exchange-sha256 SSH KexAlgorithms 加密算法为例)

内容三:重启 sshd 服务

# systemctl restart sshd

(补充:内容二里的参数只有重启了 sshd 服务后才能生效)

补充:Linux SSH 访问的限制

Linux SSH 访问的限制
(Rocky Linux 8 & RHEL 8 版)
Linux SSH 访问的限制
(openSUSE & SLES & CentOS 7 & RHEL 7 版)
Linux SSH 只影响部分客户端访问的设置
CategoriesChinese Post (中文帖子), System (系统), System Login Security (系统登录安全), System Network & System Security & System Log (系统网络 & 系统安全 & 系统日志), System Operation & System Setting & System Software (系统操作 & 系统设置 & 系统软件), System Port Security (系统端口安全), System Security (系统安全), System Setting (系统设置), System Setting Other Contents (系统设置其它内容), System Setting Others (系统设置其它)

Post navigation

Previous PostPrevious 随笔 16
Next PostNext [内容] Linux 用户登录记录的显示

Aspiration (愿景):

Everyone can achieve self-achievement and self-happiness fairly

每个人都能公平地实现自我成就和自我幸福

Logo (徽标):

Additional Information (其他信息):

About     Manual     Clone     Contact
Disclaimer     Friendly Links     Donation

关于     手册     克隆     联系
免责声明     友情链接     捐赠

Search Inside Website (站内搜索)

Search Outside Website (站外搜索):

Google         Wikipedia         Bing

Eternal URL (永恒网址):

https://eternity.eternalcenter.com Will be last access method / 将是最后的访问方式

Proudly powered by LNMP Proudly powered by WordPress