ssh保护主机教程

1,密钥管理

ssh管理密钥是以用户为单位,位于家目录下的/.ssh/config、/.ssh/authorized_keys。其中config用于管理私钥和不同域名/ip的对应关系,authorized_keys用于管理允许登录当前用户的公钥集合。

config格式
Host github.com
HostName ssh.github.com
Port 443
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

而authorized_keys文件的内容是公钥的内容,可使用命令 cat /path/isa.pub >> ~/.ssh/authorized_keys从文件末尾直接写入。

2,ssh登录ip、登录方式限制

vim /etc/ssh/sshd_config

按照需求修改如下配置

AllowUsers user@1.1.1.1 # 允许通过ssh登录的ip和用户,此处配置的是ip来源为1.1.1.1的user
Port 669 #修改默认端口,记得放开防火墙端口,忘记了可以通过商家面板上的vnc进入进入主机放开
PermitRootLogin no # 禁止root用户通过ssh登录
RSAAuthentication yes # 启用 RSA 密钥认证机制
PubkeyAuthentication yes # 启用基于公钥的认证方法
PasswordAuthentication no # 关闭密码登录

这里可以创建一个低权限的用户进行登录,然后实际使用中通过su root切换用户或者sudo提升权限。

root@ubuntu:/home/user# adduser u
Adding user u' ... Adding new group u' (1001) ...
Adding new user u' (1001) with group u' ...
Creating home directory /home/u' ... Copying files from /etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for u
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]

3,ip封禁
参考blog使用fail2ban: https://www.cnblogs.com/Alwayslearn/p/16545500.html

posted @ 2025-09-14 14:48  wenzhuo4657  阅读(5)  评论(0)    收藏  举报