一.Linux网络管理命令
1.Ifconfig命令
ifconfig显示当前网络信息
格式: ifconfig [INTERFACE]
常用选项:
-a:查看所有信息
ifconfig IFACE up|down 启用|禁用某网卡接口
ifconfig interface [aftype] options | address ...
# ifconfig IFACE IP/mask [up]
# ifconfig IFACE IP netmask MASK
启用混杂模式:[-]promisc
Note:启用后立即生效。再次开机后不会再生效!
Eg:禁用eth1接口
[root@localhost ~]# ifconfig eth1 down [root@localhost ~]# ifconfig //可以看到eth1已经不再显示了 eth0 Link encap:Ethernet HWaddr 00:0C:29:7B:65:71 inet addr:172.16.249.202 Bcast:172.16.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe7b:6571/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:159414 errors:0 dropped:0 overruns:0 frame:0 TX packets:4673 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:12952278 (12.3 MiB) TX bytes:666573 (650.9 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
2.Route命令
常用选项:
-n: 查看路由表信息
格式: route add|del [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
add | del
-net: 添加|删除一个网络路由(即添加网段)
-host: 添加|删除一个主机路由
-gw : 添加| 删除网关
-netmask:添加|删除子网掩码
default:添加|删除 默认路由
Eg1:给eth0添加一个主机路由为192.168.1.3,默认网关为172.16.0.1
[root@localhost ~]# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.3 172.16.0.1 255.255.255.255 UGH 0 0 0 eth0 192.168.27.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1 172.16.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 172.16.0.1 0.0.0.0 UG 0 0 0 eth0 删除192.168.1.3这个路由 [root@localhost ~]# route del -host 192.168.1.3 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.27.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1 172.16.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 172.16.0.1 0.0.0.0 UG 0 0 0 eth0 [root@localhost ~]# 删除此路由条目 [root@localhost ~]# route del -net 192.168.0.0/24 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.27.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1 172.16.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 172.16.0.1 0.0.0.0 UG 0 0 0 eth0 或者[root@localhost ~]# route del -net 192.168.0.0 netmask 255.255.255.0 Eg2:给eth0接口添加一个网络路由为192.168.0.0默认网关为 172.16.0.1 [root@localhost ~]# route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0 [root@localhost ~]# route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0
3.Netstat命令
1>.显示网络连接:
格式: netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts][--numeric-ports][--numeric-ports] [--symbolic|-N] [--extend|-e[--extend|-e]]
常用选项:
-t :tcp协议相关
-u :udp协议相关
-l :监听的套接字 //常跟-t,-u选项
-p :打印信息
-e :扩展信息
-n :以数字形式显示
-p: 显示相关pid信息
Eg:以数字形式显示当前主机下tcp协议都监听那些端口并打印出来 [root@chang ~]# netstat -tnlp 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:22 0.0.0.0:* LISTEN 1821/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1612/cupsd tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2181/sshd tcp 0 0 0.0.0.0:50982 0.0.0.0:* LISTEN 1580/rpc.statd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1478/rpcbind tcp 0 0 :::22 :::* LISTEN 1821/sshd tcp 0 0 ::1:631 :::* LISTEN 1612/cupsd tcp 0 0 ::1:25 :::* LISTEN 1935/master tcp 0 0 :::35321 :::* LISTEN 1580/rpc.statd tcp 0 0 ::1:6010 :::* LISTEN 2181/sshd tcp 0 0 :::111 :::* LISTEN 1478/rpcbind
2>.显示路由信息
格式:netstat {--route|-r} [--numeric|-n][--numeric-hosts][--numeric-ports][--numeric-ports]
常用选项:
-r: 显示内核路由表
-n: 以数字格式显示
3>.显示接口统计数据
netstat {--interfaces|-I|-i} [iface] [-all|-a] [--extend|-e] [--program|-p]
常用选项:
-i : 显示所有的接口信息
-I IFACE : 显示多指定的接口信息
二.Linux网络属性命令
1.Ip 命令
格式: ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | addr | route }
ip link - network device configuration
set DEVICE { up | down } 激活| 禁用某接口
show DEVICE
show up : 仅显示处于激活状态的接口
ip address - protocol address management
ip addr { add | del } IFADDR dev STRING
[label LABEL]:添加地址时指明网卡别名
[scope {global|link|host}]:指明作用域
global: 全局可用;
link: 仅链接可用;
host: 本机可用;
ip address show - look at protocol addresses
[dev DEVICE]
[label PATTERN]
[primary and secondary]
ip address flush - flush protocol addresses
使用格式同show
ip route - routing table management
ip route add
添加路由:ip route add TARGET via GW dev IFACE src SOURCE_IP
TARGET:
主机路由:IP
网络路由:NETWORK/MASK
添加网关:ip route add defalt via GW dev IFACE
ip route delete
删除路由:ip route del TARGET
ip route show
ip route flush
[dev IFACE]
[via PREFIX]
2.ss命令
格式: ss [options] [ FILTER ]
常用选项:
-t : tcp协议相关
-u : udp协议相关
-w : 显示裸套接字
-x : unix sock 相关
-l : 监听状态的连接
-a : all
-n : 以数字格式显示
-p : 相关程序及其pid
-e : extend 的信息
-m : 内存用量
-o : 计时器信息
FILTER := [ state TCP-STATE ] [ EXPRESSION ]
EXPRESSION:
dport =
sport =
补充: tcp的常见状态
LISTEN : 监听
EATABLISHED :已建立的连接
FIN_WAIT_1
FIN_WAIT_2
SYN_SENT
SYN_RECV
CLOSE
ss命令跟netstat命令功能相似,但是要当前连接数较大时用ss命令用着更好。