How to configure network card in ubuntu

common problem

1. Why configure network card

Because if the network card is not configured, it will not be able to access the Internet or communicate with other computers

2. Common mistakes are:

An error occurs when pinging any address

network is unreachable

Or an error was reported after entering the restart command /etc/init.d/networking restart

[…] restaring networking(via systemctl): networking.serviceJob for networking.service failed because the control process exited with error code. See "systemctl status networking.service“ and “journalctl -xe” for details.
failed!

Or you can’t find your network card (eth0) when using ifconfig, and it shows your network card but no address when ipconfig -a

Insert picture description here

Insert picture description here

So how to configure it

1. First of all, we have to determine whether to configure a static IP or a dynamic IP mode.

Dynamic IP will redistribute the IP each time it connects to the network. This setting method is generally suitable for configuration when connecting to the router, because the router will assign IP to the connected device. If it is not set to dynamic, it may cause the network to be unable to access .
Static IP will fix this IP address, and use the same address every time you access the network. This setting is suitable for two computers directly connected, or multiple computers connected through a switch.

2. Modification of the configuration file

The location of Linux configuration files is in /etc/network/interfaces. Then we enter the following command

sudo vi /etc/network/interfaces

Generally speaking, it looks like Insert picture description here
this when opening a new file . We don't need to change these lines.
We will continue to write the following code later:

Static IP

auto eth0
iface eth0 inet static
address 192.168.0.1
netmask  255.255.255.0
gateway  192.168.0.1

Notes:

auto eth0 # means to start the network card, eth0 is the name of the network card, different computers may be different
iface eth0 inet static #Set to static
address 192.168.0.1 #Set the IPv4 address (define a 192.168.0.XXX by yourself, other host addresses can conflict)
netmask 255.255.255.0 #Subnet mask (you can set the same for different hosts)
gateway 192.168.0.1 #Gateway (define a 192.168.0.XXX by yourself, which does not conflict with Ipv4, you can use it by default----! )

Dynamic IP

auto eth0
iface eth0 inet dhcp

3. Restart the network

/etc/init.d/networking restart

If no error is reported at this time, then there is no problem with the configuration file. Call at this time

ifconfig

You can see that the network card eth0 is added and it succeeds.
Insert picture description here

If you have any questions, please leave a message.
If you are helpful, you can like it~

Guess you like

Origin blog.csdn.net/gg864461719/article/details/109427953
Recommended