安全的SSH设置
source link: https://www.lujun9972.win/blog/2018/10/11/%E5%AE%89%E5%85%A8%E7%9A%84ssh%E8%AE%BE%E7%BD%AE/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
安全的SSH设置
偶尔登陆了一次腾讯云主机的root用户,惊讶地发现有大量失败登陆的记录,把我吓了一跳。
使用 lastb
查了一下,发现登陆名称是千奇百怪。其中实验次数最多的用户名分别为:
sudo lastb|awk '{print $1}' |sort |uniq -c |sort -n -k 1 |tail
160 guest 218 hadoop 248 git 252 ubuntu 356 user 406 postgres 420 test 486 oracle 1161 admin 147101 root
之前我一直以为我这小破主机应该是没人会在意的,所以没有对SSH服务进行什么配置。现在看起来我还是太naive了。
迫于无奈,只好对SSH服务进行加固处理。目前找到的加固方法有下面几个:
确定 sshd_config 的权限
确定 sshd_config
只有root才能读写,其他用户不能进行任何操作
sudo chown root:root /etc/ssh/sshd_config sudo chmod og-rwx /etc/ssh/sshd_config
修改ssh服务端口
这一步的效果很显著,只从改了ssh服务端口后,就在没有看到任何登陆失败的提示了
Port XXXX
只启用SSH Protocol 2
Protocol 1的安全性相对要差一些
Protocol 2
不允许空密码登陆
PermitEmptyPasswords no
不允许root登陆
从上面对 lastb
的结果分析可以得知, root
是被实验最多的用户,没有之一。
因此禁用 root 登陆很有必要:
PermitRootLogin no
若你觉得一股脑儿完全禁用root登陆太过了,而希望只在特定的主机上允许root登陆,则可以禁止 root 用密码登陆
PermitRootLogin prohibit-password
然后在特定主机上建立公私密钥认证关系即可。
限制只能登陆特定用户
我们可以使用 DenyUsers
, AllowUsers
, DenyGroups
和 AllowGroups
关键字来限定允许登陆的用户。
当某个用户登陆时,该用户依次经过 DenyUsers
, AllowUsers
, DenyGroups
和 AllowGroups
的测试,只有在所有测试都通过的情况下才允许登陆。
AllowUsers ramesh john jason AllowGroups sysadmin dba DenyUsers cvs apache jane DenyGroups developers qa
修改未登陆的超时时间
用户未登陆成功的情况下,默认120秒后会登出。这个时间可以缩短,比如60.
LoginGraceTime 1m
但要注意,若该值缩减为 0
则表示没有时间限制
设置会话空闲一段时间后自动退出
设置空间一段时间自动退出有两种方法,
一种是设置 TMOUT
环境变量,比如在 ~/.bash_profile
中加上
export TMOUT=100
还有一种方法是设置 sshd_config
ClientAliveInterval 100 ClientAliveCountMax 0
Recommend
-
7
SSH 安全配置之 MaxStartups 最近发现个别服务器 ssh 登录时偶尔出现失败,发现和 MaxStartups 有关。 本地模拟环境: 10.0.7.113: 客户端 10.0.7.9: 服务端
-
11
设置Linux系统SSH-KEY访问方式设置 作者: wencst 分类: linux,Uncategorized 发布时...
-
17
炎忍的博客在 Windows 系统上给 ssh 设置代理发表于2020-06-01|更新于2020-11-25|技术记录字...
-
8
V2EX › Python ssh 可以设置常用地登录吗? chenqh · 1 天前 · 1399 次点击
-
6
openwrt 给普通用户设置 ssh 私钥访问 2022-03-13 技术 暂无评论 3...
-
1
Debian 设置 root 用户 SSH 登录
-
5
SSH无密码登录及putty设置 2018-04-04 发布在
-
7
一个 ssh 设置免密登陆脚本 2022-09-24 tech
-
9
WSL2设置SSH远程连接 2023-01-07...
-
3
设置git Ssh代理 2024-01-17 1 分钟阅读 万万没想到啊,github被封轮到自己头上了。 这个博客是在windows下用Typora写的,然后hugo生成,直接用git for windows提交,今天git无法提交了。
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK