# 修改 hostname echo -e "${GREEN}是否修改 hostname?(y/N)${NC}" read -p "请输入 y 继续,否则默认不修改: " modify_hostname if [[ "$modify_hostname" =~ ^[Yy]$ ]]; then read -p "请输入新的 hostname: " new_hostname if [ -n "$new_hostname" ]; then hostnamectl set-hostname "$new_hostname" if ! grep -q "$new_hostname" /etc/hosts; then sed -i "1s/127.0.0.1\tlocalhost/127.0.0.1\tlocalhost/" /etc/hosts sed -i "2i127.0.1.1\t$new_hostname" /etc/hosts fi fi fi
# 交互式修改 SSH 端口 echo -e "${GREEN}是否修改 SSH 端口?(y/N)${NC}" read -p "请输入 y 继续,否则默认不修改: " modify_ssh if [[ "$modify_ssh" =~ ^[Yy]$ ]]; then read -p "请输入新的 SSH 端口(默认 22): " ssh_port ssh_port=${ssh_port:-22} sed -i "s/^#\?Port .*/Port $ssh_port/" /etc/ssh/sshd_config sed -i "s/^#\?X11Forwarding .*/X11Forwarding no/" /etc/ssh/sshd_config ufw allow "$ssh_port"/tcp fi
# 修改 DNS 配置 echo -e "${GREEN}是否修改 DNS 配置?(y/N)${NC}" read -p "请输入 y 继续,否则默认不修改: " modify_dns if [[ "$modify_dns" =~ ^[Yy]$ ]]; then read -p "请输入新的 DNS 服务器(多个用空格分隔): " dns_servers if [ -n "$dns_servers" ]; then cp /etc/resolv.conf /etc/resolv.conf.bak chattr -i /etc/resolv.conf > /etc/resolv.conf for dns in $dns_servers; do echo "nameserver $dns" >> /etc/resolv.conf done chattr +i /etc/resolv.conf fi fi
# 修改 GRUB_CMDLINE_LINUX,添加 "net.ifnames=0 biosdevname=0" echo -e "${GREEN}修改 GRUB 配置...${NC}" if ! grep -q "net.ifnames=0 biosdevname=0" /etc/default/grub; then sed -i 's/^GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="\1 net.ifnames=0 biosdevname=0"/' /etc/default/grub update-grub fi
# 修改 sysctl.conf 添加 BBR echo -e "${GREEN}配置 BBR...${NC}" sed -i 's/^net\.core\.default_qdisc=fq$/net.core.default_qdisc = fq/' /etc/sysctl.conf sed -i 's/^net\.ipv4\.tcp_congestion_control=bbr$/net.ipv4.tcp_congestion_control = bbr/' /etc/sysctl.conf if ! grep -q "net.core.default_qdisc = fq" /etc/sysctl.conf; then echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf fi if ! grep -q "net.ipv4.tcp_congestion_control = bbr" /etc/sysctl.conf; then echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf fi sysctl -p