Linux 内核网络协议栈包括网络设备驱动程序、数据链路层、网络层、传输层和应用层。数据链路层处理与物理网卡的交互,网络层负责 IP 协议、路由和分组转发等任务,传输层则负责 TCP 协议的实现。
在 Linux 系统中,可以使用一些命令行工具来监控网络使用情况,例如 ifconfig、ip、netstat 等工具,还可以使用诸如 Wireshark、tcpdump 等网络抓包工具来捕获网络数据流,以便进一步分析网络性能和故障,如下
- 确认网络配置是否正确:比如检查 IP 地址、子网掩码、默认网关等是否设置正确。可以使用 ifconfig、ip 等命令来查看和设置网络配置。例如,使用 ifconfig 命令来查看当前网络接口的 IP 地址和状态:
ifconfig eth0
- 确认 DNS 配置是否正确:如果访问某些网站时出现问题,可以检查 DNS 配置是否正确。可以使用 dig、nslookup 等命令来查询 DNS 解析结果。例如,使用 dig 命令查询 www.google.com 的 DNS 解析结果:
dig www.google.com
- 确认网络连接是否正常:可以使用 ping 命令来测试网络连接是否正常。例如,测试与 www.baidu.com 的网络连接是否正常:
ping www.baidu.com
- 检查网络服务是否启动:如果某个服务无法访问,可以检查其是否已经启动。可以使用 systemctl、service 等命令来管理服务。例如,使用 systemctl 命令来检查 sshd 服务是否已经启动:
systemctl status sshd
- 检查防火墙规则是否正确:如果防火墙配置不正确,可能导致一些网络连接被屏蔽。可以使用 iptables、firewalld 等命令来管理防火墙规则。例如,使用 iptables 命令来查看当前的防火墙规则:
iptables -L
- 捕获网络数据包分析问题:如果无法通过上面的步骤找到问题所在,可以使用网络抓包工具来捕获网络数据包进行分析。可以使用 tcpdump、Wireshark 等工具来捕获和分析网络数据包。例如,使用 tcpdump 命令来捕获 eth0 网络接口的数据包:
tcpdump -i eth0
网络相关的命令
- ip 命令:用于管理和配置 IP 地址、路由、网络设备等。
$ ip addr show eth0
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:7c:f1:a0 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.100/24 brd 192.168.0.255 scope global noprefixroute dynamic eth0
valid_lft 86002sec preferred_lft 86002sec
inet6 fe80::20c:29ff:fe7c:f1a0/64 scope link
valid_lft forever preferred_lft forever
- ifconfig 命令:查看和配置网络接口信息。
$ ifconfig eth0
: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
mtu 1500 inet 192.168.0.100 netmask 255.255.255.0 broadcast 192.168.0.255
ether 00:0c:29:7c:f1:a0 txqueuelen 1000 (Ethernet) RX packets 146180
bytes 178746339 (170.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX
packets 46581 bytes 3297534 (3.1 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- netstat 命令:显示网络状态和连接信息。
$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:mysql 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:http 0.0.0.0:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
5.ss 命令:另一种网络状态查询工具,比 netstat 更快速、更精简。
$ ss -an | grep ':22' LISTEN 0 128 0.0.0.0:22 *:* LISTEN 0 128 [::]:22 [::]:*
6.route 命令:用于查看和修改路由表信息。
$ route -n
7.traceroute 命令:跟踪数据包在网络中的路径。
traceroute www.baidu.com
traceroute to www.baidu.com (220.181.57.216), 30 hops max, 60 byte packets
1 gateway (192.168.0.1) 1.371 ms 1.336 ms 1.329 ms
2 114.239.9.245 (114.239.9.245) 15.676 ms 15.681 ms 15.677 ms
8.telnet 命令:测试远程主机是否可以连接指定端口。
telnet www.baidu.com 80
Trying 93.184.216.34... Connected to www.baidu.com. Escape character is '^]'.
9.nc 命令:用于在网络上进行简单的文本传输。
$ echo 'hello world' | nc -v -w 1 example.com 80
10.scp 命令:用于在不同的主机之间复制文件。
$ scp file.txt user@qq.com:/path/to/destination
整理不易,欢迎点赞,收藏,转发,关注我每天分享一下运维小知识。