Linux ssh优化脚本(解决连接慢的问题)
#!/bin/bash
# 定义配置文件路径
SSHD_CONFIG="/etc/ssh/sshd_config"
# 确保脚本以root权限运行
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
# 添加或修改UseDNS配置
if grep -q "^#*UseDNS" $SSHD_CONFIG; then
sed -i 's/^#*UseDNS.*/UseDNS no/' $SSHD_CONFIG
else
echo "UseDNS no" >> $SSHD_CONFIG
fi
# 修改GSSAPIAuthentication配置
if grep -q "^#*GSSAPIAuthentication" $SSHD_CONFIG; then
sed -i 's/^#*GSSAPIAuthentication.*/GSSAPIAuthentication no/' $SSHD_CONFIG
else
echo "GSSAPIAuthentication no" >> $SSHD_CONFIG
fi
# 修改GSSAPICleanupCredentials配置
if grep -q "^#*GSSAPICleanupCredentials" $SSHD_CONFIG; then
sed -i 's/^#*GSSAPICleanupCredentials.*/GSSAPICleanupCredentials no/' $SSHD_CONFIG
else
echo "GSSAPICleanupCredentials no" >> $SSHD_CONFIG
fi
# 重新启动sshd服务以应用更改
systemctl restart sshd
echo "Configuration updated and sshd service restarted."