使用 /etc/network/interfaces 配置网络接口

发布时间: 更新时间: 总字数:797 阅读时间:2m 作者:IP:上海 网址

/etc/network/interfaces 是 debian 系统中最为重要的网络配置文件,它用于配置网络接口的参数。

介绍

  • NAME /etc/network/interfaces - network interface configuration for ifup and ifdown
  • /etc/network/interfaces 文件用于指定如何配置网络接口
    • Debian 自带的网络管理的名字叫 ifupdown
    • 该文件由 ifquery、ifup 和 ifdown 处理更改系统网络状态

安装

bash
apt install ifupdown
  • 详细配置信息 man interfaces
    • /usr/share/doc/ifupdown/examples/network-interfaces.gz
  • Starting and Stopping Interfaces
text
# ifdown enp7s0
# ifup enp7s0
  • Reinitialize new network setup
bash
# systemctl status networking
# systemctl restart networking
  • hooks
text
   /etc/network/if-pre-up.d/
          Scripts in this directory are run before bringing the interface up.

   /etc/network/if-up.d/
          Scripts in this directory are run after bringing the interface up.

   /etc/network/if-down.d/
          Scripts in this directory are run before bringing the interface down.

   /etc/network/if-post-down.d/
          Scripts in this directory are run after bringing the interface down.

常见配置

常规配置

text
auto lo

iface lo inet loopback
iface eth0 inet static
    address 192.168.43.81
    netmask 255.255.255.0
    gateway 192.168.43.1

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

iface home inet static
    address 192.168.43.80
    netmask 255.255.255.0
    gateway 192.168.43.1

说明:

  • auto lo
    • auto 用来识别以 -a 参数运行 ifup 时应该启用的物理接口(这一参数被用在系统启动脚本中)
    • 每条 `auto 接口 命令应占据一行,ifup 根据列出的顺序依次启用接口
  • allow-hotplug wlan0
    • allow- 用来识别应从各种子系统启用的接口
    • ifup --allow=hotplug eth0 eth1 只会启用列在 allow-hotplug 行中的 eth0 或 eth1
    • 其中 allow-auto和auto是同义词
  • iface lo inet ...、iface eth0 inet ...、iface wlan0 inet ...、iface default inet ...
    • iface 后是逻辑接口名。在最简单的情况下,如果不存在 mapping 配置,逻辑接口名与物理接口名相同。接口名后是接口所使用的地址族(address family)
      • inet是 TCP/IP v4
      • inet6是 IPv6
    • ... inet loopback、... inet static、... inet manual、... inet dhcp
      • inet后是用来配置接口的方法
        • loopback 方法用来定义 IPv4 loopback 接口
        • static 方法定义接口的静态 IPv4 地址,包含的选项有 address、netmask、broadcast、metric、gateway、pointopoint、hwaddress、mtu、scope
        • manual 方法默认不配置接口,需通过外部程序(ifupdown 以外的程序)配置或手动配置
        • dhcp 方法使用 dhclient、pump、udhcpc、dhcpcd 中任意一个工具,通过 DHCP 获取 IP 地址

br 配置

text
auto br0
iface br0 inet static
	address 10.10.0.15/24
	gateway 10.10.0.1
	bridge_ports eth0 eth1
	up /usr/sbin/brctl stp br0 on

静态 IP 无线

text
auto wlan0
iface wlan0 inet static
  address 192.168.3.186
  netmask 255.255.255.0
  gateway 192.168.3.1
  wpa-ssid xxx
  wpa-psk xxx
  wpa-proto WPA

动态 IP 无线

text
auto wlan0
iface wlan0 inet dhcp
  wpa-ssid xxx
  wpa-psk xxx
  wpa-proto WPA

参考

  1. https://manpages.debian.org/bookworm/ifupdown-ng/interfaces.5.en.html
  2. https://wiki.debian.org/NetworkConfiguration