[Raspberry Pi 4B] 3. The laptop provides network for Raspberry Pi Ubuntu 18.04|Why does Ubuntu 18.04 change /etc/resolv.conf and modify the nameserver and restart the network and the network is reset?

One, condition:

The notebook must have dual network cards, one wireless and one Ethernet, which is generally the case for current notebooks.

2. Notebook configuration

Turn on network sharing

Open the control panel, control panel\network and Internet\network connection, as shown in the figure below:
Insert picture description here

  1. Click the WLAN adapter -> right click -> properties -> sharing -> check the port to allow other network users to connect through this computer's Internet connection (N) -> home network connection (H): -> Ethernet 2 (Raspberry Pi Network card connected by network cable)

Insert picture description here

Get the IP address of the interface between the notebook computer and the Raspberry Pi

Check in the console, ipconfig, as shown below, because the adapter connected to the Raspberry Pi is Ethernet 2, so for the
IPv4 address of the interface ........: 26.26.26.1
subnet mask... .......: 255.255.255.248
Insert picture description here
Now the network on the laptop side is configured. Why is the interface 192.168.137.1? Because the upper part has automatically set the "local connection" IP address to static IP 192.168.137.1 when sharing the Internet , of course, this IP address can also be set to other commonly used static IP addresses.

Fourth, the configuration of the Ubuntu system on the Raspberry Pi

The laptop here is equivalent to a router. In order for the Raspberry Pi to access the Internet via Ethernet, the IP address of the Raspberry Pi must be set. I set a static IP. The IP setting requirement is to be in the same network segment as the IP of the interface between the laptop and the Raspberry Pi (actually a gateway) . From the above information, it is easy to know that the address range of the Raspberry Pi is192.168.137.1-254/24

Configure Ubuntu static IP

Let’s set the IP address of the Ubuntu 18.04 system on the Raspberry Pi 4B as static:

#打开相应的网络配置文件
sudo vi /etc/network/interfaces

#填入
auto eth0 
iface eth0 inet static
address 192.168.137.10      #这里设置IP,主要在上面给出的范围内即可
netmask 255.255.255.0         #这是子网掩码的设置
gateway 192.168.137.1         #这是电脑与树莓派的接口地址,即网关地址
dns-nameserver 8.8.8.8 #dns域名解析,可以更换为其他的,比如8.8.8.8 119.29.29.29
#退出并且保存
按下ESC
:wq

Insert picture description here

Configure DNS server

After the above steps, and then restart the network: sudo /etc/init.d/networking restartyou can go online, but I remember that the DNS server configuration in the previous file seems to be invalid. They said that after the above settings are completed and the computer is restarted, the nameserver will be automatically added to the /etc/resolv.conf file 8.8.8.8, but this is not the case when I actually do it. Maybe the blog is too long and it is a little different from the current version (Ubuntu 18.04). In this way, the domain name cannot be resolved, but the IP address can only be pinged. So the configuration method is different.

Correct posture:

#配置解析文件
sudo vi /etc/systemd/resolved.conf

The configuration is as follows:
Insert picture description here
and modify the DNS column, set the addresses of multiple DNS servers, so that the DNS modification is completed, save and exit, no need to restart the system, I don’t know why some blogs say that it needs to be restarted, just restart the network service. .
Restart network service : sudo /etc/init.d/networking restart
In fact, there is restart network:, sudo service networking restartbut I use the above one. I don't know the difference for now.

pit

Of course, I still have to talk about some of the pitfalls. I changed this file because the domain name could not be resolved, so I searched it. Some blogs may be because they said that the system version is the previous or it is actually CentOS. Some questions.
There are many blogs that say to change sudo vi /etc/resolved.conf, and then restart the network service after changing this file in the Ubuntu 18.04 system, you will find that this file has changed back again, as shown in the figure below:
Insert picture description here
Let me talk about it first, I don’t know about other versions of the system, but What has changed in Ubuntu 18.04 is /etc/systemd/resolved.confnot sudo vi /etc/resolv.conf. I later verified that it was to modify this file. I commented out the DNS line and restarted the network service. Then I went to ping www.bilibili.com and it didn't work. After the restoration, it worked.Insert picture description here

Then the students who didn’t know went to search at this time:
Modify /etc/resolv.conf and restore to the original state
Then there is a blog saying that you can /etc/sysconfig/network-script/ifcfg-eth0add a content inside PEERDNS=no, but you will find that this file does not exist at all, and you don't need to create a new one, because there is no /systemconfig directory under the /etc directory. In short, this is useless. This may be the CentOS method. Although it is all Linux, there are many differences.

Of course there seems to be chattr +i /etc/resolv.conf ~this command -i: Do not change files or directories arbitrarily. I don't know if it works, try it yourself.

Here
https://blog.csdn.net/u012432611/article/details/47702939

I don’t know the specific reason. I have seen that the resolv.conf file is actually a Link file , as follows.
Insert picture description here
The address of this blog is: Ubuntu on the issue of revising the resolv.conf restart failure
**Original: **The following method I There is no way to try, but I think the reason is reasonable.
There is a resolvconf service in Ubuntu , which is used to control the contents of /etc/resolv.conf. So once we restart the system or the service, the content in the /etc/resolv.conf file will be restored to the original content. So directly modifying /etc/resolv.conf will not solve this problem.

Solution:

1. Define the DNS address in the configuration file of the network card:

iface eth0 inet static

address 192.168.0.10

netmask 255.255.255.0

gateway 192.168.0.1

dns-nameserver 8.8.8.8 8.8.4.4

2. Modify the configuration file of the resolvconf service:

vim /etc/resolvconf/resolv.conf.d/base

Add or modify in it:

nameserver 8.8.8.8 8.8.4.4

Guess you like

Origin blog.csdn.net/qq_42820594/article/details/107325437