2023.5.12 Solve the problem that ens33 has no ip in Ubuntu

Ens33 in Ubtuntu has no ip

If the Ubuntu version is too high

sudo netplan apply

If Ubuntu 16.04 and earlier

sudo vi /etc/systemd/resolved.conf

The specific situation is shown in the figure below

image-20230512082505417

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 00:0c:29:ba:af:1e brd ff:ff:ff:ff:ff:ff
    altname enp2s1

According to the information prompt

The ens33 interface status is "DOWN", which means it is currently disabled and may not be properly configured or started. You need to make sure the ens33 interface is enabled and configured with an IP address.

You can enable the ens33 interface with the following command:

sudo ip link set ens33 up

You can then configure the IP address with the following command:

sudo ip addr add <ip_address>/<subnet_mask> dev ens33

Regarding the selection of ip, if you use NAT mode, open the cmd window of the local computer (win + R)

image-20230512083305710

enter

ipconfig

Find VMnet8, select the appropriate IPv4 (192.168.220.***)

image-20230512083403949

For example, if your IP address is 192.168.220.200 and your subnet mask is 255.255.255.0, you can use the following command:

sudo ip addr add 192.168.220.200/24 dev ens33

Next, you can use the following command to check that the IP address is configured correctly:

ip addr show ens33

If all is well, you should be able to see that the IP address for the ens33 interface has been correctly configured.

image-20230512083654299

Also, if you have a newer version of Ubuntu, you may have a network interface naming scheme like “ens33” instead of the previous “eth0” naming scheme. This is because Ubuntu introduced a new predictive naming scheme for network interfaces. If your network interface name is not “ens33”, you will need to change the interface name in the command accordingly.

Next, restart the network. If you use Network Manager to manage network connections, you can use the following command to restart the Network Manager service:

sudo service network-manager restart

1. Unable to ping www.baidu.com

hadoop@ubuntu:~/Desktop$ ping www.baidu.com
ping: www.baidu.com: Temporary failure in name resolution

image-20230512084622406

According to the information, the ens33 network interface was successfully configured and assigned an IP address. However, the ping command cannot resolve the domain name, which may be caused by a problem with the DNS settings.

You can try to solve this problem by following steps:

  1. Check your DNS settings are correct

DNS server addresses can be added using the following command:

sudo vi /etc/systemd/resolved.conf

Add the following line to the file, replacing the DNS server address with the DNS server address you want to use:

nameserver 8.8.8.8

This will use Google’s public DNS servers. You can also use other DNS servers, such as those provided by your ISP.

image-20230512085425664

After saving and closing the file, restart the systemd-resolved service for the changes to take effect:

sudo systemctl restart systemd-resolved.service

You can use the resolvectl status command to check whether DNS has been configured successfully.

sudo resolvectl status

image-20230512085929900

2. Still can’t ping, consider the problem of DNS, choose public DNS

Open the /etc/systemd/resolved.conf file:

sudo vi /etc/systemd/resolved.conf

Comment out the DNS line and add a line DNS=114.114.114.114, namely:

#DNS=8.8.8.8
DNS=114.114.114.114

image-20230512091010806

Save and close the file.

Restart the systemd-resolved service:

sudo systemctl restart systemd-resolved

Confirm that the new DNS server address has taken effect:

resolvectl status

You should now see Current DNS Server: 114.114.114.114.

3. Forgot to set the gateway

In older versions of Ubuntu (such as Ubuntu 16.04 and earlier), you can configure network interfaces and gateways in the /etc/network/interfaces file. You need to edit the file and add the following lines:

auto eth0
iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1

In the above example, eth0 is the name of the network interface, address is the IP address of the interface, netmask is the subnet mask, and gateway is the default gateway. Please modify according to your network settings.

If you are using a newer version of Ubuntu (such as Ubuntu 18.04 and later), use netplan to configure network interfaces and gateways. In this case, you need to edit the /etc/netplan/*.yaml file and add the following line:

sudo vi /etc/netplan/*.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses:
        - 192.168.220.200/24
      gateway4: 192.168.220.1
      nameservers:
          addresses: [114.114.114.114]

In the above example, eth0 is the name of the network interface, addresses is the IP address and subnet mask of the interface, gateway4 is the default gateway, and nameservers is the IP address of the DNS server.

Note that the netplan filename ends in .yaml and you need to run the sudo netplan apply command to apply the changes.

image-20230512092416122

I found the problem. The reason is that I used /etc/netplan/*.yaml to configure the ip before, but I used sudo ip addr add 192.168.220.200/24 dev ens33 to configure it. The two ips are different, causing confusion

4. Using sudo ip addr add conflicts with /etc/netplan/*.yaml

If the IP address you set manually using the sudo ip addr add command is inconsistent with the IP address configured in the /etc/netplan/*.yaml file, it may cause confusion and errors in the network configuration. This is because the Ubuntu system will first read the netplan configuration file at startup and use the configuration information in it to set up the network interface. If you manually changed the IP address without updating the netplan configuration file, then the system will use a different IP address on the next boot, which may cause network failures or connectivity issues. Therefore, it is recommended that you only change network settings in the netplan configuration file to ensure consistency and correctness. If you need to change the IP address or other network settings, this is best done by modifying the netplan configuration file.

Modify the ip gateway to make it consistent

image-20230512100013772

If using netplan to configure networking in Ubuntu 18.04 or later, when you modify the /etc/netplan/*.yaml files, you need to apply the changes for them to take effect. To do this, run the following command:

sudo netplan apply

This will reload the network configuration and apply the changes. No need to restart any services.

4. The display of ip a is different from /etc/netplan/*.yaml

image-20230512103134618

It can be found that the network connection is successful, but the ip does not seem to be set by us

sudo vi /etc/netplan/*.yaml

image-20230512103311036

Add dhcp4: no # Disable DHCP

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    ens33:
      addresses:
        - 192.168.220.200/24 # Please replace with the IP address and subnet mask you want to use
      dhcp4: no # disable DHCP
      gateway4: 192.168.220.1 # Please replace with your default gateway
      nameservers:
              addresses: [114.114.114.114] # Please replace with the DNS server address you want to use

application configuration

sudo netplan apply

restart service

sudo systemctl restart NetworkManager