如何在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
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設定為允許全部。
[email protected]:~# 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通網路:
[email protected]:~# 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