如何配置CentOS的SSH防火墙规则
导读:配置CentOS的SSH防火墙规则可以通过多种方式实现,包括使用iptables、firewalld或nftables。以下是使用这些工具的详细步骤: 使用 iptables 查看当前的iptables规则: sudo iptables...
配置CentOS的SSH防火墙规则可以通过多种方式实现,包括使用iptables、firewalld或nftables。以下是使用这些工具的详细步骤:
使用 iptables
-
查看当前的iptables规则:
sudo iptables -L -n -
允许SSH连接:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT -
保存iptables规则: CentOS 7及以下版本使用
service iptables save,CentOS 8及以上版本使用firewall-cmd。-
CentOS 7:
sudo service iptables save -
CentOS 8:
sudo firewall-cmd --runtime-to-permanent
-
-
重启iptables服务(如果需要):
sudo systemctl restart iptables
使用 firewalld
-
查看当前的firewalld状态:
sudo firewall-cmd --state -
允许SSH连接:
sudo firewall-cmd --permanent --add-service=ssh -
重新加载firewalld配置:
sudo firewall-cmd --reload -
验证SSH端口是否开放:
sudo firewall-cmd --list-all
使用 nftables
-
查看当前的nftables规则:
sudo nft list ruleset -
允许SSH连接:
sudo nft add rule inet filter input tcp dport 22 accept -
保存nftables规则:
sudo nft list ruleset > /etc/nftables.conf -
启用nftables服务(如果尚未启用):
sudo systemctl enable --now nftables
其他安全建议
-
限制SSH访问来源:可以通过编辑
/etc/hosts.deny和/etc/hosts.allow文件来限制SSH访问来源。sudo nano /etc/hosts.deny添加:
sshd: ALL然后编辑
/etc/hosts.allow:sudo nano /etc/hosts.allow添加:
sshd: 192.168.1.0/24 -
更改默认SSH端口:可以通过编辑
/etc/ssh/sshd_config文件来更改默认SSH端口。sudo nano /etc/ssh/sshd_config找到并修改:
Port 22保存并退出,然后重启SSH服务:
sudo systemctl restart sshd
通过以上步骤,你可以配置CentOS的SSH防火墙规则,并采取其他安全措施来保护你的服务器。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 如何配置CentOS的SSH防火墙规则
本文地址: https://pptw.com/jishu/769751.html
