Linux网络命令与DHCP

371 阅读9分钟

LINUX 网络

1. 网络配置命令

1.1 ifconfig

来自于net-tools包

[root@localhost ~]# rpm -qi net-tools
Name        : net-tools
Version     : 2.0
Release     : 0.22.20131004git.el7
Architecture: x86_64
Install Date: 2022年03月17日 星期四 06时14分30秒
Group       : System Environment/Base
Size        : 938986
License     : GPLv2+
Signature   : RSA/SHA256, 2017年08月11日 星期五 02时30分02秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : net-tools-2.0-0.22.20131004git.el7.src.rpm
Build Date  : 2017年08月03日 星期四 17时17分34秒
Build Host  : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://sourceforge.net/projects/net-tools/
Summary     : Basic networking tools
Description :
The net-tools package contains basic networking tools,
including ifconfig, netstat, route, and others.
Most of them are obsolete. For replacement check iproute package.

基础用法

在不带任何选项和参数执行 ifconfig 命令时, 将显示当前主机中已启用(活动)的网络接口信息

网卡名称详解:

ens33:第一块以太网卡的名称详解:

ens33中的"en"是"EtherNet"的缩写,表示网卡

类型为以太网,"s"表示热插拔插槽上的设备(hot-plug Slot),数字"33"表示插槽 编号

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.58.5  netmask 255.255.255.0  broadcast 192.168.58.255
        inet6 fe80::5917:6792:8b3b:cb06  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d7:1a:6a  txqueuelen 1000  (Ethernet)
        RX packets 358  bytes 40955 (39.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 195  bytes 23475 (22.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 408  bytes 35256 (34.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 408  bytes 35256 (34.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:6d:c2:17  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

常用格式:

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

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

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

ifconfig 网络接口 ip地址 [netmask 子网掩码] ifconfig 网络接口 ip地址[/子网掩码长度]

ifconfig ens33:0 地址 #表示虚拟网卡

[root@localhost ~]#ifconfig ens37 down
####关闭

[root@localhost ~]#ifconfig ens37 up
####开启

[root@localhost ~]#ifconfig ens36:0 192.168.100.101   ###设置临时虚拟网卡ip
[root@localhost ~]#ifconfig ens36:0
ens36:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.101  netmask 255.255.255.0  broadcast 192.168.100.255
        ether 00:0c:29:fe:e5:c9  txqueuelen 1000  (Ethernet)
        
[root@localhost ~]#ifconfig -a
####查看所有网卡包括为开启的
[root@localhost ~]# ifconfig -s    ###网络通讯情况
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500      407      0      0 0           247      0      0      0 BMRU
lo       65536      408      0      0 0           408      0      0      0 LRU
virbr0    1500        0      0      0 0             0      0      0      0 BMU

1.2 hostname

查看或设置当前主机名

hostname [主机名]

[root@localhost ~]#hostname
localhost.localdomain
### 临时生效

通过修改/etc/hostname文件来修改主机名

[root@localhost ~]# vim /etc/hostname
www.lyn.com
#### 永久生效

通过hostnamectl来修改主机名

[root@localhost ~]# hostnamectl set-hostname www.lyn.com
[root@localhost ~]# bash
[root@www ~]# hostname
www.lyn.com
### 永久生效

1.3 route

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

route [-n]

-n:将路由记录中的地址信息显示为数字形式

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.58.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.58.2    0.0.0.0         UG    100    0        0 ens33
192.168.58.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0


### 路由表主要构成:
### Destination: 目标网络ID,表示可以到达的目标网络ID,0.0.0.0/0 表示所有未知网络,又称为默认路
由,优先级最低
### Genmask:目标网络对应的netmask
### Iface: 到达对应网络,应该从当前主机哪个网卡发送出来
### Gateway: 到达非直连的网络,将数据发送到临近(下一个)路由器的临近本主机的接口的IP地址,如果
是直连网络,gateway是0.0.0.0
### Metric: 开销cost,值越小,路由记录的优先级最高

[root@localhost ~]#route add -net 192.168.100.0/24 gw 192.168.91.2
### 给192.168.100.0/24段添加静态路由
[root@localhost ~]#route -n |grep 192.168.100.0
192.168.100.0   192.168.91.2    255.255.255.0   UG    0      0        0 ens33
[root@localhost ~]#route del -net 192.168.100.0/24 
### 删除添加路由
[root@localhost ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.91.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

[root@localhost ~]#route del -net 192.168.100.0/24 
###删除默认路由
[root@localhost ~]#route -n
### 查看
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.91.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@localhost ~]#route add default gw 192.168.91.2
### 重新添加路由
[root@localhost ~]#systemctl restart network
### 需要重启服务后才能看到
[root@localhost ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.91.2    0.0.0.0         UG    100    0        0 ens33
192.168.91.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

1.4 netstat

查看网络连接情况 netstat

netstat 选项

-a:显示主机中所有活动的网络连接信息(包括监听、非监听状态的服务端口)
-n:以数字的形式显示相关的主机地址、端口等信息 
-r:显示路由表信息
-l:显示处于监听(Listening)状态的网络连接及端口信息 
-t:查看 TCPTransmission Control Protocol,传输控制协议)相关的信息 
-u:显示 UDPUser Datagram Protocol,用户数据报协议)协议相关的信息
-p:显示与网络连接相关联的进程号、进程名称信息(该选项需要 root 权限)

[root@localhost ~]#systemctl start httpd
[root@localhost ~]#netstat -ntap |grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      10379/httpd         
[root@localhost ~]#netstat -ntap |grep ":22"
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1057/sshd           
tcp        0      0 192.168.91.100:22       192.168.91.1:50728      ESTABLISHED 1707/sshd: root@pts 
tcp        0     52 192.168.91.100:22       192.168.91.1:60503      ESTABLISHED 10224/sshd: root@pt 
tcp        0      0 192.168.91.100:22       192.168.91.1:60708      ESTABLISHED 9609

1.5 ss(socket statistics)

格式: ss [选项]

ss 命令:也可以查看网络连接情况,主要用于获取 socket 统计信息,它可以显示和 netstat 命令类似的输出内容。 但 ss 的优势在于它能够显示更多更详细的有关 TCP 和连接状态的信息,而且比 netstat 更快速更高效 当服务器的socket连接数量变得非常大时,无论是使用netstat命令还是直接cat /proc/net/tcp,执行速度都会很慢。 ss快的秘诀在于,它利用到了TCP协议栈中tcp_diag。tcp_diag是一个用于分析统计的模块,可以获得Linux 内核中第一手的信息,这就确保了ss的快捷高效 ss命令是Linux CentOS 7中iproute软件包的一部分,默认已经安装。

#选项:
-t: tcp协议相关
-u: udp协议相关
-w: 裸套接字相关
-x:unix sock相关
-l: listen状态的连接
-a: 所有
-n: 数字格式
-p: 相关的程序及PID
-e: 扩展的信息
-m:内存用量
-o:计时器信息
-r: --resolve 把 IP 解释为域名,把端口号解释为协议名称。

### 可以使用time命令来比较
[root@localhost ~]# time netstat -atn
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN     
tcp        0      0 192.168.58.5:22         192.168.58.1:61670      ESTABLISHED
tcp        0      0 192.168.58.5:22         192.168.58.1:52852      ESTABLISHED
tcp6       0      0 :::111                  :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 ::1:6010                :::*                    LISTEN     
tcp6       0      0 ::1:6011                :::*                    LISTEN     

real	0m0.055s
user	0m0.000s
sys	0m0.005s

[root@localhost ~]# time ss -atn
State      Recv-Q Send-Q                                       Local Address:Port                                                      Peer Address:Port              
LISTEN     0      128                                                      *:111                                                                  *:*                  
LISTEN     0      5                                            192.168.122.1:53                                                                   *:*                  
LISTEN     0      128                                                      *:22                                                                   *:*                  
LISTEN     0      128                                              127.0.0.1:631                                                                  *:*                  
LISTEN     0      100                                              127.0.0.1:25                                                                   *:*                  
LISTEN     0      128                                              127.0.0.1:6010                                                                 *:*                  
LISTEN     0      128                                              127.0.0.1:6011                                                                 *:*                  
ESTAB      0      0                                             192.168.58.5:22                                                        192.168.58.1:61670              
ESTAB      0      0                                             192.168.58.5:22                                                        192.168.58.1:52852              
LISTEN     0      128                                                     :::111                                                                 :::*                  
LISTEN     0      128                                                     :::22                                                                  :::*                  
LISTEN     0      128                                                    ::1:631                                                                 :::*                  
LISTEN     0      100                                                    ::1:25                                                                  :::*                  
LISTEN     0      128                                                    ::1:6010                                                                :::*                  
LISTEN     0      128                                                    ::1:6011                                                                :::*                  

real	0m0.066s
user	0m0.000s
sys	0m0.003s

1.6 ping

测试网络连通性

格式:

ping [选项] 目标主机

### -c 选项是发送多少个包
[root@localhost ~]# ping -c 3 192.168.58.6   
PING 192.168.58.6 (192.168.58.6) 56(84) bytes of data.
64 bytes from 192.168.58.6: icmp_seq=1 ttl=64 time=0.552 ms
64 bytes from 192.168.58.6: icmp_seq=2 ttl=64 time=1.64 ms
64 bytes from 192.168.58.6: icmp_seq=3 ttl=64 time=1.63 ms

--- 192.168.58.6 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2025ms
rtt min/avg/max/mdev = 0.552/1.277/1.643/0.513 ms



### -w  表示等待10秒
[root@localhost ~]# ping -c 3 192.168.58.6 -w 10
PING 192.168.58.6 (192.168.58.6) 56(84) bytes of data.
64 bytes from 192.168.58.6: icmp_seq=1 ttl=64 time=0.314 ms
64 bytes from 192.168.58.6: icmp_seq=2 ttl=64 time=2.18 ms
64 bytes from 192.168.58.6: icmp_seq=3 ttl=64 time=0.434 ms

--- 192.168.58.6 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2054ms
rtt min/avg/max/mdev = 0.314/0.978/2.187/0.856 ms

1.7 traceroute

路由追踪

格式:traceroute IP地址

[root@localhost ~]# traceroute 192.168.58.5
traceroute to 192.168.58.5 (192.168.58.5), 30 hops max, 60 byte packets
 1  localhost.localdomain (192.168.58.5)  0.040 ms  0.010 ms  0.005 ms

1.8 nslookup

域名解析 nslookup

格式:nslook 域名

[root@localhost ~]# nslookup www.baidu.com
Server:		8.8.8.8
Address:	8.8.8.8#53

Non-authoritative answer:
www.baidu.com	canonical name = www.a.shifen.com.
www.a.shifen.com	canonical name = www.wshifen.com.
Name:	www.wshifen.com
Address: 103.235.46.39

                    ### dig 可以显示出更多详细信息
[root@localhost ~]# dig www.baidu.com

; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5222
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.baidu.com.			IN	A

;; ANSWER SECTION:
www.baidu.com.		402	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	83	IN	CNAME	www.wshifen.com.
www.wshifen.com.	90	IN	A	103.235.46.39

;; Query time: 85 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN:  4 03 20:15:46 CST 2022
;; MSG SIZE  rcvd: 111

1.9永久修改网络相关配置文件

网卡

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
BOOTPROTO=static(静态)            //网卡获取地址模式
ONBOOT=yes                  //开机是否自启动


IPADDR=192.168.100.100               //配置你自己的IP地址子网网关还有dns服务器
NETMASK=255.255.255.0
GATEWAY=192.168.100.2
DNS1=8.8.8.8
[root@localhost ~]# systemctl restart network         //重启网卡服务 
[root@localhost ~]# ping 192.168.100.1                 //测试网络是否通顺
PING 192.168.100.1 (192.168.100.1) 56(84) bytes of data.
64 bytes from 192.168.100.1: icmp_seq=1 ttl=128 time=0.871 ms
64 bytes from 192.168.100.1: icmp_seq=2 ttl=128 time=0.487 ms
64 bytes from 192.168.100.1: icmp_seq=3 ttl=128 time=0.347 ms


#TYPE:接口类型;常见有的Ethernet, Bridge
#PROXY_METHOD:此配置文件应用到的设备  
#BROWSER_ONLY:
#BOOTPROTO:激活此设备时使用的地址配置协议,常用的dhcp, static, none, bootp
#DEFROUTE:
#IPV4_FAILURE_FATAL:
#IPV6INIT:
#IPV6_AUTOCONF:
#IPV6_DEFROUTE:
#IPV6_FAILURE_FATAL:
#IPV6_ADDR_GEN_MODE:
#NAME:此配置文件应用到的设备
#UUID:设备的惟一标识
#DEVICE:设备名
#ONBOOT:在系统引导时是否激活此设备
#IPADDR::想设置的ip地址
#NETMASK:子网掩码
#GATEWAY:网关
#DNS1:DNS解析

域名解析配置文件

本地主机映射文件

/etc/resolv.conf

保存的是你DNS服务器的地址

/etc/hosts 文件 保存主机名与IP地址的映射记录

[root@localhost ~]# cat /etc/hosts
127.0.0.1  localhost localhost.localdomain  localhost4 localhost4. 
192.168.91.100     www.yun.com
[root@kgc ~]#ping www.yun.com
PING www.yun.com (192.168.91.100) 56(84) bytes of data.
64 bytes from www.yun.com (192.168.91.100): icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from www.yun.com (192.168.91.100): icmp_seq=2 ttl=64 time=0.065 ms
64 bytes from www.yun.com (192.168.91.100): icmp_seq=3 ttl=64 time=0.067 ms
64 bytes from www.yun.com (192.168.91.100): icmp_seq=4 ttl=64 time=0.062 ms
64 bytes from www.yun.com (192.168.91.100): icmp_seq=5 ttl=64 time=0.071 ms
64 bytes from www.yun.com (192.168.91.100): icmp_seq=6 ttl=64 time=0.130 

hosts文件和DNS服务器的比较

  • 默认情况下,系统首先从 hosts 文件查找解析记录
  • hosts 文件只对当前的主机有效
  • hosts 文件可减少 DNS 查询过程,从而加快访问速度

1.10 scp

远程同步

[root@kgc opt]#scp /opt/passwd root@192.168.91.101:/opt/passwd
              命令字 本地文件地址  对方用户名@地址冒号对面地址
[root@kgc opt]#scp root@192.168.91.101:/opt/passwd  /opt/
               命令字 对方用户名@地址冒号对面地址        本地地址

DHCP

配置文件的

#设置全局配置参数
default-lease-time 21600;   ###默认租约为6小时,单位为秒
max-lease-time 43200;       ###最大租约为12小时,单位为秒
option domain-name "benet.com";    ###指定默认域名
option domain-name-servers 202.106.0.20202.106.148.1; ###指定DNS服务器地址
ddns-update-style none;          ###禁用DNS动态更新



###subnet网段声明(作用于整个子网段,部分配置参数优先级高于全局配置参数)
subnet 192.168.80.0 netmask 255.255.255.0 {      ###声明要分配的网段地址
range 192.168.80.10 192.168.80.20;               ###设置地址池
option routers 192.168.80.254;                   ###指定默认网关地址

}
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100192.168.1.200;
option routers 192.168.1.254;


}


subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.20192.168.2.30;
option routers 192.168.2.254;


}
###host主机声明( 给单机分配固定的IP地址)
host hostname{                                  ###指定需要分配固定IP地址的客户机名称
hardware ethernet 00:c0:c3:22:46:81;            ###指定该主机的MAC地址
fixed-address 192.168.4.100;                   ###指定保留给该主机的IP地址

在 windows中 使用

ipconfig 查看网卡信息

ipcongfig /release 释放地址

ipcongfig /renew 重新获取地址

ipconfig /all 全部信息

dhcp中继

info-center source ds channel 0 log state off trap state off //4095
Undo ter m   //4096
华为打断配置
创建vlan
sys                                           //视图模式
show vlan                               //查看VLAN
vlan batch 10 20 100             //创建vlan
display vlan                           //查看vlan

int  e0/0/2
port  link-type access
Port default vlan 10
q
 
int  e0/0/3
port  link-type access
Port default vlan 20
q
int  e0/0/4
port  link-type access
Port default vlan 100
q

int e0/0/1
port link t
port trunk allow-pass vlan all

三层
vlan batch 10 20 100      //新建vlan

dhcp enable                   //开启dhcp华为默认关闭
int g0/0/1                      //进入接口
port link trunk               // 接口类型
port trunk allow-pass vlan all  //允许所有vlan通过

int vlan 10
ip add 192.168.10.1 24               //配置网关地址
dhcp  select relay                      // 配置中继
dhcp relay server 192.168.100.100   //宣告服务器地址
dis this

int vlan 20
ip add 192.168.20.1 24 
dhcp  select relay
dhcp relay server 192.168.100.100
dis this

int vlan 100
ip add 192.168.100.1 24 
dhcp  select relay
dhcp relay server 192.168.100.100
dis this

实验:模拟局域网内dhcp动态分配ip地址

1、yum install dhcp -y
2、进入centos7的网络适配器:改成仅主机模式
3、查看自己VMnet1(仅主机模式下虚拟机的虚拟网卡)的ipv4网段是什么
4、点击虚拟机上方菜单栏编辑→ 虚拟网络编辑器→ 右下角更改设置→分别选定VMnet1、VMnet8,将左下角复选框“使用本地DHCP服务将IP地址分配给虚拟机”的√ 去掉!! → 点击应用!确定!
5、vim /etc/sysconfig/network-sctipts/ifcfg-ens33
           
           ONBOOT=yes
           IPADDR=192.168.100.100 (这里的网段是第3步的网段,查看自己的VMnet1) 
           NETMASK=255.255.255.0
           GATEWAY=192.168.100.1    
           #DNS1=8.8.8.8
           
6、systemctl restart network
7、vim /etc/dhcp/dhcpd.conf

           subnet 192.168.100.0 netmask 255.255.255.0 {
           range 192.168.100.11 192.168.100.50;
           option routers 192.168.100.1;
           }

8、systemctl start dhcpd
            systemctl status dhcpd

9、将windows网络设配器也改成仅主机  cmd命令:ipconfig  查看是否已动态获取
            ipconfig /release   ###释放当前IP地址
            ipconfig /renew     ###获取IP地址