Ubuntu重启后如何配置网络

问题:重启Ubuntu18.04后网卡配置丢失


解决办法参考链接1:https://blog.csdn.net/weixin_37813152/article/details/122244742
解决办法参考链接2:https://blog.csdn.net/lsc_1893/article/details/118696693


首先我们ifconfig是看不到网卡的

(base) hht@star-X11DAi-N:~$ ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12017  bytes 65338825 (65.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12017  bytes 65338825 (65.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

但是通过ifconfig -a是可以看见网卡的,只是没有RUNNING,因此需要通过ifconfig eno1(网卡名) up启用网卡。
(启动网卡后仍然没有IP地址)

ifconfig -a
ifconfig eno1(网卡名) up
(base) hht@star-X11DAi-N:~$ ifconfig -a

'这里是没有配置的示例'
eno1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 3c:ec:ef:8c:9e:48  txqueuelen 1000  (Ethernet)
        RX packets 609  bytes 208278 (208.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
'这个是已经配置好的示例'
eno2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.60  netmask 255.255.254.0  broadcast 192.168.31.255
        inet6 fe80::3eec:efff:fe8c:9e49  prefixlen 64  scopeid 0x20<link>
        ether 3c:ec:ef:8c:9e:49  txqueuelen 1000  (Ethernet)
        RX packets 504967  bytes 53105575 (53.1 MB)
        RX errors 0  dropped 1229  overruns 0  frame 0
        TX packets 32182  bytes 35464830 (35.4 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12023  bytes 65371030 (65.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12023  bytes 65371030 (65.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

解决办法:

1 修改interfaces中配置:

sudo vim /etc/network/interfaces

vim后写入以下配置:

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

#allow-hotplug eno2
auto eno2
iface eno2 inet static
 address 192.168.30.95  'IP地址'
 netmask 255.255.254.0	'子网掩码'
 gateway 192.168.30.1	'网关'
 broadcast 192.168.31.255	'广播地址'

up route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.30.95 dev eno2

'多张网卡就写多个auto eno和多个路由配置'

~

使网卡获取得到ip地址:

dhclient eno2

2 设置DNS解析(解决resolve.conf被覆盖问题)

2.1 修改resolve.conf中配置:

!注意:这里不能直接修改/etc/resolve.conf(因为这个是软连接的原因),因此需要修改的是/etc/systemd/resolved.conf

sudo vim /etc/systemd/resolved.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
DNS=8.8.8.8   "主要就是修改这里!!!"
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
~ 

2.2 重启域名解析服务

systemctl restart systemd-resolved
systemctl enable systemd-resolved

2.3 备份当前的/etc/resolve.conf

重新设置/run/systemd/resolve/resolv.conf/etc/resolve.conf的软链接:

mv  /etc/resolv.conf  /etc/resolv.conf.bak
ln  -s  /run/systemd/resolve/resolv.conf  /etc/

2.4 检验是否备份成功

cat /etc/resolv.conf

查看是否出现如下配置:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 8.8.8.8
nameserver 114.114.114.114

之后网络就成功恢复了~

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值