从命令行在 Debian 11/12 上设置固定 IP 地址

默认情况下,Debian 配置为自动从 DHCP 服务器接收 IP 地址。如果需要,您可以手动设置静态 IP 地址。在本教程中,我将解释如何从命令终端或在 Linux Debian 11/12 上配置固定 IP 地址。

在 Linux Debian 11/12 上设置固定 IP 地址

您的网络接口的配置文件位于/etc/network/interfaces。要从命令行更改 Debian 上的静态 IP 地址,只需使用编辑器修改此文件vi,vim或者nano:

$ sudo vi /etc/network/interfaces
## ou ##
$ sudo vim /etc/network/interfaces
## ou ##
$ sudo nano /etc/network/interfaces

这是配置文件的示例。找到主网络接口的线路。例如eth0这里用红色写着“主要网络接口» :

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

⚠️ 注意力 :不要从环回接口中删除线路lo以及将源加载到的行/etc/network/interfaces.d/*

替换以下示例中的部分,自定义值:

  • 地址:固定IP地址
  • 网络掩码: 子网掩码
  • 网关:网关
  • dns 名称服务器:DNS 服务器(如果需要)。对于这个例子,我把。
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    gateway 192.168.1.254
    dns-nameservers 1.1.1.1 8.8.8.8

💡您可以在同一行注明地址和掩码。例如 :192.0.2.7/24

如果需要将接口配置为IPV6,请替换参数inet标杆inet 6。例如 :

    iface eth0 inet6 static
        address 2001:db8::c0ca:1eaf/64
        gateway 2001:db8::1ead:ed:beef

将更改保存到配置文件后,重新启动网络接口:

$ ifdown eth0
$ ifup eth0

在一些更复杂的情况下,可能需要重新启动网络管理器服务:

$ sudo systemctl restart network-manager

你也可以自然地。

查看命令行IP配置

在应用任何更改之前或之后,最好检查一下配置。您可以使用以下命令列出系统的网络接口ip:

ip -c addr

争论-c允许您以颜色显示文本。要仅显示 IPV4 地址,请使用选项-4

Debian 上使用“ip”命令的网络接口列表

💡 在旧版本的 Debian (<10) 上,您可以使用命令ifconfig

在上面的示例中,有两个网络接口名称:

  • :系统内部的环回接口,允许系统与自身通信。
  • 以太网0:第二个是活动接口,允许在本地网络和互联网上进行通信。

在网络配置级别,您还可以查看以下信息:

  • 内网:IP地址/子网掩码
  • 布罗德: 广播地址
  • 动态的或者静止的:表示配置的IP地址是动态的还是固定的

了解有关订购的更多信息ip, 类型man ip