debian

如何在Debian上配置网络

小樊
80
2025-09-11 06:02:30
栏目: 智能运维

在Debian上配置网络主要有以下三种方法,可根据系统版本和需求选择:

一、使用 /etc/network/interfaces 文件(传统方法,适用于所有版本)

  1. 查看网卡名称
    ip addrls /sys/class/net
  2. 编辑配置文件
    sudo nano /etc/network/interfaces,添加以下内容:
    • 静态IP
      auto eth0  
      iface eth0 inet static  
      address 192.168.1.100  
      netmask 255.255.255.0  
      gateway 192.168.1.1  
      dns-nameservers 8.8.8.8 8.8.4.4  
      
    • 动态IP(DHCP)
      auto eth0  
      iface eth0 inet dhcp  
      
  3. 重启网络服务
    sudo systemctl restart networking

二、使用 netplan(适用于Debian 10及更高版本)

  1. 安装netplan
    sudo apt update && sudo apt install netplan.io
  2. 编辑配置文件
    sudo nano /etc/netplan/01-netcfg.yaml,示例:
    network:  
      version: 2  
      renderer: networkd  
      ethernets:  
        eth0:  
          dhcp4: no  
          addresses: [192.168.1.100/24]  
          gateway4: 192.168.1.1  
          nameservers:  
            addresses: [8.8.8.8, 8.8.4.4]  
    
  3. 应用配置
    sudo netplan apply

三、使用 NetworkManager(图形化工具,适合桌面用户)

  1. 安装NetworkManager
    sudo apt update && sudo apt install network-manager
  2. 启用服务
    sudo systemctl enable NetworkManager && sudo systemctl start NetworkManager
  3. 图形界面配置
    • 点击系统托盘中的 NetworkManager 图标编辑连接 → 选择接口(如eth0)→ 设置静态IP或DHCP。
    • 也可通过命令行:
      sudo nmcli connection add type ethernet con-name "Wired" ifname eth0 ipv4.addresses "192.168.1.100/24" ipv4.gateway "192.168.1.1" ipv4.method manual

验证配置

注意:配置前建议备份原始文件(如 sudo cp /etc/network/interfaces /etc/network/interfaces.bak),避免误操作导致网络中断。

0
看了该问题的人还看了