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 (宇宙公民开源学院)

Category: System Security (系统安全)

Posted on June 6, 2022November 22, 2024

[内容] 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 只影响部分客户端访问的设置
Posted on June 6, 2022July 9, 2023

[工具] Shell 批量检测指定用户是否可以登录本地服务器

介绍

基本信息

作者:朱明宇
名称:批量检测指定用户是否可以登录本地服务器
作用:批量检测指定用户是否可以登录本地服务器

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量

directorylist=”daemon bin sys adm uucp guest nobody lpd lp” #要被检测的用户

脚本

#!/bin/bash

####################### Separator ########################

directorylist="daemon bin sys adm uucp guest nobody lpd lp"

####################### Separator ########################

directorycheck=

for i in `echo $directorylist`
do
        directorycheck="$directorycheck `cat /etc/passwd | egrep "^$i:" | egrep -v '/sbin/nologin|false|/bin/false' | awk -F: '{print $1}'`"
done

echo $directorycheck
Posted on June 4, 2022November 22, 2024

[步骤] Linux 密码的安全 (设置密码复杂度和加密算法) (openSUSE & SLES 版)

步骤一:修改 /etc/pam.d/common-password-pc 配置文件

# vim /etc/pam.d/common-password-pc

将以下内容:

......
password	requisite	pam_cracklib.so ......
......

修改为:

......
password        requisite       pam_cracklib.so try_first_pass local_users_only enforce-for-root minlen=15 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 maxrepeat=5 retry=3 difok=3 remember=5
......

并添加以下内容:

......
password    requisite     pam_pwhistory.so try_first_pass local_users_only enforce-for-root remember=5 use_authtok
password    sufficient    pam_unix.so sha512 shadow try_first_pass use_authtok remember=24 use_authtok

(
补充:这里以
1) pam_cracklib.so 模块使用前 1 个模块从用户那里得到的密码 (try_first_pass)
2) 只作用于本地用户 (local_users_only)
3) 也作用于 root 用户 (enforce-for-root)
4) 密码最小长度为 15 个字符 (minlen=15)
5) 密码必须包含数字的个数 (dcredit=-1)
6) 密码必须包含大写字母的个数 (ucredit=-1)
7) 密码必须包含小写字母的个数 (lcredit=-1)
8) 密码必须包含特殊字符的个数 (ocredit=-1)
9) 最多只允许 5 个连续字符 (maxrepeat=5,如果是 0 则禁用该选项)
10) 3 次尝试错误密码后产生错误提示 (retry=3)
11) 新密码最多可以有 3 个字符和旧密码相同 (difok=3)
12) 新密码不能和最近用过的 24 个密码相同 (remember=24)
13) 使用 sha512 加密方法加密 (sha512)
为例
)

步骤二:修改 /etc/security/pwquality.conf 配置文件

# vim /etc/security/pwquality.conf

将部分内容修改如下:

......
minlen = 15
......
dcredit = -1
......
ucredit = -1
......
lcredit = -1
......
ocredit = -1
......
dictcheck = 1
......
usercheck = 1
......
maxrepeat = 5
......
retry = 3
......
difok = 3
......

(
补充:这里以
1) 密码最小长度为 15 个字符 (minlen = 15)
2) 密码必须包含数字的个数 (dcredit = -1)
3) 密码必须包含大写字母的个数 (ucredit = -1)
4) 密码必须包含小写字母的个数 (lcredit = -1)
5) 密码必须包含特殊字符的个数 (ocredit = -1)
6) 密码不能包含字典 (dictcheck = 1)
7) 密码不能包含用户 (usercheck = 1)
8) 新密码不能和前 5 个老密码重复 (maxrepeat=5)
9) 3 次尝试错误密码后产生错误提示 (retry=3)
10) 新密码最多可以有 3 个字符和旧密码相同 (difok=3)
为例
)

步骤三:修改 /etc/login.defs 配置文件

# vim /etc/login.defs

将以下内容:

......
ENCRYPT_METHOD ......
......

修改为:

......
ENCRYPT_METHOD SHA512
......

(补充:这里以使用 SHA512 哈希算法加密密码为例)

Posted on June 4, 2022November 22, 2024

[步骤] Linux 密码的安全 (设置密码复杂度和加密算法) (CentOS 7 & RHEL 7 版)

步骤一:修改 /etc/pam.d/system-auth-ac 配置文件

# vim /etc/pam.d/system-auth-ac

将以下内容:

......
password    requisite     pam_pwquality.so ......
......
password    requisite     pam_pwhistory.so ......
......
password    sufficient    pam_unix.so
......

修改为:

......
password    requisite     pam_pwquality.so try_first_pass local_users_only enforce-for-root minlen=15 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 maxrepeat=5 retry=3 difok=3 remember=24
......
password    requisite     pam_pwhistory.so try_first_pass local_users_only enforce-for-root remember=24 use_authtok
......
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=24
......

(
补充:这里以
1) pam_pwquality.so 模块使用前 1 个模块从用户那里得到的密码 (try_first_pass)
2) 只作用于本地用户 (local_users_only)
3) 也作用于 root 用户 (enforce-for-root)
4) 密码最小长度为 15 个字符 (minlen=15)
5) 密码必须包含数字的个数 (dcredit=-1)
6) 密码必须包含大写字母的个数 (ucredit=-1)
7) 密码必须包含小写字母的个数 (lcredit=-1)
8) 密码必须包含特殊字符的个数 (ocredit=-1)
9) 最多只允许 5 个连续字符 (maxrepeat=5,如果是 0 则禁用该选项)
10) 3 次尝试错误密码后产生错误提示 (retry=3)
11) 新密码最多可以有 3 个字符和旧密码相同 (difok=3)
12) 新密码不能和最近用过的 24 个密码相同 (remember=24)
13) 使用 sha512 加密方法加密 (sha512)
为例
)

步骤二:修改 /etc/pam.d/password-auth-ac 配置文件

# vim /etc/pam.d/password-auth-ac

将以下内容:

......
password    requisite     pam_pwquality.so ......
......
password    requisite     pam_pwhistory.so ......
......
password    sufficient    pam_unix.so
......

修改为:

......
password    requisite     pam_pwquality.so try_first_pass local_users_only enforce-for-root retry=3 minlen=15 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1 maxrepeat=5 difok=3 remember=24
......
password    requisite     pam_pwhistory.so try_first_pass local_users_only enforce-for-root remember=24 use_authtok
......
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=24
......

(
补充:这里以
1) pam_pwquality.so 模块使用前 1 个模块从用户那里得到的密码 (try_first_pass)
2) 只作用于本地用户 (local_users_only)
3) 也作用于 root 用户 (enforce-for-root)
4) 密码最小长度为 15 个字符 (minlen=15)
5) 密码必须包含数字的个数 (dcredit=-1)
6) 密码必须包含大写字母的个数 (ucredit=-1)
7) 密码必须包含小写字母的个数 (lcredit=-1)
8) 密码必须包含特殊字符的个数 (ocredit=-1)
9) 最多只允许 5 个连续字符 (maxrepeat=5,如果是 0 则禁用该选项)
10) 3 次尝试错误密码后产生错误提示 (retry=3)
11) 新密码最多可以有 3 个字符和旧密码相同 (difok=3)
12) 新密码不能和最近用过的 24 个密码相同 (remember=24)
13) 使用 sha512 加密方法加密 (sha512)
为例
)

步骤三:修改 /etc/security/pwquality.conf 配置文件

# vim /etc/security/pwquality.conf

将部分内容修改如下:

......
minlen = 15
......
dcredit = -1
......
ucredit = -1
......
lcredit = -1
......
ocredit = -1
......
dictcheck = 1
......
usercheck = 1
......

(
补充:这里以
1) 密码最小长度为 15 个字符 (minlen = 15)
2) 密码必须包含数字的个数 (dcredit = -1)
3) 密码必须包含大写字母的个数 (ucredit = -1)
4) 密码必须包含小写字母的个数 (lcredit = -1)
5) 密码必须包含特殊字符的个数 (ocredit = -1)
6) 密码不能包含字典 (dictcheck = 1)
7) 密码不能包含用户 (usercheck = 1)
为例
)

步骤四:修改 /etc/login.defs 配置文件

# vim /etc/login.defs

将以下内容:

......
ENCRYPT_METHOD ......
......

修改为:

......
ENCRYPT_METHOD SHA512
......

(补充:这里以使用 SHA512 哈希算法加密密码为例)

Posted on May 27, 2022November 22, 2024

[排错] 解决 Linux 运行 su 命令时正确密码输入无效

分析

当 /usr/bin/su 文件的权限设置不正确时,会出现运行 su 命令时正确密码输入无效的情况

解决方法

# chown root:root /usr/bin/su ; chmod 4755 /usr/bin/su

Posts pagination

Previous page Page 1 … Page 20 Page 21 Page 22 … Page 48 Next page

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