配置CentOS SSH防火墙规则通常涉及使用firewalld
或iptables
。以下是使用这两种方法的步骤:
firewalld
启动并启用 firewalld
服务:
sudo systemctl start firewalld
sudo systemctl enable firewalld
开放SSH端口(默认是22):
sudo firewall-cmd --permanent --add-service=ssh
重新加载防火墙规则以应用更改:
sudo firewall-cmd --reload
验证SSH端口是否已开放:
sudo firewall-cmd --list-all
你应该能在输出中看到ssh
服务,并且端口22是开放的。
iptables
启动并启用 iptables
服务:
sudo systemctl start iptables
sudo systemctl enable iptables
开放SSH端口(默认是22):
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
保存 iptables
规则:
CentOS 7及以上版本使用firewalld
,但如果你使用的是CentOS 6或更早版本,可以使用以下命令保存规则:
sudo service iptables save
验证规则是否已添加:
sudo iptables -L -n
你应该能在输出中看到一条规则,允许TCP端口22的流量。
安全性:确保只允许必要的IP地址访问SSH端口,可以使用iptables
的-s
选项来指定源IP地址。
sudo iptables -A INPUT -p tcp -s 192.168.1.1 --dport 22 -j ACCEPT
SELinux:如果启用了SELinux,可能需要调整SELinux策略以允许SSH连接。
防火墙状态:在配置防火墙规则之前,确保防火墙服务是启动的,并且没有被其他安全软件(如ufw)覆盖。
通过以上步骤,你应该能够成功配置CentOS的SSH防火墙规则,确保SSH服务的安全性和可用性。