专栏
天翼云开发者社区

SSH 安全使用及技巧

2023-11-23 16:51:05 12阅读

SSH 是一种广泛使用的协议,用于安全地访问 Linux 服务器,下面分享一些增强安全访问的方法和SSH使用的技巧。

1. 禁用root登陆

关闭 root 用户的服务器访问是一种防御策略。禁用root登陆,并创建具有root权限的新用户名。

useradd -G sudo,netdev,kvm,vde2-net,docker -d /home/starby -s /bin/bash -m starXXXX
passwd starXXXX
然后修改配置文件 /etc/ssh/sshd_config: 禁止root用户远程登陆,指定允许用户登陆。同时禁止空密码账户登陆。
PermitRootLogin no
AllowUsers  starXXXX
PermitEmptyPasswords no
具体含义可以参考 man 5 sshd_config 。然后重启 sshd 服务即可。
sudo systemctl restart sshd

2. 更改默认端口

修改默认的22端口为别的值,增加攻击者扫描的难度。需修改配置文件 /etc/ssh/sshd_config,同时注意修改防火墙的配置规则来匹配此配置。

Port 50022

3. 限制登陆/访问尝试

通过指定允许的密码尝试次数,在尝试一定次数后自动终止SSH 连接,可以防止攻击者利用此漏洞对服务器进行暴力破解。同样需要修改配置文件 /etc/ssh/sshd_config 中的 MaxAuthTries 值。

#StrictModes yes
MaxAuthTries 2
#MaxSessions 4

4. 使用 SSH 版本 2

通过修改配置文件 /etc/ssh/sshd_config 中的 Protocol 值来实现:

Protocol 2

5. 关闭TCP端口转发和X11转发

关闭 AllowTcpForwarding 和 X11Forwarding 功能,可以防止攻击者尝试通过 SSH 连接的端口转发来访问您的其他系统。

X11Forwarding no 
AllowTcpForwarding no

6. 使用 SSH 密钥连接

服务器禁止密码登陆

连接到服务器的最安全方法之一是使用 SSH 密钥。可以通过修改 /etc/ssh/sshd_config 文件中与密码相关的参数来完全关闭对服务器的密码访问。

PasswordAuthentication no
# AuthenticationMethods "publickey"
# ssh-keygen will generate RSA key in the example.
RSAAuthentication yes
PubkeyAuthentication yes
ssh-keygen 产生密钥

密钥文件可以使用命令 ssh-keygen 来生成,一对密钥分为 Public 和 Private. 公钥将上传到要连接的服务器,而私钥则存储在自己的计算机上,不能用来分享。

[client]$ ssh-keygen -b 4096 -t rsa -C your_email@example.com -f ~/.ssh/id_rsa-2COM
[client]$ chmod 600 ~/.ssh/id_rsa-2COM
[client]$ scp ~/.ssh/id_rsa-2COM.pub  USER@SERVER:~/
[client]$ ssh USER@SERVER
[server]$ cat ~/.ssh/id_rsa-2COM.pub > ~/.ssh/authorized_keys
[server]$ chmod 600 ~/.ssh/authorized_keys
[server]$ rm id_rsa-2COM.pub
 
[client]$ ssh -l USER -i ~/.ssh/id_rsa-2COM SERVER

上面配置中公钥文件为 id_rsa-2COM.pub,私钥文件为 id_rsa-2COM.

7. SSH 连接的 IP 限制

iptables 设置

增加这样的限制,一方面是使用 iptables 规则等进行限制。比如如下配置:

# allow 50022
iptables -A INPUT -p tcp --dport 50022 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 50022 -j ACCEPT
# allow loopback
iptables -A INPUT -i lo -p all -j ACCEPT
iptables -A OUTPUT -o lo -p all -j ACCEPT
 
# Allow to access outside and the packet in from outside.
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# only allow ssh from 192.168.0.3. Just for example.
iptables -A INPUT -s 192.168.0.3 -p tcp --dport 50022 -j ACCEPT
 
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
系统访问控制文件 hosts.allow

另一方面也可以使用系统访问控制文件 /etc/hosts.allow 来进行限制 SSH 权限,允许特定 IP 范围,或单个 IP 或者使用拒绝命令阻止所有剩余的 IP 地址,然后重启 sshd 服务进行生效。参考 man 5 hosts_access, man 5 hosts.allow , 下面是 /etc/hosts.allow 示例:

$ cat /etc/hosts.allow
# /etc/hosts.allow: list of hosts that are allowed to access the system.
#                   See the manual pages hosts_access(5) and hosts_options(5).
#
# Example:    ALL: LOCAL @some_netgroup
#             ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
#
# If you're going to protect the portmapper use the name "rpcbind" for the
# daemon name. See rpcbind(8) and rpc.mountd(8) for further information.
#
sshd: 192.168.80.0/24: ALLOW
  • 0
  • 0
  • 0
0 评论
0/1000
评论(0) 发表评论
l****n

l****n

3 篇文章 1 粉丝
关注

SSH 安全使用及技巧

2023-11-23 16:51:05 12阅读

SSH 是一种广泛使用的协议,用于安全地访问 Linux 服务器,下面分享一些增强安全访问的方法和SSH使用的技巧。

1. 禁用root登陆

关闭 root 用户的服务器访问是一种防御策略。禁用root登陆,并创建具有root权限的新用户名。

useradd -G sudo,netdev,kvm,vde2-net,docker -d /home/starby -s /bin/bash -m starXXXX
passwd starXXXX
然后修改配置文件 /etc/ssh/sshd_config: 禁止root用户远程登陆,指定允许用户登陆。同时禁止空密码账户登陆。
PermitRootLogin no
AllowUsers  starXXXX
PermitEmptyPasswords no
具体含义可以参考 man 5 sshd_config 。然后重启 sshd 服务即可。
sudo systemctl restart sshd

2. 更改默认端口

修改默认的22端口为别的值,增加攻击者扫描的难度。需修改配置文件 /etc/ssh/sshd_config,同时注意修改防火墙的配置规则来匹配此配置。

Port 50022

3. 限制登陆/访问尝试

通过指定允许的密码尝试次数,在尝试一定次数后自动终止SSH 连接,可以防止攻击者利用此漏洞对服务器进行暴力破解。同样需要修改配置文件 /etc/ssh/sshd_config 中的 MaxAuthTries 值。

#StrictModes yes
MaxAuthTries 2
#MaxSessions 4

4. 使用 SSH 版本 2

通过修改配置文件 /etc/ssh/sshd_config 中的 Protocol 值来实现:

Protocol 2

5. 关闭TCP端口转发和X11转发

关闭 AllowTcpForwarding 和 X11Forwarding 功能,可以防止攻击者尝试通过 SSH 连接的端口转发来访问您的其他系统。

X11Forwarding no 
AllowTcpForwarding no

6. 使用 SSH 密钥连接

服务器禁止密码登陆

连接到服务器的最安全方法之一是使用 SSH 密钥。可以通过修改 /etc/ssh/sshd_config 文件中与密码相关的参数来完全关闭对服务器的密码访问。

PasswordAuthentication no
# AuthenticationMethods "publickey"
# ssh-keygen will generate RSA key in the example.
RSAAuthentication yes
PubkeyAuthentication yes
ssh-keygen 产生密钥

密钥文件可以使用命令 ssh-keygen 来生成,一对密钥分为 Public 和 Private. 公钥将上传到要连接的服务器,而私钥则存储在自己的计算机上,不能用来分享。

[client]$ ssh-keygen -b 4096 -t rsa -C your_email@example.com -f ~/.ssh/id_rsa-2COM
[client]$ chmod 600 ~/.ssh/id_rsa-2COM
[client]$ scp ~/.ssh/id_rsa-2COM.pub  USER@SERVER:~/
[client]$ ssh USER@SERVER
[server]$ cat ~/.ssh/id_rsa-2COM.pub > ~/.ssh/authorized_keys
[server]$ chmod 600 ~/.ssh/authorized_keys
[server]$ rm id_rsa-2COM.pub
 
[client]$ ssh -l USER -i ~/.ssh/id_rsa-2COM SERVER

上面配置中公钥文件为 id_rsa-2COM.pub,私钥文件为 id_rsa-2COM.

7. SSH 连接的 IP 限制

iptables 设置

增加这样的限制,一方面是使用 iptables 规则等进行限制。比如如下配置:

# allow 50022
iptables -A INPUT -p tcp --dport 50022 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 50022 -j ACCEPT
# allow loopback
iptables -A INPUT -i lo -p all -j ACCEPT
iptables -A OUTPUT -o lo -p all -j ACCEPT
 
# Allow to access outside and the packet in from outside.
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# only allow ssh from 192.168.0.3. Just for example.
iptables -A INPUT -s 192.168.0.3 -p tcp --dport 50022 -j ACCEPT
 
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
系统访问控制文件 hosts.allow

另一方面也可以使用系统访问控制文件 /etc/hosts.allow 来进行限制 SSH 权限,允许特定 IP 范围,或单个 IP 或者使用拒绝命令阻止所有剩余的 IP 地址,然后重启 sshd 服务进行生效。参考 man 5 hosts_access, man 5 hosts.allow , 下面是 /etc/hosts.allow 示例:

$ cat /etc/hosts.allow
# /etc/hosts.allow: list of hosts that are allowed to access the system.
#                   See the manual pages hosts_access(5) and hosts_options(5).
#
# Example:    ALL: LOCAL @some_netgroup
#             ALL: .foobar.edu EXCEPT terminalserver.foobar.edu
#
# If you're going to protect the portmapper use the name "rpcbind" for the
# daemon name. See rpcbind(8) and rpc.mountd(8) for further information.
#
sshd: 192.168.80.0/24: ALLOW
文章来自专栏

工作环境

2 篇文章 1 订阅
0 评论
0/1000
评论(0) 发表评论
  • 0
    点赞
  • 0
    收藏
  • 0
    评论