Linux self-study journey-basic commands (commonly used network commands to configure a static IP address-basic concepts of bridge mode, NAT mode, host-only mode, and IP address)

Linux self-study tour-basic commands (commonly used network commands to configure static IP addresses and the concept of various network modes)


Preface

1. In the previous section, we talked about some shutdown and restart commands under Linux. If you haven't read it, you can click the link below to watch: shutdown and restart commands

2. Starting from this section, we will talk about some commonly used commands in the network. In this section, we will first talk about how to configure a static IP address.


Tip: The following is the content of this article, the following cases are for reference

1. Before manually configuring the IP address

1. VMware's network adapter

1. Remember the network adapter in the device we checked when we created a virtual host
Insert picture description here


2. It depends on how we access the network, that is, through the adjustment of this network adapter, we can adjust our Internet posture, we click Go in and have a look!
Insert picture description here


3. We can see that there are 5 clickable buttons, let's look at the first three first, that is, through these three buttons we can adjust our posture for accessing the network.

2. Bridge mode

1. The bridge mode can be simply understood as: assign an IP address that is in the same network segment as your physical network card, and allow the virtual machine to access the Internet like your physical network card.

**Physical network card: It can be understood as your computer's own network card. We are now using a virtual machine, but our virtual machine must be installed on a real computer, so the physical network card is your own virtual machine installed Network card of your computer.

**Network segment: It can be understood as a network range you are currently in, and the same network segment can be understood as your own real function is on Baidu. As long as I pull my IP address into your network segment range, then I can also Check Baidu on the Internet with you.

(The simple understanding is that if you choose the bridge mode, your virtual machine will be the same as your real physical network card. Your real physical network card can check Baidu on the Internet, then you can also access Baidu in the virtual machine.)

3. NAT mode (this mode will be used in the demonstration)

1. The NAT mode can be simply understood as: Assign an address on the same network segment as your physical network card with the bridge mode to ensure that the virtual machine and the real machine can access the Internet differently. The NAT mode can assign addresses of different network segments arbitrarily, but in the Internet At that time, it will convert its own address into an address that can be used to access the Internet, that is, your address in NAT mode does not need to be a network segment address with the real machine, but if you want to access the Internet, such as accessing Baidu, it will convert its own address to ensure that you can The address of the Internet can also ensure that you can access the Internet just like a real machine.

4. Host only mode

1. This is very simple and rude. If you can’t access the Internet, such as accessing Baidu, you just shut yourself out. The simple understanding is: create a group, and put the people you want to contact into this group. We can contact each other. If you do not contact, you will not be able to access the Internet like a real machine if you choose this mode, and you can only contact with your own machine in host mode in private.

5.IP address

1. IP addresses can be simply divided into public IP addresses and intranet IP addresses . How do you understand?

Public IP address: Xiaoming is in Yinhai District, Beihai City, Guangxi Zhuang Autonomous Region, and Xiaoli is in Yuelu District, Changsha City, Hunan Province. Now Xiaoming wants to send express to Xiaoli, so the question is, how can Xiaoming judge that Xiaoli is in Changsha, Hunan Province Where is Yuelu District? At this time, the usefulness of the public network IP address comes out. In the Internet, we can use your public network IP address to determine which province and city area you are currently in, so if Xiaoming knows Xiaoli’s public network IP address, Xiaoming It will be "Oh, look at this string of public network IP, you can see it from Hunan." Simply put, it can be seen that you can roughly tell which area you are in by what public IP address you use. Checking the public IP on the Internet can show the corresponding area.

2. But knowing the public IP address is not enough. Think about it, Xiaoming knows that Xiaoli is in Yuelu District, Changsha City, Hunan Province, but is this enough? If you send a courier, you only need to be accurate to Yuelu. The courier will know who you want to send to? So we need another intranet IP address.

Intranet IP address: This is easy to understand. Xiaoli is in Jinnan Community in Yuelu District (assuming the community name), then the conditions are met. Xiaoming already knows Xiaoli’s public network IP (region), and Intranet IP (where you live), can you send it by express?

3. Of course, this example is barely passed. In the end, we will briefly describe that if we want to communicate between multiple hosts, we need an IP address, and then the IP address is divided into public IP and intranet IP. When we give a remote When a host in an unknown area sends information, we must know the public IP of this host, so that we can ensure that the data can be sent to the area where he is located, and then after the data is sent to that area, the public IP will be converted to An internal network IP, and then the internal network IP can be accurate to which cell the host is located. (Speaking of conversion, did you think of the previous NAT mode, haha, actually this NAT technology is used)

(It is mentioned here that the public IP address is generally used to access the Internet, that is, if you want to go online to Baidu, you must have the public IP, but the public IP is costly to buy; and the internal IP is only used for internal use. Is inside a local area network)

2. Manually configure the IP address (intranet IP address)

1. We have finished the basic concept of IP address before, and then I said that my demo network adapter chooses NAT mode, so now let's actually configure the IP address in Linux. (The following configurations are all internal network IP addresses, public network IP addresses need to be purchased with money, we can play the internal network casually)

2. Since we chose the NAT mode, let's take a look at the IP network segment assigned to us by the selected NAT mode. That is to say, if you choose any mode, the system will assign you an IP network segment, we modify it manually If the IP address is changed to any IP address in this network segment, it will do.

3. First, let's take a look at the network segment range allocated to us by the NAT mode. Click in the virtual network editor as shown below.
Insert picture description here

4. After clicking in, find the entry of type NAT mode. After clicking, you will see the subnet IP and subnet mask below. This is the network segment assigned to us by our NAT mode. Of course, my address and everyone’s address may be It’s not the same. You can modify it. Just click on the bottom right to change the settings. If you have learned the Internet, you can modify it yourself. If you haven’t learned it, you can modify it to be the same as mine, because I will only use my own IP. Operation, please solve the difference by yourself or modify it to be the same as mine. (Remember to apply after the modification)
Insert picture description here
5. I only explain what my subnet IP and subnet mask are. For more details, please learn network knowledge by yourself. CCNA (a network certification) knowledge is enough.

Subnet IP: 192.168.137.0; Subnet mask: 255.255.255.0 means that the network segment where I am in NAT mode is 192.168.137.0, and the intranet IP addresses that can be used in this network segment are 192.168.137.1–192.168.137.254 , (Note here, 192.168.137 is not moving, only the following .0 is changed, that is, you can change this 0 to any number between 1-254)

6. So now that we have chosen the NAT mode and understand the IP network segments that can be allocated, let's actually operate the machine.

[root@dns ~]# vim /etc/sysconfig/network-scripts/ifcfg-

(vim我们没讲,修改IP地址也有很多方法,不过我个人最喜欢用的就是以上方法)
(以上代码大概可以理解为修改一个保存了你系统的IP地址的配置文件,我们就是要修改这个配置文件中的IP地址)
(不过复制以上代码之后,先别急着回车,先按两下Tab键,然后会出现以下选项)
[root@dns ~]# vim /etc/sysconfig/network-scripts/ifcfg-
ifcfg-ens33      ifcfg-ens33.bak  ifcfg-ens37      ifcfg-lo
[root@dns ~]# vim /etc/sysconfig/network-scripts/ifcfg-

(大家应该知道要修改IP地址的话就相当是修改网卡的地址)
(我让大家Tab两下的原因就是怕大家的系统版本和我不一样,所以可能网卡名字不一样)
(我的是Centos7.6,所以第一个ens33是我的网卡,大家如果是Centos6的话应该是eth0)
(一般情况下第一个就是你的网卡,总之大家注意一下,实在不知道大家可以网上查一下对应Centos版本的网卡名)
[root@dns ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

(那么我直接编辑ifcfg-ens33这个文件)

7. After you press Enter, you will enter this interface. It doesn't matter if you don't understand it as a beginner. What we mainly care about is to modify the intranet IP address. Leave the rest alone, pay attention to the two parts where I drew the yellow line. If you open this configuration file and there are these two entries, you can directly modify the values ​​inside to change the IP address. If you don’t have these two entries, please add them yourself. Go in, but if you haven’t learned vim here, I don’t need to add it by yourself, you can copy the following code (if there is an ip address, because I didn’t say vim, so you can modify it by Baidu in another way, one is It is very quick to modify through the graphical interface, I am talking about the situation without an IP address)

perl -p -i -e"s/dhcp/static/g" /etc/sysconfig/network-scripts/ifcfg-ens33
(这段代码要修改的地方就是ifcfg-ens33,请修改为自己的网卡)

echo -e "IPADDR=192.168.137.25\nNETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-ens33

(这段代码可以理解为将一段信息写入一个配置文件中)
(这段代码也有要修改的地方,第一个就是IPADDR那边,还记得我们的网段嘛,
这里大家可根据自己前面所查看的网段自行修改,因为我是192.168.137.0,
我讲后面的0可以修改为1.254,这里我改为25,大家不同的可以自行修改这里)
(第二个要修改的地方就是代码后面的ifcfg-ens33,这里是网卡,大家请修改为自己的网卡)

Insert picture description here

8. So if we modify the IP address, how can we view the modified IP address?

[root@dns ~]# ifdown ens33;ifup ens33

(这个代码的意思是断开ens33这个网卡的链接然后在打开ens33的链接,相当于重启网卡)
(要修改的地方是ens33,改成你自己网卡名)

[root@dns ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:98:5a:a6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.137.25/24 brd 192.168.137.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::9fa5:329f:c7ff:b80f/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

(这个代码的意思就是查看ens33这块网卡的信息)
(要修改的也是ens33,修改成自己的网卡)
(出来的内容很多别怕,我们找到inet的关键字,这里可以利用管道命令)

[root@dns ~]# ip addr show ens33 | grep inet
    inet 192.168.137.25/24 brd 192.168.137.255 scope global noprefixroute ens33
    inet6 fe80::9fa5:329f:c7ff:b80f/64 scope link noprefixroute

(加了管道和grep也很好理解吧,我们只想查看包含inet这个关键字的行)
(第一行就很清楚了,192.168.137.25就是我们自己修改的内网IP地址,也就是我这台虚拟主机的内网IP地址)

to sum up

It's over because of time. You can learn more about it on Baidu for details. I just mention it briefly here. So we learned in this section

{ 1. The three network modes of vmware: bridge mode and NAT mode and host-only mode. The bridge mode and NAT mode can access the Internet, while the host-only mode can only be used internally

2. The basic concept of IP address: public network IP is used to access the external network, and internal network IP is used for internal communication

3. Manually configure the IP address and view the IP address under Linux
}

This is Jiehua, see you next time!

Guess you like

Origin blog.csdn.net/qq313088385/article/details/114831362