在CentOS 7中,网络状态查询是一个常见的任务,它对于系统管理员来说尤为重要,通过了解网络状态,可以确保系统正常运行并排除潜在的网络问题,本文将详细介绍如何在CentOS 7中查询网络状态,包括使用各种命令和工具。
一、使用`ip`命令查看网络状态
在CentOS 7中,ip
命令是最常用的网络管理工具之一,以下是一些常用的子命令:
1、查看所有网络接口的状态:
ip addr show
输出示例:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:b8:9f:e4 brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic enp0s3 valid_lft 86384sec preferred_lft 86384sec inet6 fe80::a00:27ff:feb8:9fe4/64 scope link valid_lft forever preferred_lft forever
2、查看特定网络接口的状态:
ip addr show dev enp0s3
输出示例:
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:b8:9f:e4 brd ff:ff:ff:ff:ff:ff inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic enp0s3 valid_lft 86384sec preferred_lft 86384sec inet6 fe80::a00:27ff:feb8:9fe4/64 scope link valid_lft forever preferred_lft forever
3、查看路由表:
ip route show
输出示例:
default via 192.168.1.1 dev enp0s3 onlink 192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.10
二、使用ifconfig
命令查看网络状态
虽然ifconfig
命令在CentOS 7中已被废弃,但仍然可以通过安装net-tools
包来使用它:
1、安装net-tools
包:
yum install net-tools -y
2、查看所有网络接口的状态:
ifconfig -a
输出示例:
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::a00:27ff:feb8:9fe4/64 scope link ether 08:00:27:b8:9f:e4 txqueuelen 1000 (Ethernet) RX packets 12345 errors 0 dropped 0 overruns 0 frame 0 TX packets 54321 errors 0 dropped 0 overruns 0 carrier 0 collisions 0 txqueuelen 1000 RX bytes 1234567 (1.2 MiB) TX bytes 543210 (543.2 KiB) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1/128 scope host loop txqueuelen 1000 (Local Loopback) RX packets 12345 errors 0 dropped 0 overruns 0 frame 0 TX packets 54321 errors 0 dropped 0 overruns 0 carrier 0 collisions 0 txqueuelen 1000 RX bytes 1234567 (1.2 MiB) TX bytes 543210 (543.2 KiB)
三、使用`nmcli`命令查看网络状态
nmcli
是NetworkManager的命令行工具,用于管理网络连接,以下是一些常用的子命令:
1、查看所有网卡的详细信息:
nmcli device show
输出示例:
GENERAL.DEVICE: enp0s3 GENERAL.TYPE: ethernet GENERAL.HWADDR: 08:00:27:b8:9f:e4 GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: Wired connection 1 ...
2、查看所有网卡的连接状态信息:
nmcli device status
输出示例:
DEVICE TYPE STATE CONNECTION enp0s3 ethernet connected Wired connection 1 lo loopback unmanaged
3、查看所有活动的连接:
nmcli connection show --active
输出示例:
NAME UUID TYPE DEVICE Wired connection 1 [uuid-string] ethernet enp0s3
四、使用`ss`命令查看网络状态
ss
命令用于显示套接字统计信息,类似于netstat
,但功能更强大,以下是一些常用的子命令:
1、查看所有监听端口及对应的进程:
ss -tuln
输出示例:
State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd"),pid=872,12768) LISTEN 0 128 *:80 *:* users:(("httpd"),pid=884/nginx)
2、查看TCP协议的连接情况:
ss -tln
输出示例:
State Recv-Q Send-Q Local Address:Port Peer Address:Port ESTABSH 0 0 [::ffff]:ssh [::ffff]:54321 users:(("sshd"),pid=872/sshd) ESTABSL 0 0 [::ffff]:http [::ffff]:54321 users:(("httpd"),pid=884/nginx)
五、使用netstat
命令查看网络状态
netstat
命令也是一个常用的网络状态查看工具,但在CentOS 7中已被ss
命令取代,以下是一些常用的子命令:
1、查看所有监听端口及对应的进程:
netstat -tuln
输出示例:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 872/sshd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 884/nginx
2、查看TCP协议的连接情况:
netstat -tln
输出示例:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 [::ffff]:ssh [::ffff]:54321 ESTABLISHED 872/sshd tcp 0 0 [::ffff]:http [::ffff]:54321 ESTABLISHED 884/nginx
六、使用hostname
命令查看主机名和设置主机名
hostname
命令用于显示或设置系统的主机名,以下是一些常用的子命令:
1、查看当前主机名:
hostname
输出示例:
centos7
2、设置新的主机名(需要root权限):
hostname new-hostname
然后编辑/etc/hosts
文件,更新相应的条目。
七、使用`ping`命令测试网络连通性
ping
命令用于测试与目标地址的网络连通性,以下是一些常用的子命令:
1、ping一个IP地址:
ping 8.8.8.8
输出示例:
PING 64 bytes from 8.8.8.8: icmp_seq=1 ttl=46 time=24.3 ms
2、ping一个域名:
ping google.com
输出示例:
Ping google.com (172.217.6.206): 56 data bytes From google.com: icmp_seq=1 ttl=54 time=24.3 ms
八、使用traceroute
命令跟踪数据包路径
traceroute
命令用于显示数据包到目标地址的路径,以下是一些常用的子命令:
1、跟踪到一个IP地址的路径:
traceroute 8.8.8.8
输出示例:
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets 1 gateway (192.168.1.1) 2.345 ms 2.456 ms 2.567 ms ...
2、跟踪到一个域名的路径:
traceroute google.com
输出示例:
traceroute to google.com (172.217.6.206), 30 hops max, 60 byte packets 1 gateway (192.168.1.1) 2.345 ms 2.456 ms 2.567 ms ...
九、使用`arp`命令查看ARP缓存
arp
命令用于显示和操作系统ARP缓存中的MAC地址和IP地址对应关系,以下是一些常用的子命令:
1、查看ARP缓存:
arp -a
输出示例:
? (192.168.1.1) at <incomplete> address> [incomplete]> on wlan0 [ethernet] [free] permanent pe15pec9-cc5b-46dd-a1bf-cab7ce3b79d9 [ether] [free] persistent ccb9aaf4-dee7-45ec-bb3c-c5c9c6fdba9c [ether] [free] persistent d4bef5e9-a6e7-4bcd-8fdb-f5aacbedcfd3 [ether] [free] permanent e8:cd:52:79:3c:ab [ether] [free] persistent f4:f2:6d:e5:f6:b7 [ether] [free] persistent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8:f7:64:ea:e8:cd [ether] [free] permanent f8...
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1411580.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复