1. 最彻底的安全措施:禁用Telnet服务优先停用该服务,是因为Telnet会以明文传输用户名、密码等数据,存在遭窃听或篡改的风险。停止并禁用Telnet可执行以下命令:
sudo systemctl stop telnet.socket# 停止Telnet服务sudo systemctl disable telnet.socket # 禁止开机自启通过systemctl status telnet.socket确认服务状态(显示“inactive (dead)”且“disabled”即为生效)。
2. 限制Telnet端口的防火墙访问服务即使继续运行,防火墙也能拦截未经授权的访问。CentOS上常用的防火墙工具包括:
sudo firewall-cmd --permanent --remove-service=telnet# 移除Telnet服务规则(若已存在)sudo firewall-cmd --reload # 重新加载配置sudo iptables -A INPUT -p tcp --dport 23 -j DROP # 拒绝23端口的TCP连接sudo service iptables save # 保存规则(CentOS 6及以下)执行该操作后,可有效拦截外部IP对Telnet端口的访问。
3. 以SSH替换Telnet(最佳替代方案)SSH能够提供加密通信通道,从根本上消除Telnet的安全缺陷。配置步骤如下:
sudo yum install openssh-server -ysudo systemctl start sshdsudo systemctl enable sshd/etc/ssh/sshd_config):PermitRootLogin no # 禁止root远程登录PubkeyAuthentication yes # 启用公钥认证(更安全)PasswordAuthentication no # 可选:禁用密码认证(需提前配置公钥)MaxAuthTries 3 # 限制登录尝试次数(防止暴力破解)sudo systemctl restart sshdsudo firewall-cmd --permanent --add-service=sshsudo firewall-cmd --reload安全性还可借助密钥认证或SSH反向隧道继续提高。
4. 使用SELinux强化安全防护借助强制访问控制(MAC),SELinux能够限制Telnet服务权限:
sudo setenforce 1# 临时设置为Enforcing模式sudo sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config# 永久生效sudo setsebool -P telnetd_disable_trans on# 阻止Telnet的网络传输通过getsebool -a | grep telnet确认策略是否已经生效。
5. 监控与入侵防御
/var/log/secure(CentOS)中的Telnet登录记录,识别异常尝试:sudo grep 'telnet' /var/log/securesudo yum install fail2ban -ysudo systemctl start fail2bansudo systemctl enable fail2ban编辑/etc/fail2ban/jail.local,添加Telnet监控规则:[telnet]enabled = trueport = 23filter = telnetdlogpath = /var/log/securemaxretry = 3bantime = 600重启Fail2Ban使配置生效。6. 必须使用时限制Telnet登录用户如有特殊需求而保留Telnet,应采取以下方式压缩攻击面:
telnetuser),同时设置强密码。/etc/securetty文件:注释或删除root用户的登录权限,防止root通过Telnet登录:sudo vi /etc/securetty# 注释掉类似“tty1”“tty2”的行(或添加“#”前缀)/etc/xinetd.d/telnet,添加only_from指令限制允许访问的IP段:service telnet{disable = noonly_from = 192.168.1.0/24# 仅允许内网IP访问}重启xinetd服务使配置生效:sudo systemctl restart xinetd注意:此方法仍无法解决明文传输问题,仅作为临时缓解措施。