setup_network.sh 587 字节
#!/bin/bash
# 一键配置 Debian 网络自动启用 enp2s0

set -e

INTERFACES_FILE="/etc/network/interfaces"
BACKUP_FILE="/etc/network/interfaces.bak.$(date +%F-%T)"

echo "正在备份原配置到 $BACKUP_FILE ..."
sudo cp $INTERFACES_FILE $BACKUP_FILE

cat <<EOF | sudo tee $INTERFACES_FILE
# /etc/network/interfaces 自动生成配置
auto lo
iface lo inet loopback

auto enp2s0
iface enp2s0 inet dhcp
EOF

echo "配置已写入 $INTERFACES_FILE"

echo "重启网络服务..."
sudo systemctl restart networking

echo "完成!当前 IP 地址:"
ip addr show enp2s0 | grep "inet "