目录
一 查看网络配置
1、ifconfig命令
ifconfig 查询所有活动的网络设备
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
该口已启用 支持广播、正在运行、支持组播 最大传输单元1500字节
inet 192.168.40.126 netmask 255.255.255.0 broadcast 192.168.40.255
inet6 fe80::4521:124d:9706:406c prefixlen 64 scopeid 0x20<link>
ipv6地址 子网长度 作用域 link表示仅该接口有效
ether 00:0c:29:02:e6:68 txqueuelen 1000 (Ethernet)
mac地址 传输队列长度(传输缓存大小) 接口类型
RX packets 415 bytes 40619 (39.6 KiB)
接收 报文个数 总和字节数
RX errors 0 dropped 0 overruns 0 frame 0
接收 错误 丢弃 溢出 冲突帧数
TX packets 431 bytes 44281 (43.2 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 316 bytes 27428 (26.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 316 bytes 27428 (26.7 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:76:02:0a 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 -a 查询所有的网络设备
[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.40.126 netmask 255.255.255.0 broadcast 192.168.40.255
inet6 fe80::4521:124d:9706:406c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:e6:68 txqueuelen 1000 (Ethernet)
RX packets 477 bytes 45648 (44.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 480 bytes 49179 (48.0 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 316 bytes 27428 (26.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 316 bytes 27428 (26.7 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:76:02:0a 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
virbr0-nic: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 52:54:00:76:02:0a 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 加设备名 查询指定设备
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.40.126 netmask 255.255.255.0 broadcast 192.168.40.255
inet6 fe80::4521:124d:9706:406c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:e6:68 txqueuelen 1000 (Ethernet)
RX packets 502 bytes 47508 (46.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 497 bytes 53673 (52.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig -s 查询网络通讯情况
[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 529 0 0 0 516 0 0 0 BMRU
lo 65536 316 0 0 0 316 0 0 0 LRU
virbr0 1500 0 0 0 0 0 0 0 0 BMU
给设备创建虚拟网卡
冒号后的0代表的是虚拟网卡的序号,不能重复使用
[root@localhost ~]# ifconfig ens33:0 192.168.40.178
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.40.126 netmask 255.255.255.0 broadcast 192.168.40.255
inet6 fe80::4521:124d:9706:406c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:e6:68 txqueuelen 1000 (Ethernet)
RX packets 559 bytes 51867 (50.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 544 bytes 58844 (57.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.40.178 netmask 255.255.255.0 broadcast 192.168.40.255
ether 00:0c:29:02:e6:68 txqueuelen 1000 (Ethernet)
二 hostname命令
1、临时修改主机名
hostname 加主机名
su 或 bash 刷新,重启就失效
[root@localhost ~]# hostname xiaoyi
[root@localhost ~]# su
[root@xiaoyi ~]# hostname zihao
[root@xiaoyi ~]# bash
[root@zihao ~]#
[root@zihao ~]# hostname
zihao
[root@zihao ~]#
2、永久修改主机名
hostnamectl set-hostname 主机名
[root@zihao ~]# hostnamectl set-hostname xiaoyi
[root@zihao ~]# su
[root@xiaoyi ~]#
三 route命令查看路由表
Destination列对应目标网段的地址;Gateway列对应下一跳路由器的地址;Iface列对应发送
数据的网络接口。当目标网段为“default”时,表示此行是默认网关记录;当下一跳为“*”时,表示目
标网段是与本机直接相连的。
直接执行“route”命令无法直接看出默认网关地址,可执行命令“route -n”后,输出信息中的“*”
地址将显示为“0.0.0.0”,默认网关记录中的“default”也将显示为“0.0.0.0”。
[root@xiaoyi ~]# 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.40.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
route -n 查看或设置主机中路由表信息
[root@xiaoyi ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.40.2 0.0.0.0 UG 100 0 0 ens33
192.168.40.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@xiaoyi ~]#
四 netstat 命令 ss命令
netstat 命令
端口扫描,查看端口是否正常工作
常用选项
-a 显示当前主机所有活动的网络链接信息
-n 以数字的形式显示相关的主机地址和端口信息
-r 显示路由表的信息
-l 显示处于监听状态的网络链接和端口信息
-t 显示tcp协议的信息
-u 显示udp协议的信息
-p 显示与网络链接相关的进程号,进程名称信息(必须要使用root权限)
通常使用“-anpt”组合选项,以数字形式显示当前系统中所有的 TCP 连接信息,同时显示对
应的进程信息
示例
[root@xiaoyi ~]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1631/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1185/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1183/cupsd
配合管道符grep过滤出需要的内容
[root@xiaoyi ~]# netstat -antp | grep 22
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1631/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1185/sshd
tcp 0 36 192.168.40.126:22 192.168.40.1:59499 ESTABLISHED 54896/sshd: root@pt
tcp6 0 0 :::22 :::* LISTEN 1185/sshd
[root@xiaoyi ~]#
ss命令
查看系统的网络连接情况,获取socket统计信息
常用选项
-t 显示tcp协议的信息
-u 显示udp协议的信息
-w 裸套接字
-x 内核socket相关信息
-l 处于监听状态
-a 显示所有网络连接活动
-n 数字形式
-p 相关进程的pid 和进程名
用法与netstat差不多
ss的查询速度比netstat要快
ss适用于高并发,链接量较大的场景,netstat可能会卡死
五 dig命令
dig 显示当前网页的详细信息
[root@xiaoyi ~]# 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: 62101
;; 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. 1082 IN CNAME www.a.shifen.com.
www.a.shifen.com. 254 IN A 180.101.50.188
www.a.shifen.com. 254 IN A 180.101.50.242
;; Query time: 100 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: 四 5月 18 19:04:54 CST 2023
;; MSG SIZE rcvd: 101
六 测试网络连接
6.1 ping 命令
ping -c 5 www.baidu,com
-c 指定ping几次
[root@xiaoyi ~]# ping -c 5 www.baidu.com
PING www.a.shifen.com (180.101.50.242) 56(84) bytes of data.
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=1 ttl=128 time=153 ms
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=2 ttl=128 time=160 ms
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=3 ttl=128 time=50.2 ms
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=4 ttl=128 time=48.5 ms
64 bytes from 180.101.50.242 (180.101.50.242): icmp_seq=5 ttl=128 time=45.7 ms
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 45.747/91.678/160.734/53.351 ms
ping -w 5 www.baidu.com
-w 超时等待五次
[root@xiaoyi ~]# ping -w 5 www.baidu.com
PING www.a.shifen.com (180.101.50.188) 56(84) bytes of data.
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=1 ttl=128 time=39.6 ms
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=2 ttl=128 time=37.8 ms
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=3 ttl=128 time=36.7 ms
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=4 ttl=128 time=74.3 ms
64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=5 ttl=128 time=42.1 ms
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4008ms
rtt min/avg/max/mdev = 36.792/46.131/74.325/14.213 ms
[root@xiaoyi ~]#
6.2 traceroute命令
traceroute 测试从当前主机到目的主机之间经过的网络节点
[root@xiaoyi ~]# traceroute www.baidu.com
traceroute to www.baidu.com (180.101.50.188), 30 hops max, 60 byte packets
1 gateway (192.168.40.2) 0.077 ms 0.046 ms 0.037 ms
2 * * *
3 * * *
4 * * *
5 * * *
6 * * *
6.3 nslookup命令
nslookup 测试dns域名解析 正向解析,通过域名解析出真正的ip地址
[root@xiaoyi ~]# 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.
Name: www.a.shifen.com
Address: 180.101.50.242
Name: www.a.shifen.com
Address: 180.101.50.188
七 DSN解析地址的配置文件
/etc/resolv.conf 文件中记录了本机默认使用的 DNS 服务器的地址信息,对该文件所做 的修改
将会立刻生效。Linux 操作系统中最多可以指定 3 个(第 3 个以后的将被忽略)不同 的 DNS 服务
器地址,优先使用第一个 DNS 服务器。resolv.conf 文件中的“search localdomain”行用来设置默认
的搜索域(域名扩展名)
vim /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114
nameserver 192.168.40.102
也可以去到配置文件中
[root@xiaoyi ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=e7859de8-2dc6-48ff-9f2d-08d41242ea23
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.40.126
NETMASK=255.255.255.0
GATEWAY=192.168.40.2
DNS1=8.8.8.8
DNS2=114.114.114.114
DNS3=192.168.40.2 ##自己的NAT地址