配置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服務的安全性和可用性。