配置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防火墙规则,并采取其他安全措施来保护你的服务器。