Debian configures static IP and DNS

1. Environment:

Operating system: Debian GNU/Linux 10

2. Configure static IP

1. Interfaces configuration
file path:/etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

#开机启动eth1
auto eth1
#接口eth1配置,static配置静态IP,dhcp为自动获取IP
iface eth1 inet static
#静态IP地址
address 192.168.8.87
#子网掩码
netmask 255.255.255.0
#网关
gateway 192.168.8.1

注:在这里面配置DNS并不起作用,没啥卵用。

2. Restart network configuration
(1) Encounter problems -1
Execute/etc/init.d/networking restart

Restarting networking (via systemctl): networking.service[ 5705.038541] IPv6: ADDRCONF(NETDEV_UP): eth1: link is not ready
[ 5705.378501] Micrel KSZ8081 or KSZ8091 20b4000.ethernet-1:02: attached PHY driver [Micrel KSZ8081 or KSZ8091] (mii_bus:phy_addr=20b4000.ethernet-1:02, irq=POLL)
[ 5705.405629] IPv6: ADDRCONF(NETDEV_UP): eth1: link is not ready
Job for networking.service failed because the control process exited with error code.
See "systemctl status networking.service" and "journalctl -xe" for details.
 failed!
root@npi:~# [ 5707.448000] fec 2188000.ethernet eth1: Link is Up - 100Mbps/Full - flow control rx/tx
[ 5707.456065] IPv6: ADDRCONF(NETDEV_CHANGE): eth1: link becomes ready

Don't worry about this situation, just reboot and then you can use it.
Note: After modifying **/etc/network/interfaces**, it will take effect after restarting.

  • 3. Result test:

    Then you can ping the external network IP, but domain name resolution is not supported yet.

3. Configure DNS

1. Many attempts have been made on this DNS configuration, and there are probably the following methods.
Configure **/etc/resolv.conf**, which is what I use, but it needs to solve the problem of resetting after restarting.
Configure **/etc/sysconfig/network-scripts/ifcfg-ethx**. This method is suitable for CentOS and Redhat. Debian does not have this directory.
Configure **/etc/nework/interfaces** and specify DNS through the parameter dns-nameservers. The verification was not successful.
2. Configure /etc/resolv.conf
to configure DNS:

nameserver 114.114.114.114
nameserver 8.8.8.8

Then restart the network:

/etc/init.d/networking restart

At this time, you should be able to ping the domain name of the external network.

3. Solve the problem of resetting /etc/resolv.conf after restarting.
This problem is because **/etc/resolv.conf** is re-initialized to 127.0.0.1 when the system starts, but the initialized location cannot be found. So the curve saved the country.

(1) Create a network initialization script
networkcfg.sh

vi /root/networkcfg.sh
#!/bin/sh

sleep 3
echo "nameserver 114.114.114.114" > /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
/etc/init.d/networking restart

注:sleep 3秒是为了等 resolv.conf 文件被初始化完后再修改。 实现的就是把要配置的DNS重新写入到resolv.conf,然后重启网络。


(2) Call /etc/rc.local in the system startup script

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/root/networkcfg.sh &


exit 0

Then restart the test and you can see that DNS is effective.
注:启动脚本配置看 Debian 10 配置开机自启动脚本

Guess you like

Origin blog.csdn.net/weixin_38090079/article/details/131676737