网络命令
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络命令相关的知识,希望对你有一定的参考价值。
网卡配置管理命令:ip, ifconfig,mii-tool,ethtool,ping,netstat,ss
路由设置管理命令:route,traceroute ,tracert
8.1.ifconfig
功能:配置打印网络接口
语法:ifconfig [interface]
ifconfig interface [aftype] options | address ...
常用选项:
up:启用网卡
down:停用网卡
-a:显示所有网卡信息
示例:
[[email protected] ~]# ifconfig eth0 #显示指定网卡信息 [[email protected] ~]# ifconfig eth0 down #停用网卡,eth0信息不显示了
eth0启用又显示了
[[email protected] ~]# ifconfig -a #显示所有网卡信息 [[email protected] ~]# ifconfig eth0:0 192.168.10.1/24 设置临时网卡
8.2.mii-tool
功能:查看操作接口状态
示例:
[[email protected] ~]# mii-tool eth0 #查看网卡状态,以前的虚拟机里不支持,现在vmware的版本里是支持的 eth0: negotiated 100baseTx-FD, link ok #网卡连接ok # mii-tool eth0 eth0: no link--代表网线和网卡没连
8.3.ethtool
功能:查询和设置网络硬件信息
示例:
[[email protected] ~]# ethtool eth0 #查看网卡的工作模式等信息 Settings for eth0: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supported pause frame use: No Supports auto-negotiation: Yes Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Advertised pause frame use: No Advertised auto-negotiation: Yes Speed: 1000Mb/s # ethtool -s eth0 speed 10 duplex half autoneg off #改成10M/s的速率,半双工,关闭自动协商 # ethtool -s eth0 speed 100 duplex full autoneg on#再改回来
8.4.ip(重点掌握的,centos7对ifconfig已经弱化了)
功能:打印并设置路由、设备、策略路由和隧道配置信息
语法:语法十分复杂,这里不一一列举
通过ip组合衍生出好多命令
ip link 网络设备配置命令
ip addr 管理网络设备与协议,相对ip link,增加了对ip地址的管理
ip addrlabel ipv6的地址标签
ip route 管理路由
ip rule 管理路由策略
ip tunnel 隧道配置
ip maddr 多播地址管理
ip mroute 多播路由管理
ip monitor 状态监控
ip xfrm 设置xfrm,xfrm是一个ip框架
示例:
[[email protected] ~]# ip link show #查看默认网卡信息 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:a0:3f:29 brd ff:ff:ff:ff:ff:ff [[email protected] ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:a0:3f:29 brd ff:ff:ff:ff:ff:ff inet 192.168.19.54/24 brd 192.168.19.255 scope global eth0 inet6 fe80::20c:29ff:fea0:3f29/64 scope link valid_lft forever preferred_lft forever [[email protected] ~]# ip addr show 跟ip addr显示没有什么区别 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:a0:3f:29 brd ff:ff:ff:ff:ff:ff inet 192.168.19.54/24 brd 192.168.19.255 scope global eth0 inet6 fe80::20c:29ff:fea0:3f29/64 scope link valid_lft forever preferred_lft forever [[email protected] ~]# ip addr add dev eth0 192.168.11.1/24 label eth0:1 [[email protected] ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:a0:3f:29 brd ff:ff:ff:ff:ff:ff inet 192.168.19.54/24 brd 192.168.19.255 scope global eth0 inet 192.168.10.1/24 brd 192.168.10.255 scope global eth0:0 inet 192.168.11.1/24 scope global eth0:1 添加临时网卡及ip,这里已经有显示 inet6 fe80::20c:29ff:fea0:3f29/64 scope link valid_lft forever preferred_lft forever [[email protected] ~]# ip addr del dev eth0 192.168.10.1 #删除临时及网卡 [[email protected] ~]# ip addr flush eth0 #刷新网络设备
说明:ip地址也被清除了
[[email protected] ~]# ip route add 192.168.20.0/24 via 192.168.19.54 #设置一条静态路由 [[email protected] ~]# ip route show #查看已经生效了 192.168.20.0/24 via 192.168.19.54 dev eth0 192.168.19.0/24 dev eth0 proto kernel scope link src 192.168.19.54 169.254.0.0/16 dev eth0 scope link metric 1002 default via 192.168.19.2 dev eth0 [[email protected] ~]# ip route del 192.168.20.0/24 #删除刚刚设置的路由 [[email protected] ~]# ip route 192.168.19.0/24 dev eth0 proto kernel scope link src 192.168.19.54 169.254.0.0/16 dev eth0 scope link metric 1002 default via 192.168.19.2 dev eth0
8.5.route
功能:打印设置路由表
常用选项:
-n:不使用通讯协定或主机名称,直接使用 IP 或 port number;
-ee:显示更详细的信息
增加 (add) 与删除 (del) 路由的相关参数:
-net :添加到网络的路由
-host :添加到主机的路由
netmask :子网掩码设置
gw :下一跳地址
dev :下一跳网络接口,后面接 eth0 等
示例:
[[email protected] ~]# route -n #查看路由表,netstat -rn也可以查看 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.19.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.19.2 0.0.0.0 UG 0 0 0 eth0 #添加到主机的路由 [[email protected] ~]# route add -host 192.168.6.1 dev eth0 [[email protected] ~]# route add -host 192.168.6.12 gw 192.168.6.1 #添加到网络的路由 [[email protected] ~]# route add -net 192.168.6.1 netmask 255.255.255.0 eth0 [[email protected] ~]# route add -net 192.168.6.1 netmask 255.255.255.0 eth0 gw 192.168.6.2 [[email protected] ~]# route add -net 192.168.6.1/24 #添加默认网关 [[email protected] ~]# route add default gw 1.1.1.2 #删除路由 [root[email protected] ~]# route del -host 192.168.6.1 dev eth0 [[email protected] ~]# route del -net 192.168.6.1 netmask 255.255.255.0 eth0 [[email protected] ~]# route del default gw 1.1.1.2
8.6.ping
功能:发送目标主机ICMP的网络请求
常用选项:
-c:次数
示例:
[[email protected] ~]# ping -c 3 192.168.19.1 #不加-c会一直ping下去 PING 192.168.19.1 (192.168.19.1) 56(84) bytes of data. 64 bytes from 192.168.19.1: icmp_seq=1 ttl=128 time=0.125 ms 64 bytes from 192.168.19.1: icmp_seq=2 ttl=128 time=0.381 ms 64 bytes from 192.168.19.1: icmp_seq=3 ttl=128 time=0.386 ms --- 192.168.19.1 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 0.125/0.297/0.386/0.122 ms
8.7.traceroute
功能:将路由包跟踪打印到网络主机上
常用选项:
-I --icmp Use ICMP ECHO for tracerouting #ICMP协议请求路由包
-T --tcp Use TCP SYN for tracerouting #tcp协议请求路由包
-p port --port=port #端口请求
示例:
[[email protected] ~]# traceroute baidu.com traceroute to baidu.com (180.149.132.47), 30 hops max, 60 byte packets 1 192.168.19.2 (192.168.19.2) 0.086 ms 0.037 ms 0.041 ms 2 * * * 3 * * * 4 * * * 5 * * * 6 * * * 说明:*号主要是服务器把udp数据包屏蔽了,所有没有返回ICMP [[email protected] ~]# traceroute -T -p 80 baidu.com traceroute to baidu.com (180.149.132.47), 30 hops max, 52 byte packets 1 192.168.19.2 (192.168.19.2) 0.168 ms 0.072 ms 0.101 ms 2 180.149.132.47 (180.149.132.47) 42.414 ms 37.795 ms 38.718 ms
8.8.tracert
功能:将路由包跟踪打印到网络主机上 (windows上使用的命令)
-d:指定不将ip地址解析到主机名称
-h:maximum_hops 指定跃点数以跟踪到称为target_name的主机路由
w:等待由timeout对每个应答指定的毫秒数
target_name:目标主机的名称或ip地址
示例:
8.9.netstat
功能:打印网络连接、路由表、接口统计信息、伪装连接和多播成员
常用选项:
-r 显示路由表
-i 显示接口表
-n 不解析名字
-p 显示程序名 PID/Program
-l 显示监听的socket
-a 显示所有socket
-o 显示计时器
-Z 显示上下文
-t 只显示tcp连接
-u 只显示udp连接
-s 显示每个协议统计信息
示例:
[[email protected] ~]# netstat -tlunap #显示所有监听端口 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:22 0.0.0.0:* LISTEN 897/sshd tcp 0 96 192.168.19.54:22 192.168.19.1:7024 ESTABLISHED 15148/sshd tcp 0 0 192.168.19.54:22 192.168.19.1:7023 ESTABLISHED 15129/sshd tcp 0 0 :::22 :::* LISTEN 897/sshd tcp 0 0 :::80 :::* LISTEN 13764/httpd [[email protected] ~]# netstat -tlnap #显示所有tcp连接 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:22 0.0.0.0:* LISTEN 897/sshd tcp 0 0 192.168.19.54:22 192.168.19.1:7024 ESTABLISHED 15148/sshd tcp 0 0 192.168.19.54:22 192.168.19.1:7023 ESTABLISHED 15129/sshd tcp 0 0 :::22 :::* LISTEN 897/sshd tcp 0 0 :::80 [[email protected] ~]# netstat -ulnap #显示所有udp连接,目前没有udp连接,所以为空 Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name [[email protected] ~]# netstat -r #可以显示路由表信息 Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.19.0 * 255.255.255.0 U 0 0 0 eth0 link-local * 255.255.0.0 U 0 0 0 eth0 default 192.168.19.2 0.0.0.0 UG 0 0 0 eth0
8.10.ss
功能:另一个比netstat更强大的socket查看工具
语法:ss [options] [ FILTER ]
常用选项:
-n 不解析名字
-a 显示所有socket
-l 显示所有监听的socket
-o 显示计时器
-e 显示socket详细信息
-m 显示socket内存使用
-p 显示进程使用的socket
-i 显示内部TCP信息
-s 显示socket使用汇总
-4 只显示IPV4的socket
-0 显示包socket
-t 只显示TCP socket
-u 只显示UDP socket
-d 只显示DCCP socket
-w 只显示RAW socket
-x 只显示Unix域socket
-f FAMILY 只显示socket族类型( unix, inet,inet6, link, netlink)
-A 查询socket{all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]
-D 将原始的TCP socket转储到文件
-F 从文件中读取过滤信息
过滤:
-o state 显示TCP连接状态信息
示例:
[[email protected] ~]# ss -t -a #显示所有tcp连接 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::ssh :::* LISTEN 0 128 *:ssh *:* LISTEN 0 511 :::http :::* ESTAB 0 0 192.168.19.54:ssh 192.168.19.1:7024 ESTAB 0 0 192.168.19.54:ssh 192.168.19.1:7023 [[email protected] ~]# ss -u -a #显示所有udp连接 State Recv-Q Send-Q Local Address:Port Peer Address:Port [[email protected] ~]# ss -s #显示所有socket Total: 287 (kernel 295) TCP: 6 (estab 2, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 2 Transport Total IP IPv6 * 295 - - RAW 0 0 0 UDP 0 0 0 TCP 5 3 2 INET 5 3 2 FRAG 0 0 0 [[email protected] ~]# ss -o state established #显示已经建立的连接 Recv-Q Send-Q Local Address:Port Peer Address:Port 0 0 192.168.19.54:ssh 192.168.19.1:7024 timer:(keepalive,25min,0) 0 96 192.168.19.54:ssh 192.168.19.1:7023 timer:(on,248ms,0) [[email protected] ~]# ss -o state TIME_WAIT #显示所有的timeout,这里没有的 ss: no socket states to show with such filter
本文出自 “烂笔头” 博客,请务必保留此出处http://lanbitou.blog.51cto.com/9921494/1930601
以上是关于网络命令的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装