Detailed explanation of linux modifying ip address

Modifying the IP address is divided into temporary modification (still belonging to dynamic allocation) and permanent modification (setting static ip). Temporary modification is to use the ifconfig command to modify directly, but after a period of time, or restart, it will be dynamically redistributed. The permanent modification is to directly write the ip in the configuration file, which will never change.

Temporary modification

First, first check the ip address and the network card used

ifconfig

insert image description here
You can see that the eth0 network card is used, and the ip address is: 192.168.8.10.

2. Change the ip address to 192.168.8.66 (the network segment must be the same as that of the virtual machine before modification, and the ip address is not occupied by others)

modify temporary ip

ifconfig eth0 192.168.8.66/24	

Configure default routing

route add default gw 192.168.8.1	

insert image description here
We will find that the second command (set the gateway) cannot be executed, because the relevant configuration of the eth0 network card is not set in the configuration file, and the gateway cannot be accessed.
insert image description here

Open the interfaces file first

leafpad /etc/network/interfaces

Add two lines at the end:

auto eth0
iface eth0 inet dhcp

Note: dhcp means automatic

The modification is as follows: insert image description here
After modifying this file, you need to restart it (you can modify the ip in the future and restart the service directly).

reboot

Because the ip address is temporarily modified, the ip address becomes 192.168.8.6 after restarting,
insert image description here
and then enter the following command again:

ifconfig eth0 192.168.8.66/24     #配置临时ip
route add default gw 192.168.8.1           #配置默认路由

At this time, the ip is successfully modified, and the external network can be accessed.

insert image description here

permanent modification

Here it is changed to 192.168.8.56 (the network segment must be the same as that of the virtual machine before modification, and the ip address is not occupied by others)

Modify the configuration file

vim /etc/network/interfaces

Add the following code:

auto eth0
#静态设置ip
iface eth0 inet static	
#设置ip地址
address 192.168.8.56	
#设置子网掩码
netmask 255.255.255.0	
#设置网关
gateway 192.168.8.1		

insert image description here
restart network card

systemctl restart networking

insert image description here

After setting the static ip address, the NAT mode and the bridge mode switch, resulting in the inability to access the Internet

There is a pitfall in modifying the static ip address here. If the mode is switched after the ip address is fixed, it will cause inability to access the Internet.

We know that in most cases, the network segments of nat mode and bridge mode are different. Take my virtual machine as an example:

Bridge mode :

Network segment: the same as the host network segment.

The network segment is: 192.168.8.*

insert image description here

NAT mode :

You can see that the network segment in NAT mode is: 192.168.70.*
insert image description here
So after switching modes, you need to modify the configuration file again.


Please correct me if there are mistakes or inappropriate descriptions.

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/127453936