在 Linux 下,“telnet 如何防火墙”通常有两种理解,我分别说明:
telnet 默认端口是 23,且明文传输,极不安全,生产环境建议直接禁用或限制。
# 禁止 telnet 端口
firewall-cmd --permanent --remove-service=telnet
firewall-cmd --permanent --add-port=23/tcp --zone=public
firewall-cmd --permanent --remove-port=23/tcp
firewall-cmd --reload
只允许特定 IP:
firewall-cmd --permanent --add-rich-rule='
rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="23" accept'
firewall-cmd --reload
# 禁止 telnet
iptables -A INPUT -p tcp --dport 23 -j DROP
# 只允许某个 IP
iptables -A INPUT -p tcp --dport 23 -s 192.168.1.100 -j ACCEPT
iptables -A INPUT -p tcp --dport 23 -j DROP
保存:
service iptables save # CentOS 6
# 或
iptables-save > /etc/sysconfig/iptables
systemctl stop telnet.socket
systemctl disable telnet.socket
如果你要用 telnet ip port 测试连通性:
# firewalld
firewall-cmd --permanent --add-port=23/tcp
firewall-cmd --reload
# iptables
iptables -A INPUT -p tcp --dport 23 -j ACCEPT
✅ 不要用 telnet 做远程登录
✅ 用 SSH(22端口) 替代
ssh user@ip
如果你能说明:
我可以给你更精确的命令。