外观
Linux安全
约 309 字大约 1 分钟
Linux
2025-03-28
SSH 登录安全配置
- 更改默认端口(低)
- 不使用 22
- 启用 2FA(高)
- 安装验证软件
apt install libpam-google-authenticator - 配置 Google Auth
google-authenticator - 修改配置文件
vi /etc/ssh/sshd_configChallengeResponseAuthentication yes;AuthenticationMethods publickey,keyboard-interactive
- 配置 PAM
nano /etc/pam.d/sshd;auth required pam_google_authenticator.so - 重启服务
systemctl restart sshd
- 安装验证软件
- 使用 SSH 密钥代替密码(高)
- 生成密钥对:
ssh-keygen -t ed25519(推荐 Ed25519 算法,安全且高效) - 将公钥添加到服务器:ssh-copy-id username@server_ip。
- 禁用密码登录:在/etc/ssh/sshd_config 中设置 PasswordAuthentication no。
- 生成密钥对:
只为用户开放一个文件夹的权限
sudo mkdir -p /var/upload
sudo chown actionsuser:users /var/upload
sudo chmod 700 /var/upload哪个进程在对外发包: ss -tanp | awk -F'[",=]' '/users:\(\(/ {cnt[$2 ":" $6]++} END{for(i in cnt) print cnt[i], i}' | sort -nr
查看日志
#统计PAM hit black次数
grep "PAM hit black" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -rn
#登陆失败的记录
lastb -n 20
# 登录日志中包含accepted\|session opened的20条
sudo grep -i "accepted\|session opened" /var/log/auth.log | tail -20
#统计尝试了哪些用户名及尝试次数
grep -oE "Invalid user [^ ]+|Failed password for invalid user [^ ]+" /var/log/auth.log | awk '{print $NF}' | sort | uniq -c | sort -rn
#统计登录ip
grep -oP "from \K[0-9.]+" /var/log/auth.log | sort | uniq -c | sort -rn | head -10