Linux network commands and DHCP

1. Network configuration commands

1.1ifconfig

Insert image description here

ifconfig  具体网卡名称      #只显示具体网卡的详细信息(无论该网卡是否使用)

Insert image description here

ifconfig  -a      #表示显示所有网卡包括没有启动的网卡

Insert image description here

ifconfig   网卡名称   [up|down]   #表示开启或关闭网卡

Insert image description here
Insert image description here

ifconfig ens33:0 192.168.91.200设置虚拟网卡

Insert image description here

ifconfig ens33:0查看虚拟网卡的详细信息

Insert image description here

关闭虚拟网卡 ifconfig ens33:0 down 

Insert image description here

#网络通讯情况    ifconfig -s

Insert image description here

配置网卡信息的命令:vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
#接口类型
BOOTPROTO=static
#地址获取方式 手动配置    dhcp 动态获取
NAME=ens37
#网卡名称
DEVICE=ens37
#网卡
ONBOOT=yes
#开机是否自启 开启
IPADDR=192.168.190.100
#地址
NETMASK=255.255.255.0
#子网掩码
GATEWAY=192.168.190.2
#网关
DNS1=8.8.8.8
#将ip地址转换成 域名

1.2 hostname

查看或设置当前主机名  hostname [主机名] //此为临时修改,关机后并不会保存

Insert image description here

通过修改/etc/hostname文件来修改主机名
[root@localhost ~]# vim /etc/hostname
#永久生效

Insert image description here

通过hostnamectl来修改主机名[root@localhost ~]# hostnamectl set-hostname www
[root@localhost ~]# bash
[root@www ~]# hostname
www.bdqn.com
#永久生效

1.3route

查看或设置主机中路由表信息

Insert image description here

route  -n

Insert image description here
#The main components of the routing table are:

  • #Destination: Destination network ID, indicating the reachable destination network ID, 0.0.0.0/0 represents all unknown networks, also known as the default route, with the lowest priority
  • #Genmask: netmask corresponding to the target network
  • #Iface: When reaching the corresponding network, which network card of the current host should send it out?
  • #Gateway: Reach the non-directly connected network and send the data to the IP address of the interface adjacent to the host on the adjacent (next) router. If it is a directly connected network, the gateway is 0.0.0.0
  • #Metric: Cost, the smaller the value, the highest priority of routing records
添加 指定网段的路由记录

route add -net network segment address gw IP address
Insert image description here

删除指定网段的路由记录

route del -net network segment address
Insert image description here

向路由表中添加默认网关记录

route add default gw IP address
Insert image description here

删除路由表中默认的网关记录

route del default gw IP address
Insert image description here

2. Management port

2.1netstat:

Port scan to see if the port is working properly

常用格式netstat -antp表示显示所有端口以数字方式tcp连接相关的进程信息

Insert image description here

netstat选项:
a所有信息
n以数字方式显示信息
r显示路由表信息
l显示处于监听状态,网络连接和端口信息(监听状态标识建立通信端到端可以通信)
t显示tcp协议
u显示udp协议
p显示与网络链接相关的进程号,进程名称信息(必须有root权限)

netstat -antp | grep tcp View information about tcp
Insert image description here

2.2ss

Obtain the communication socket and socket information. The usage is basically the same as netstat
ss option:

a所有信息
n以数字方式显示信息
r显示路由表信息
l显示处于监听状态,网络连接和端口信息(监听状态标识建立通信端到端可以通信)
t显示tcp协议
u显示udp协议
p显示与网络链接相关的进程号,进程名称信息(必须有root权限)
x表示内核相关

Commonly used commands ss -antp
Insert image description here
ss -antp |grep ssh specifies searching for ssh related information
Insert image description here

2.3 The difference between netstat and ss

  • 1.ss is faster than netstat
  • 2. In scenarios where ss uses a large connection, netstat may freeze.

3. Network communication

ping:在linux表示长ping,会一直ping,window是默认5次

Insert image description here

ping -c+数字 表示指定次数

Insert image description here

ping -W表示等待响应时间,等待之后继续ping
ping -w表示等待超时时间,超时之后不再继续ping

4、traceroute

测试当前主机到目的主机网络节点
 traceroute查看路径(类似window的tracert命令)

The tracert command
Insert image description here
traceroute 8.8.8.8 of the window can see that there are thirty points.
Insert image description here

5. Application of DHCP in real-life scenarios

将虚拟机A作为DHCP服务器,给另一台虚拟机B分配地址,使虚拟机B能够正常联网

First turn off the local DHCP service of the virtual machine
Insert image description here

第二步,在主机A上采用yum安装DHCP

Insert image description here

第三步查看DHCP安装的详细信息

Please add image description

第四步,进入DHCP安装的配置信息文件中

Please add image description

第五步,进入该文件后,显示当前路径下的文件配置信息存放于  /user/share/doc/dhcp*/dhcp.conf.example中

Insert image description here

第六步,将/user/share/doc/dhcp*/dhcp.conf.example下的配置文件信息拷贝到 /etc/dhcp/dhcpd.conf/中

Insert image description here

第七步,进入/etc/dhcp/dhcpd.conf中,更改其配置文件

Insert image description here
Insert image description here

第八步 ,重新启动DHCP

Insert image description here

第九步,查看dhcp使用的端口是否打开

Insert image description here

最后一步,打开一台新的pc,发现其能通过刚才设置的DHCP连通网络。

Please add image description

6. Achieve time synchronization in simulated intranet environment

yum -y install chrony   //安装chrony
rpm -qc chrony   //查看chrony配置文件所在位置

Insert image description here

vim /etc/chrony.conf  //进入配置信息文件

Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/m0_62231324/article/details/132059393
Recommended