Linux ip 命令使用示例

https://cloud.tencent.com/developer/article/1183389

查看当前主机网络连接信息
    # ip link show
    1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT 
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: eno16777728:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff

查看当前主机指定网络连接信息    
    # ip link show eno16777728
    2: eno16777728:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff        

多播的启用与关闭
    # ip link set eno16777728 multicast off
    # ip link show eno16777728  //如下,没有出项MULTICAST
    2: eno16777728:  mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
        link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff
    # ip link set eno16777728 multicast on    

网卡的启用与关闭
    # ip link set eno33554960 down
    # ip link set eno33554960 up

显示主机ip地址信息
    # ip addr show
    1: lo:  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: eno16777728:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:9d brd ff:ff:ff:ff:ff:ff
        inet 172.24.8.131/24 brd 172.24.8.255 scope global dynamic eno16777728
          valid_lft 1196sec preferred_lft 1196sec           ### Author : Leshami
        inet6 fe80::20c:29ff:fe57:269d/64 scope link     ### Blog : http://blog.csdn.net/leshami 
          valid_lft forever preferred_lft forever                ### QQ/Weixin : 645746311
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.144/24 brd 192.168.81.255 scope global dynamic eno33554960
          valid_lft 1380sec preferred_lft 1380sec

为指定网卡删除ip地址
    # ip addr del dev eno33554960 192.168.81.144/24
    # ip addr show eno33554960                    
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff

为指定网卡添加ip地址
    # ip addr add dev eno33554960 192.168.81.189/24  
    # ip addr show eno33554960                    
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.189/24 scope global eno33554960
          valid_lft forever preferred_lft forever      

为指定网卡添加多ip地址      
    # ip addr add dev eno33554960 192.168.81.150/24
    # ip addr show eno33554960
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.189/24 scope global eno33554960
          valid_lft forever preferred_lft forever
        inet 192.168.81.150/24 scope global secondary eno33554960
          valid_lft forever preferred_lft forever

为指定网卡添加多ip及使用别名
    # ip addr add dev eno33554960 192.168.81.199/24 label eno33554960:0

    # ip addr show label eno33554960:0
        inet 192.168.81.199/24 scope global secondary eno33554960:0
          valid_lft forever preferred_lft forever

    # ifconfig|grep eno33554960:0 -A2  
    eno33554960:0: flags=4163  mtu 1500
            inet 192.168.81.199  netmask 255.255.255.0  broadcast 0.0.0.0
            ether 00:0c:29:57:26:a7  txqueuelen 1000  (Ethernet)              

释放特定网卡ip地址
    # ip addr show eno33554960 
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.144/24 scope global dynamic eno33554960
          valid_lft 1364sec preferred_lft 1364sec
        inet 192.168.81.199/24 scope global secondary eno33554960:0
          valid_lft forever preferred_lft forever
    # ip addr flush eno33554960 //如果不指定特定网卡,则表示当前主机所有网卡ip地址被释放
    # ip addr show eno33554960
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff

使用ip route添加网络路由
    ###当前本机IP地址为172.24.8.131
    ###假定要到达目标地址为:192.168.10.0/24,下一跳为 172.24.8.2 则添加路由命令如下

    # route -n  //首先查看当前的路由
    Kernel IP routing table
    Destination    Gateway        Genmask        Flags Metric Ref    Use Iface
    0.0.0.0        192.168.81.2    0.0.0.0        UG    100    0        0 eno33554960
    172.24.8.0      0.0.0.0        255.255.255.0  U    100    0        0 eno16777728
    192.168.81.0    0.0.0.0        255.255.255.0  U    100    0        0 eno33554960
    # ip route list //首先查看当前的路由
    default via 192.168.81.2 dev eno33554960  proto static  metric 100 
    172.24.8.0/24 dev eno16777728  proto kernel  scope link  src 172.24.8.131  metric 100 
    192.168.81.0/24 dev eno33554960  proto kernel  scope link  src 192.168.81.144  metric 100

    # ip route add 192.168.10.0/24 via 172.24.8.2 dev eno16777728
    # ip route list |grep 192.168.10
    192.168.10.0/24 via 172.24.8.2 dev eno16777728 

使用ip route添加主机路由
    ###假定要到达目标地址为:192.168.20.1,下一跳为: 172.24.8.254 
    # ip route add 192.168.20.1 via 172.24.8.254
    # ip route list |grep 192.168.20.1
    192.168.20.1 via 172.24.8.254 dev eno16777728

    删除之前添加的网络路由和主机路由
    # ip route del 192.168.10.0/24  ###对于网络路由应指定掩码
    # ip route del 192.168.20.1

    # ip route list
    default via 192.168.81.2 dev eno33554960  proto static  metric 100 
    172.24.8.0/24 dev eno16777728  proto kernel  scope link  src 172.24.8.131  metric 100 
    192.168.81.0/24 dev eno33554960  proto kernel  scope link  src 192.168.81.144  metric 100 

删除缺省网关
    # ip route del default
    # ip route list|grep default

添加缺省网关
    # ip route add default via 172.24.8.2 dev eno16777728
    # ip route list|grep default
    default via 172.24.8.2 dev eno16777728 

配置特定网卡指定IP路由
    # ip addr add dev eno33554960 172.27.8.150/24  ###为eno33554960添加一个新ip
    # ip addr show eno33554960
    3: eno33554960:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:57:26:a7 brd ff:ff:ff:ff:ff:ff
        inet 192.168.81.144/24 scope global dynamic eno33554960
          valid_lft 1246sec preferred_lft 1246sec
        inet 172.27.8.150/24 scope global eno33554960
          valid_lft forever preferred_lft forever

    ###配置到达网络192.168.10.0经由172.27.8.2路由并且从这个ip 172.27.8.150收发数据包
    # ip route add 192.168.10.0 via 172.27.8.2 src 172.27.8.150 
    # ip route list |grep 172.27.8.150
    172.27.8.0/24 dev eno33554960  proto kernel  scope link  src 172.27.8.150 
    192.168.10.0 via 172.27.8.2 dev eno33554960  src 172.27.8.150

你可能感兴趣的:(Linux ip 命令使用示例)