Shell IV 网络相关命令

Shell-IV-网络相关命令

什么是网络?

是OSI的7层模型? 还是TCP/IP 协议簇?wifi/移动通信的空口互联? 又或者是内核进程的虚拟数据转发?

  • 网络的本质基于预定义的通信协议通过物理介质(或虚拟物理介质如虚拟端口,虚拟mac地址)在设备间进行数据传输的一种组织结构。
    • 网络是“有层次”的,无论是固网通信还是空口通信。数据包的传输都在进行一个“封装-解封装”的过程
    • 网络是“动态”的,端到端的网络通信,需要网卡持续监听外部网络接收到的数据包,并通过中断触发内核来对数据包进行处理

具体的可以查看后续的网络协议解析

IP/网卡查询

ifconfig (old)

ifconfig 提供了简单的查询网卡以及ip信息的功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 #网络帧大小,决定了转发的二层网络包的最大长度
inet 172.16.243.147 netmask 255.255.240.0 broadcast 172.16.255.255
# ipv4 地址 掩码 广播地址
inet6 fe80::216:3eff:fe4e:4cd3 prefixlen 64 scopeid 0x20<link>
# ipv6 地址 掩码
ether 00:16:3e:4e:4c:d3 txqueuelen 1000 (Ethernet)
# 以太网mac地址 最大传输队列长度
RX packets 4986558 bytes 3656623569 (3.4 GiB)
# 接收到的数据统计
RX errors 0 dropped 0 overruns 0 frame 0
# 异常,丢包统计
TX packets 3390873 bytes 827044117 (788.7 MiB)
# 发送的数据统计
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 异常,丢包统计

ip addr

新一代的ip 网络查询/配置命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:16:3e:4e:4c:d3 brd ff:ff:ff:ff:ff:ff
altname enp0s5
altname ens5
inet 172.16.243.147/20 brd 172.16.255.255 scope global dynamic noprefixroute eth0
valid_lft 1891653623sec preferred_lft 1891653623sec
inet6 fe80::216:3eff:fe4e:4cd3/64 scope link
valid_lft forever preferred_lft forever


ip addr add 10.0.0.1/24 dev eth0 # 添加 IP
ip addr del 10.0.0.1/24 dev eth0 # 删除 IP
ip addr flush dev eth0 # 清空 IP
ip addr show # 查看所有地址
ip addr show dev eth0 # 查看某网卡的地址信息

查询 网络链路状态,虚拟网卡配置

1
2
3
4
5
6
7
8
9
10
[root@iZbp19tqlmjz1dmnm8w43uZ _posts]# ip link show
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:16:3e:4e:4c:d3 brd ff:ff:ff:ff:ff:ff
altname enp0s5
altname ens5

ip link set veth-c up # 设置网络接口打开
ip link set veth-c down # 设置网络接口关闭
ip link add veth-c type veth peer name veth-br # 设置虚拟网络(定义的一对peer veth网络)
ip link del veth-c # 删除网络

ip netns

创建/设置 linux 网络命名空间

网络命名可以将其所在进程的网络与宿主机进行隔离

1
2
ip netns add net1                          # 添加网络命名空间
ip netns exec net1 ip addr # 在宿主机向命名空间内执行命令

寻址/路由

IP所处的网络层 主要负责接入网络的设备寻址和数据转发

arp

arp 协议是工作在数据链路层-网络层中间的 数据协议,其通过广播模式向广播域内所有设备请求其所需ip地址对应的mac地址,通常用于网络接入(查找网关),和冲突检测(是否有同ip设备上线同一局域网)。使用arp 命令可以查看本地的ip地址映射表项

1
2
3
4
[root@iZbp19tqlmjz1dmnm8w43uZ _posts]# arp -a
? (10.88.0.11) at be:34:94:fb:34:f6 [ether] on cni-podman0
? (10.88.0.10) at <incomplete> on cni-podman0

ip route

提供了本地路由的查询功能

1
2
3
4
5
6
7
8
9
10

[root@iZbp19tqlmjz1dmnm8w43uZ _posts]# ip route
default via 172.16.255.253 dev eth0 proto dhcp src 172.16.243.147 metric 100
# 网段 网关 网卡 源地址
10.88.0.0/16 dev cni-podman0 proto kernel scope link src 10.88.0.1
172.16.240.0/20 dev eth0 proto kernel scope link src 172.16.243.147 metric 100

ip route add 10.0.0.0/24 via 192.168.1.1 dev eth0 # 添加路由
ip route del 10.0.0.0/24 # 删除路由

网络状态

netstat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
netstat -tuln	        #显示所有正在监听的 TCP/UDP 端口(数字显示)
netstat -tulnp #显示监听端口及对应进程 PID/名称
[root@iZbp19tqlmjz1dmnm8w43uZ _posts]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
#协议 接收队列 发送队列 地址 传输的外部地址 进程号/进程
tcp 0 0 0.0.0.0:6380 0.0.0.0:* LISTEN 1018202/redis-serve
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd

netstat -an #显示所有连接(包括监听 + 活跃连接)
Proto RefCnt Flags Type State I-Node Path
#协议 引用次数 状态 传输类型 状态 inode编号 路径
unix 2 [ ACC ] STREAM LISTENING 18400306 /run/systemd/private
unix 2 [ ] DGRAM 17763560 /run/user/0/systemd/notify

netstat -r #显示路由表(等同于 route -n)
[root@iZbp19tqlmjz1dmnm8w43uZ _posts]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
#目的网段 网关 掩码 TCP最大传输大小和窗口大小 接口
default _gateway 0.0.0.0 UG 0 0 0 eth0
10.88.0.0 0.0.0.0 255.255.0.0 U 0 0 0 cni-podman0
172.16.240.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0

netstat -i #显示所有网卡接口的统计数据(收发包、丢包等)
[root@iZbp19tqlmjz1dmnm8w43uZ _posts]# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
#接口 最大传输 接收正常 异常 丢包 溢出 发送正常 异常 丢包 溢出
cni-podman0 1500 44683 0 0 0 53354 0 0 0 BMRU
eth0 1500 4990035 0 0 0 3393020 0 0 0 BMRU
lo 65536 1215234 0 0 0 1215234 0 0 0 LRU
vethfcdc13a0 1500 37461 0 0 0 45818 0 0 0 BMRU

netstat -s #显示协议数据统计(TCP/UDP/ICMP等)

ss

socket 信息查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ss -t        # 查询tcp 信息
ss -u # 查询udp 信息
ss -tulnp # 查询tcp/udp 以及端口和进程信息
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
#协议id 状态 接收统计 发送统计 本地ip地址:端口 对端ip地址:端口 进程信息
udp UNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=537,fd=5),("systemd",pid=1,fd=34))
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=574,fd=5))
udp UNCONN 0 0 [::]:111 [::]:* users:(("rpcbind",pid=537,fd=7),("systemd",pid=1,fd=36))
udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=574,fd=6))
tcp LISTEN 0 511 0.0.0.0:6380 0.0.0.0:* users:(("redis-server",pid=1018202,fd=6))
tcp LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=537,fd=4),("systemd",pid=1,fd=33))
tcp LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=990145,fd=6),("nginx",pid=990144,fd=6),("nginx",pid=990143,fd=6))
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1021,fd=3))
tcp LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=537,fd=6),("systemd",pid=1,fd=35))
tcp LISTEN 0 511 [::]:80 [::]:* users:(("nginx",pid=990145,fd=7),("nginx",pid=990144,fd=7),("nginx",pid=990143,fd=7))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1021,fd=4))

连通性/请求查询

ping

ping命令用来检查ip是否可以与对端正常通信,底层使用的是icmp协议。icmp协议中定义了一个字段(ttl time to live 存活时间)。这里的time 是在网络中传输时,没经过一个网络设备则会-1。当为0时数据包被丢弃。linux中的ping持续执行,window 会请求4次

1
2
3
4
ping 8.8.8.8
64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=30.4 ms
ping -c 5 8.8.8.8 # 向8.8.8.8 ip 发起5次ping 请求
ping -I eth0 8.8.8.8 # 指定网卡eth0 发起ping 请求

nslookup

1
2
3
4
5
6
7
8
9
10
[root@iZbp19tqlmjz1dmnm8w43uZ _posts]# nslookup baidu.com
Server: 100.100.2.136
Address: 100.100.2.136#53

Non-authoritative answer:
Name: baidu.com
Address: 39.156.66.10
Name: baidu.com
Address: 110.242.68.66

curl

通过命令行创建http请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl www.baidu.com

curl -d "username=user&password=pass" https://example.com/login
# 携带消息体发送post 请求
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "age": 30}'
# 传递json 数据
curl -o file.txt https://example.com/file.txt
# 下载文件到本地
curl --cookie "session=abc123" https://example.com
# 定义cookie信息
curl -I https://example.com
# 获取响应头
curl -i https://example.com
# 获取响应
curl -L https://short.url/link
# 跟踪重定向
curl -F "file=@localfile.txt" https://example.com/upload
# 上传文件
# 其他参数 :
# -v:详细模式(debug 信息)
# -s:静默模式(不输出进度条)
# -sS:静默但出错时显示错误信息(常用组合)
# -w '%{http_code}':输出 HTTP 状态码
# -k:忽略 HTTPS 证书错误(测试用)

wget

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
wget https://example.com/file.zip
# 获取文件
wget -O newname.zip https://example.com/file.zip
# 保存文件
wget -c https://example.com/large.iso
# 断点续传
wget -r https://example.com/
# 递归下载
wget --mirror --convert-links --adjust-extension --page-requisites --no-parent https://example.com
# 下载到本地,离线浏览
wget --header="Authorization: Bearer TOKEN123" https://api.example.com/data.json
wget --user=admin --password=123 https://example.com/protected/file.zip
# 配置认证信息
wget -i urls.txt

netcat

简易tcp连接工具

1
2
3
4
5
nc -l 1234         # 创建TCP 服务器
nc localhost 1234 # 连接
nc -l 1234 > received.txt # 开启窗口发送数据
nc [ip] 1234 < file.txt # 向服务端端口发送的数据

iperf

流量性能测试工具

1
2
3
4
5
6
iperf -s                     #开启服务端,默认监听5021端口
iperf -c [ip] #向目的端口发送10s tcp流量
iperf -c server_ip -u -b 100M#向服务端发送100M的 udp流量

-r #双向测试
-P #多线程

tcpdump

数据流量捕获工具

1
2
3
4
5
6
7
8
9
10
11
tcpdump -i eth0    # 捕获指定端口
tcpdump host [ip] # 获取目标ip的流量
tcpdump port # 监听指定端口
tcpdump -i eth0 src 192.168.1.1 and dst port 80 #接收源为192.168.1.1的 目的端口为80的包
tcpdump -w capture.pcap -i eth0 #捕获包到文件
tcpdump -i eth0 udp port 53 #捕获dns 流量包

# -i <接口>: 指定要监听的网络接口(如 eth0, wlan0)
# -c <数量>: 限制捕获的数据包数量
# -n: 不将地址解析为主机名(提高性能)
# -t: 不显示时间戳

Shell IV 网络相关命令
http://gadoid.io/2025/04/09/Shell-IV-网络相关命令/
作者
Codfish
发布于
2025年4月9日
许可协议