如何在Debian Linux上设置静态IP地址
目的
目的是在Debian Linux服务器上配置静态IP地址。请注意,对于桌面安装,建议使用GUI工具,例如
network-manager
。如果希望直接通过/etc/network/interfaces
桌面上的文件配置网络接口,请确保禁用任何其他可能干扰网络配置的守护程序。例如,以下命令将禁用network-manager
:# systemctl stop NetworkManager.service # systemctl disable NetworkManager.service
操作系统和软件版本
- 操作系统: -Debian 9(Stretch)
要求
需要对Debian Linux系统的特权访问。困难
简单约定
使用说明
启用静态IP
默认情况下,您将在/etc/network/interfaces
网络配置文件中找到以下配置: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更新
iface eth0 inet dhcp
到iface eth0 inet static
。/etc/network/interfaces
网络配置文件的最终内容应类似于以下内容: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 static
配置IP地址
在此阶段,关于如何为我们的eth0
网络接口配置静态IP地址,我们有两种选择。第一种选择是将IP地址配置直接添加到/etc/network/interfaces
文件中。将以下行添加到现有行/etc/network/interfaces
:address 10.1.1.125 netmask 255.0.0.0 gateway 10.1.1.1生成的内容
/etc/network/interfaces
文件应类似于以下文件。根据需要更新您的IP地址,网络掩码和网关: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 static address 10.1.1.125 netmask 255.0.0.0 gateway 10.1.1.1第二个推荐的选项是在
/etc/network/interfaces.d/
目录中单独定义网络接口。在
networking
守护程序启动期间,将在/etc/network/interfaces.d/
目录中搜索网络接口配置。任何发现的网络配置都包含在中/etc/network/interfaces
。用任意文件名创建一个新的网络配置文件,例如。
eth0
并包括eth0
如下所示的IP地址配置。为此,请使用首选的文本编辑器,例如vim:# cat /etc/network/interfaces.d/eth0 iface eth0 inet static address 10.1.1.125 netmask 255.0.0.0 gateway 10.1.1.1现在,删除上面所述的行,
/etc/network/interfaces
这样您将得到:# cat /etc/network/interfaces # 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
静态DNS服务器
要配置静态DNS编辑/etc/resolv.conf
文件,并包含您首选的IP地址,nameserver
例如:nameserver 8.8.8.8或者,将以下行添加到您的
/etc/network/interfaces
网络配置文件中:dns-nameservers 8.8.8.8 8.8.4.4
应用更改
要应用更改,请重新启动网络守护程序:# service networking restart