linux-ip命令杂记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux-ip命令杂记相关的知识,希望对你有一定的参考价值。

11.3 ip命令

11.3.1 ip

查看ip是由哪个rpm生成的

[[email protected] ~]# rpm -qf `which ip`
iproute-2.6.32-32.el6_5.i686

查看iproute生成的文件

[[email protected] ~]# rpm -ql iproute
/etc/iproute2
/etc/iproute2/ematch_map
/etc/iproute2/rt_dsfield
/etc/iproute2/rt_protos
/etc/iproute2/rt_realms
/etc/iproute2/rt_scopes
/etc/iproute2/rt_tables
/etc/sysconfig/cbq
/etc/sysconfig/cbq/avpkt
/etc/sysconfig/cbq/cbq-0000.example
/sbin/bridge
/sbin/cbq
/sbin/ifcfg
/sbin/ip
/sbin/rtmon
/sbin/tc
/usr/lib/tc
/usr/lib/tc/m_xt.so
/usr/sbin/arpd
/usr/sbin/lnstat
/usr/sbin/nstat
/usr/sbin/rtacct
/usr/sbin/ss
/usr/share/doc/iproute-2.6.32
/usr/share/doc/iproute-2.6.32/COPYING
/usr/share/doc/iproute-2.6.32/README
/usr/share/doc/iproute-2.6.32/README.decnet
/usr/share/doc/iproute-2.6.32/README.distribution
/usr/share/doc/iproute-2.6.32/README.iproute2+tc
/usr/share/doc/iproute-2.6.32/README.lnstat
/usr/share/man/man8/arpd.8.gz
/usr/share/man/man8/bridge.8.gz
/usr/share/man/man8/cbq.8.gz
/usr/share/man/man8/ifcfg.8.gz
/usr/share/man/man8/ip.8.gz
/usr/share/man/man8/lnstat.8.gz
/usr/share/man/man8/nstat.8.gz
/usr/share/man/man8/routel.8.gz
/usr/share/man/man8/rtacct.8.gz
/usr/share/man/man8/rtmon.8.gz
/usr/share/man/man8/ss.8.gz
/usr/share/man/man8/tc-bfifo.8.gz
/usr/share/man/man8/tc-cbq-details.8.gz
/usr/share/man/man8/tc-cbq.8.gz
/usr/share/man/man8/tc-htb.8.gz
/usr/share/man/man8/tc-pfifo.8.gz
/usr/share/man/man8/tc-pfifo_fast.8.gz
/usr/share/man/man8/tc-prio.8.gz
/usr/share/man/man8/tc-red.8.gz
/usr/share/man/man8/tc-sfq.8.gz
/usr/share/man/man8/tc-tbf.8.gz
/usr/share/man/man8/tc.8.gz
/usr/share/tc
/usr/share/tc/normal.dist
/usr/share/tc/pareto.dist
/usr/share/tc/paretonormal.dist

11.3.2 ip-link

查看所有接口属性

[[email protected] ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff

查看指定接口属性

[[email protected] ~]# ip link show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff

关闭接口

[[email protected] ~]# ip link set dev lo down
[[email protected] ~]# ip lin show lo
1: lo: <LOOPBACK> mtu 65536 qdisc noqueue state DOWN mode DEFAULT 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

开启接口

[[email protected] ~]# ip link set dev lo up
[[email protected] ~]# ip lin show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

11.3.2 ip-addr

显示所有接口ip

[[email protected] ~]# ip addr 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
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1018sec preferred_lft 1018sec
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever

增加一个地址,两个地址

[[email protected] ~]# ip addr add  10.201.106.119 dev ens33
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# ip addr show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1654sec preferred_lft 1654sec
    inet 10.201.106.119/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever

删除IP地址

[[email protected] ~]# ip addr delete 10.201.106.119/32 dev ens33
[[email protected] ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1372sec preferred_lft 1372sec
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever

在原有的基础上增加一个IP地址,并添加label

[[email protected] ~]# ip addr add 10.201.106.222/24 dev ens33 label ‘ens33:1‘
[[email protected] ~]# ip add show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1151sec preferred_lft 1151sec
    inet 10.201.106.222/24 scope global secondary ens33:1    #显示label
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever

带label参数删除IP

[[email protected] ~]# ip addr del 10.201.106.222/24 dev ens33 label ens33:1
[[email protected] ~]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1482sec preferred_lft 1482sec
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever

只显示主地址

[[email protected] ~]# ip addr show dev ens33 
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1183sec preferred_lft 1183sec
    inet 10.201.106.222/24 scope global secondary ens33:3
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# ip addr show dev ens33 primary
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1181sec preferred_lft 1181sec
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# 

只显示辅助地址

[[email protected] ~]# ip addr show dev ens33 secondary
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.222/24 scope global secondary ens33:3
       valid_lft forever preferred_lft forever

根据标签清除IP地址

[[email protected] ~]# ip addr flush dev ens33 label ens33:3
[[email protected] ~]# ip addr show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a7:7a:ec brd ff:ff:ff:ff:ff:ff
    inet 10.201.106.130/24 brd 10.201.106.255 scope global dynamic ens33
       valid_lft 1718sec preferred_lft 1718sec
    inet6 fe80::20c:29ff:fea7:7aec/64 scope link 
       valid_lft forever preferred_lft forever

11.3.3 ip route

添加主机路由

[[email protected] ~]# ip route add 192.168.1.3/32 via 10.201.106.2 dev ens33
[[email protected] ~]# ip route show
default via 10.201.106.2 dev ens33  proto static  metric 100 
10.201.106.0/24 dev ens33  proto kernel  scope link  src 10.201.106.130  metric 100 
192.168.1.3 via 10.201.106.2 dev ens33

添加网段路由

[[email protected] ~]# ip route add 192.168.0.0/24 via 10.201.106.2 dev ens33
[[email protected] ~]# ip route show dev ens33
default via 10.201.106.2  proto static  metric 100 
10.201.106.0/24  proto kernel  scope link  src 10.201.106.130  metric 100 
192.168.0.0/24 via 10.201.106.2 
192.168.1.3 via 10.201.106.2 

删除路由

[[email protected] ~]# ip route del 192.168.1.3
[[email protected] ~]# ip route list
default via 10.201.106.2 dev ens33  proto static  metric 100 
10.201.106.0/24 dev ens33  proto kernel  scope link  src 10.201.106.130  metric 100 
192.168.0.0/24 via 10.201.106.2 dev ens33 
[[email protected] ~]# ip route del 192.168.0.0/24
[[email protected] ~]# ip route list
default via 10.201.106.2 dev ens33  proto static  metric 100 
10.201.106.0/24 dev ens33  proto kernel  scope link  src 10.201.106.130  metric 100 

添加默认路由

[[email protected] ~]# ip route add default via 10.201.106.102
[[email protected] ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.201.106.102  0.0.0.0         UG    0      0        0 ens33
0.0.0.0         10.201.106.2    0.0.0.0         UG    100    0        0 ens33
10.201.106.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33

清空所有路由表

ip route flush

11.3.4 ss

显示所有TCP进程,以端口号显示

[[email protected] ~]# ss -tan
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
ESTAB      0      0      10.201.106.130:22                 10.201.106.1:49530              
ESTAB      0      0      10.201.106.130:22                 10.201.106.1:49506              
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::* 

显示已连接状态的TCP连接

[[email protected] ~]# ss -tan state ESTABLISHED
Recv-Q Send-Q Local Address:Port               Peer Address:Port              
0      52     10.201.106.130:22                 10.201.106.1:49530              
0      0      10.201.106.130:22                 10.201.106.1:49506    

配置文件配置网络

ifcfg-eth0配置

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

DEVICE=eth0
# If you‘re having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
ONBOOT="yes"
TYPE="Ethernet"
NM_CONTROLLED="yes"
BOOTPROTO="static"
IPADDR=10.201.106.250
NETMASK=255.255.255.0
GATEWAY=10.201.106.2

配置文件配置路由条目(1)

[[email protected] network-scripts]# vim route-eth0 

192.168.0.0/24 via 10.201.106.2

[[email protected] network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.201.106.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0     10.201.106.2    255.255.255.0   UG    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         10.201.106.2    0.0.0.0         UG    0      0        0 eth0

配置文件配置路由条目(2)

[[email protected] network-scripts]# vim route-eth0 

ADDRESS0=192.168.20.0
NETMASK0=255.255.255.0
GATEWAY0=10.201.106.2
ADDRESS1=192.168.30.0
NETMASK1=255.255.255.0
GATEWAY1=10.201.106.2

[[email protected] network-scripts]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  
Determining IP information for eth0... done.
ipcalc: bad IPv4 address: 192.168.3330.0
Error: an inet prefix is expected rather than "192.168.3330.0/24".
                                                           [  OK  ]
[[email protected] network-scripts]# vim route-eth0 
[[email protected] network-scripts]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  
Determining IP information for eth0... done.
                                                           [  OK  ]
[[email protected] network-scripts]# 
[[email protected] network-scripts]# 
[[email protected] network-scripts]# 
[[email protected] network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.20.0    10.201.106.2    255.255.255.0   UG    0      0        0 eth0
10.201.106.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.30.0    10.201.106.2    255.255.255.0   UG    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         10.201.106.2    0.0.0.0         UG    0      0        0 eth0

ifcfg配置多地址

[[email protected] network-scripts]# ifconfig eth0:1 192.168.0.22/24 up

[[email protected] network-scripts]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:4E:AF:24  
          inet addr:10.201.106.128  Bcast:10.201.106.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4e:af24/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2221 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1851 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:204895 (200.0 KiB)  TX bytes:278337 (271.8 KiB)

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:4E:AF:24  
          inet addr:192.168.0.22  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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)

    [[email protected] network-scripts]# 
    [[email protected] network-scripts]# ping 192.168.0.22
    PING 192.168.0.22 (192.168.0.22) 56(84) bytes of data.
    64 bytes from 192.168.0.22: icmp_seq=1 ttl=64 time=1.05 ms
    64 bytes from 192.168.0.22: icmp_seq=2 ttl=64 time=0.339 ms

配置文件添加别名网卡地址

cp ifcfg-eth0 ifcfg-eth0:1
[[email protected] network-scripts]# vim ifcfg-eth0:1

DEVICE=eth0:1
HWADDR=00:0C:29:4E:AF:24
TYPE=Ethernet
UUID=a493b909-229d-4a02-b1b5-ebbb51d16983
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO="static"
IPADDR=192.168.0.22
NETMASK=255.255.255.0
DNS1=10.201.106.2

tui

setup调用 network configuration

更改主机名

[[email protected] network-scripts]# vim /etc/sysconfig/network

NETWORKING=yes            #网络总开关,网络不通先确认该项有没打开
HOSTNAME=zabbix.localdomain

将eth0改为eth1

[[email protected] ~]# cd /etc/udev/rules.d/

vim 70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:4e:af:24", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1   #改为eth1

[[email protected] rules.d]#  lsmod               #查看网卡
[[email protected] rules.d]# modprobe -r e1000    #卸载网卡
[[email protected] rules.d]# modprobe  e1000      #加载网卡
[[email protected] ~]# service network restart    #重启网络服务
[[email protected] ~]# ifconfig -a
eth1      Link encap:Ethernet  HWaddr 00:0C:29:4E:AF:24  
          inet addr:10.201.106.128  Bcast:10.201.106.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe4e:af24/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:350 errors:0 dropped:0 overruns:0 frame:0
          TX packets:305 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:34875 (34.0 KiB)  TX bytes:65070 (63.5 KiB)

以上是关于linux-ip命令杂记的主要内容,如果未能解决你的问题,请参考以下文章

linux的shell特性三兼杂记

Python 杂记:argparse 模块

openssl命令杂记

linux 常见命令及说明杂记

MongoDB基础命令杂记

nmcli和nmtui命令杂记