注:Telnet传输数据未加密,存在安全风险,建议优先使用SSH替代。
安装Telnet服务(如需使用)
sudo apt update
sudo apt install xinetd telnetd # 安装xinetd和telnetd
配置Telnet服务
编辑配置文件 /etc/xinetd.d/telnet,确保以下参数:
service telnet
{
disable = no # 启用服务(生产环境建议设为yes禁用)
only_from = 192.168.1.0/24 # 限制访问IP(可选)
per_source = 5 # 限制单IP并发连接数
cps = 10 30 # 限制连接速率(每秒10次,峰值30次)
}
重启服务生效:
sudo systemctl restart xinetd
防火墙规则
sudo ufw deny 23/tcp # 拒绝Telnet默认端口(23)
sudo ufw allow from 192.168.1.0/24 to any port 23 # 允许特定IP访问
sudo ufw reload
sudo iptables -A INPUT -p tcp --dport 23 -j DROP # 拒绝所有Telnet流量
sudo iptables -I INPUT -s 192.168.1.100 -p tcp --dport 23 -j ACCEPT # 允许特定IP
sudo sh -c "iptables-save > /etc/iptables/rules.v4"
用户权限控制
libpam-pwquality):/etc/pam.d/telnet,添加:auth required pam_pwquality.so retry=3 minlen=8 difok=4
sudo apt install openssh-server
sudo systemctl enable ssh
sudo ufw allow ssh # 允许SSH访问(默认端口22)
sudo systemctl disable telnet.socket
sudo systemctl stop telnet.socket
sudo tail -f /var/log/auth.log | grep telnet
fail2ban防止暴力破解(需安装):sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
在[telnet]段启用并配置规则(参考SSH配置示例)。总结:Telnet仅适用于测试环境,生产环境务必使用SSH。若必须使用Telnet,需通过防火墙、IP限制、PAM等多层防护降低风险。