如何在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系统进行特权访问。

约定

  •  - 要求使用root权限直接以root用户或使用命令执行给定的linuxsudo命令
  • $  - 要求给定的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 dhcpiface 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

PS:这里是另外一种配置,二选一

一种另外选择配置英文的在/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

PS:如果您使用的是虚拟机VirturalBox,请注意要先将网络配置成桥接网络,原来是NAT模式,改成静态静态还是连接不上,需要把网络改成Bridged Adapter即可,Promiscuous Mode设置为允许全部。

root@debian-extrem:~# 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 enp0s3
iface enp0s3 inet static 
        address 192.168.1.98
        netmask 255.255.255.0
        gateway 192.168.1.1
        dns-nameservers 8.8.8.8 8.8.4.4

现在能ping通网络:

root@debian-extrem:~# ping www.baidu.com
PING www.a.shifen.coM (119.75.217.26) 56(84) bytes of data.
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=1 ttl=49 time=48.2 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=2 ttl=49 time=46.1 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=3 ttl=49 time=52.4 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=4 ttl=49 time=47.9 ms
64 bytes from 119.75.217.26 (119.75.217.26): icmp_seq=5 ttl=49 time=52.1 ms

转载来源:https ://linuxconfig.org/how-to-setup-a-static-ip-address-on-debian-linux

猜你喜欢

转载自blog.csdn.net/Aria_Miazzy/article/details/84827026