在Linux上配置FTP服務器的端口轉發,通常需要使用iptables或者firewalld這樣的防火墻工具。以下是兩種常見的方法:
登錄到你的Linux服務器。
檢查iptables規則(可選):
sudo iptables -L -n -v
添加端口轉發規則:
假設你的FTP服務器運行在本地端口21上,并且你想將其轉發到外部IP地址<外部IP>
的端口21上。
sudo iptables -t nat -A PREROUTING -p tcp --dport 21 -j DNAT --to-destination <外部IP>:21
sudo iptables -t nat -A POSTROUTING -p tcp -d <外部IP> --dport 21 -j MASQUERADE
保存iptables規則: 不同的Linux發行版可能有不同的方法來保存iptables規則。例如,在Debian/Ubuntu上,你可以使用:
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
在CentOS/RHEL上,你可以使用:
sudo service iptables save
重啟防火墻服務(如果需要):
sudo systemctl restart iptables
登錄到你的Linux服務器。
檢查firewalld狀態:
sudo firewall-cmd --state
啟用firewalld服務(如果尚未啟用):
sudo systemctl start firewalld
添加端口轉發規則:
假設你的FTP服務器運行在本地端口21上,并且你想將其轉發到外部IP地址<外部IP>
的端口21上。
sudo firewall-cmd --permanent --add-forward-port=port=21:proto=tcp:toport=21:toaddr=<外部IP>
重新加載firewalld配置:
sudo firewall-cmd --reload
驗證規則是否已添加:
sudo firewall-cmd --list-all
通過以上步驟,你應該能夠在Linux服務器上成功配置FTP服務器的端口轉發。