Common network management commands

@ (Commonly used network management commands)

ifconfig

ifconfig command: View and manage the interface and address

使用:

    ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
    [root@node0 ~]# ifconfig        #直接使用显示当前所有以启用接口信息
    [root@node0 ~]# ifconfig -a     #显示所有接口信息,包括down掉的接口
    [root@node0 ~]# ifconfig -s     #显示接口统计信息 
    Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
    docker0          1500        0      0      0 0             0      0      0      0 BMU
    eth0             1500   348847      0      0 0        925801      0      0      0 BMRU
    lo              65536       33      0      0 0            33      0      0      0 LRU
    
    # ifconfig  IFACE  IP/MASK  [up|down]
    # ifconfig  IFACE  IP  netmask  NETMASK  
    [root@node0 ~]# ifconfig eth1 192.168.0.100/24 up
    [root@node0 ~]# ifconfig eth1 192.168.0.100 netmask 255.255.0.0 up      #给eth1配置地址并启用

    options:
        [-]promisc
    [root@node0 ~]# ifconfig eth1 promisc   #启用混杂模式
    [root@node0 ~]# ifconfig eth1 -promisc  #取消混杂模式
    
    注意:命令修改接口配置立即送往内核中的TCP/IP协议栈,并生效;
        
    管理IPv6地址:
        ifconfig IFACE add addr/prefixlen
        ifconfig IFACE del addr/prefixlen

route command

View and manage routing

Route Entry type:
a host route: a single IP destination address;
Network Routing: Destination IP network;
default route: target any network, 0.0.0.0 / 0.0.0.0
Priority: host routes> Network Routing> Default Route
View:

        # route  -n     #-n选项不把IP地址反解为主机名,也可不加-n。

Add to:

        route  add  [-net|-host]  target  [netmask  Nm]  [gw GW] [metric N] [[dev] If]
            
        示例:
        [root@node0 ~]# route add -host 192.168.20.100 gw 172.16.100.110 dev eth0       #到达目标主机192.168.20.100都指向172.16.100.110,添加路由的主机需要与路由下一条在同一网络内才能添加成功
        [root@node0 ~]# route add -net 192.168.20.0/24 gw 172.16.100.110
        [root@node0 ~]# route add default gw 172.16.100.110         

delete:

        route  del  [-net|-host] target  [gw Gw]  [netmask Nm]  [[dev] If]
                
        示例: 
        [root@node0 ~]# route del -net 192.168.20.0/24 gw 172.16.100.110 dev eth0
        [root@node0 ~]# route del default

netstat command

显示网络连接,路由表,接口状态等信息  

显示路由表:netstat  -rn  #等同于route -n
        -r:显示内核路由表
        -n:数字格式
        
显示网络连接:
    netstat  [--tcp|-t]  [--udp|-u]  [--udplite|-U]  [--sctp|-S]  [--raw|-w]  [--listening|-l]  [--all|-a]  [--numeric|-n]   [--extend|-e[--extend|-e]]  [--program|-p]
    -t:TCP协议的相关连接,连接均有其状态;FSM(Finate State Machine);
    -u:UDP相关的连接
    -w:raw socket相关的连接
    -l:处于监听状态的连接
    -a:所有状态
    -n:以数字格式显示IP和Port;
    -e:扩展格式
    -p:显示相关的进程及PID;
    -c:持续输出网络状态     
示例:
    [root@node0 ~]# netstat -tan
    [root@node0 ~]# netstat -uan
    [root@node0 ~]# netstat -tnl
    [root@node0 ~]# netstat -unl
    [root@node0 ~]# netstat -tunlp
            
显示接口的统计数据:
    netstat    {--interfaces|-I|-i}    [iface]   [--all|-a]   [--extend|-e]   [--verbose|-v]   [--program|-p]  [--numeric|-n]
            
    所有接口:
        netstat  -i     #与ifconfig -s相同
    指定接口:
        netstat  -I<IFace>
        [root@node0 ~]# netstat -Ieth0
        [root@node0 ~]# netstat -I=eth0
    

ifup / ifdown command

[root@node0 ~]# ifdown eth1
[root@node0 ~]# ifup eth1
注意:通过配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE来识别接口并完成配置;

Configuring the host name

hostname命令:
    查看:hostname
    配置:hostname  HOSTNAME
    [root@node0 ~]# hostname test
    [root@node0 ~]# hostname
    test

    当前系统有效,重启后无效;
    
hostnamectl命令(CentOS 7):
    hostnamectl  status:显示当前主机名信息;
    hostnamectl  set-hostname:设定主机名,永久有效;
    [root@node0 ~]# hostnamectl set-hostname test

配置文件:/etc/sysconfig/network
    HOSTNAME=<HOSTNAME> 
注意:此方法的设置不会立即生效; 但以后会一直有效;

Configuring DNS server to point

配置文件:/etc/resolv.conf
    nameserver   DNS_SERVER_IP

测试(host/nslookup/dig):
    # dig  -t  A  FQDN
        FQDN --> IP
            
    # dig  -x  IP
        IP --> FQDN
            

ip command

Linux hosts to display or manipulate routing, network devices, policy routing and tunnels, is a powerful Linux newer features network configuration tool

使用:
语法:ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | addr | route | netns  }
    
注意: OBJECT可简写,各OBJECT的子命令也可简写;
        
ip link: network device configuration
        
    ip link set - change device attributes
        dev NAME (default):指明要管理的设备,dev关键字可省略;
        up和down:
        [root@node0 ~]# ip link set eth1 down
        [root@node0 ~]# ip link set eth1 up
            
        multicast on或multicast off:启用或禁用多播功能;
        [root@node0 ~]# ip link set eth1 multicast off
        [root@node0 ~]# ip link set eth1 multicast on
            
        name NAME:重命名接口,重命名需要先将接口down掉才能修改
        [root@node0 ~]# ip link set eth1 name new-eth1
            
        mtu NUMBER:设置MTU的大小,默认为1500;
        [root@node0 ~]# ip link set eth1 mtu 1400

        netns PID:ns为namespace,用于将接口移动到指定的网络名称空间;
        [root@node0 ~]# ip link set eth1 netns testnet              需要先创建netns:
        [root@node0 ~]# ip netns add testnet
        
    ip  link  show  - display device attributes
            
    ip  link  help -  显示简要使用帮助;
            
    ip netns:  - manage network namespaces.
        
        ip  netns  list:列出所有的netns
            
        ip  netns  add  NAME:创建指定的netns
        [root@node0 ~]# ip netns add testnet

        ip  netns  del  NAME:删除指定的netns
            
        ip  netns   exec  NAME  COMMAND:在指定的netns中运行命令
        将eth1移动到testnet命名空间中
        ip link set eth1 netns testnet
        此时ip link show将看不到eth1
        [root@node0 ~]# ip link show
        1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
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 mode DEFAULT group default qlen 1000
link/ether 00:0c:29:6c:9c:e7 brd ff:ff:ff:ff:ff:ff
        4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
link/ether 02:42:5b:93:86:1d brd ff:ff:ff:ff:ff:ff
        
        在testnet中运行ip link show 命令
        [root@node0 ~]# ip netns exec testnet ip link sh
        1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 00:0c:29:6c:9c:f1 brd ff:ff:ff:ff:ff:ff
    ip address - protocol address management.
            
        ip address add - add new protocol address
            使用:ip  addr  add  IFADDR  dev  IFACE
                [label NAME]:为额外添加的地址指明接口别名;
                [broadcast ADDRESS]:广播地址;会根据IP和NETMASK自动计算得到;
                [scope SCOPE_VALUE]:
                    global:全局可用;
                    link:接口可用;
                    host:仅本机可用;                             
                    示例:
            [root@node0 ~]# ip addr add 192.0.0.2/24 dev eth1 label eth1:1
            
            [root@node0 ~]# ip addr add  192.168.1.20/24 dev eth0
            [root@node0 ~]# ip addr ls dev eth0
            2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
                link/ether 00:0c:29:6c:9c:e7 brd ff:ff:ff:ff:ff:ff
                inet 172.16.100.100/24 brd 172.16.100.255 scope global noprefixroute eth0
                    valid_lft forever preferred_lft forever
                inet 192.168.1.20/24 scope global eth0
                    valid_lft forever preferred_lft forever
                inet6 fe80::b36b:e4ec:d650:2528/64 scope link noprefixroute 
                    valid_lft forever preferred_lft forever
        ip address delete - delete protocol address
            使用:ip addr  delete  IFADDR  dev  IFACE 
        [root@node0 ~]# ip addr del 192.168.1.20/24 dev eth0            
        ip address show - look at protocol addresses
            使用:ip  addr   list  [IFACE]:显示接口的地址;
        [root@node0 ~]# ip addr show eth0
        [root@node0 ~]# ip addr list eth0
        
        ip address flush - flush protocol addresses
            使用:ip  addr  flush  dev  IFACE
        [root@node0 ~]# ip addr flush dev eth0      
    
    ip route - routing table management
        
        ip route add - add new route
        ip route change - change route
        ip route replace - change or add new one
            使用:ip  route   add  TYPE PREFIX  via GW  [dev  IFACE]  [src SOURCE_IP]
                
        示例:
        [root@node0 ~]# ip route add 192.168.0.0/24  via 10.0.0.1  dev eth1 src  10.0.20.100
        [root@node0 ~]# ip route add  192.168.0.0/24 via 172.16.100.110
        [root@node0 ~]# ip route add default via 172.16.100.2                       
                
        ip route delete - delete route      
        示例:
            [root@node0 ~]# ip route del 192.168.0.0/24 via 172.16.100.110 dev eth0
                    
        ip route show - list routes
        示例:
            [root@node0 ~]# ip route ls 172.16.100.0/24
            [root@node0 ~]# ip route sh 172.16.100.0/24

        ip route flush - flush routing tables
        示例:
            [root@node0 ~]# ip route flush 172.16.100.0/24

        ip route get - get a single route
        示例:
        [root@node0 ~]# ip route get 192.179.10.0/24
        
        

ss command

Netstat command usage and function very similar, stronger than netstat, you can filter

    ss  [options]  [ FILTER ]
    选项:
        -t:TCP协议的相关连接
        -u:UDP相关的连接
        -w:raw socket相关的连接
        -l:监听状态的连接
        -a:所有状态的连接
        -n:数字格式
        -p:相关的程序及其PID
        -e:扩展格式信息
        -m:内存用量
        -o:计时器信息
            
    FILTER := [ state TCP-STATE ]  [ EXPRESSION ]
    
    TCP的常见状态:
        established
        syn-sent
        syn-recv
        fin-wait-{1,2}
        time-wait
        closed
        close-wait
        last-ack
        listen
        closing
                
    EXPRESSION:
        dport = 
        sport = 
    示例:'( dport = :22 or sport = :22)'
    [root@node0 ~]# ss -tan '( dport = :22 or sport = :22 )'
    [root@node0 ~]# ss -tan state established

Use the command to configure IP / ROUTE take effect only temporarily, would be lost after the restart, you want to permanently save the configuration needs to be written into the configuration file
configuration file:
IP / NETMASK / GW / attributes such as the DNS configuration file: / etc / sysconfig / network -scripts / the ifcfg-IFACE
IFACE: interface name;
routing configuration file: / etc / sysconfig / networkj- scripts / route-IFACE

配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE通过大量参数来定义接口的属性;其可通过vim等文本编辑器直接修改,也可以使用专用的命令的进行修改(CentOS 6:system-config-network (setup),CentOS 7: nmtui)

    ifcfg-IFACE配置文件参数:
        DEVICE:此配置文件对应的设备的名称;
        ONBOOT:在系统引导过程中,是否激活此接口;
        UUID:此设备的惟一标识;
        IPV6INIT:是否初始化IPv6;
        BOOTPROTO:激活此接口时使用什么协议来配置接口属性,常用的有dhcp、bootp、static、none;
        TYPE:接口类型,常见的有Ethernet, Bridge;
        DNS1:第一DNS服务器指向;
        DNS2:备用DNS服务器指向;
        DOMAIN:DNS搜索域;
        IPADDR: IP地址;
        NETMASK:子网掩码;CentOS 7支持使用PREFIX以长度方式指明子网掩码;
        GATEWAY:默认网关;
        USERCTL:是否允许普通用户控制此设备;
        PEERDNS:如果BOOTPROTO的值为“dhcp”,是否允许dhcp server分配的dns服务器指向覆盖本地手动指定的DNS服务器指向;默认为允许;
        HWADDR:设备的MAC地址;
        
        NM_CONTROLLED:是否使用NetworkManager服务来控制接口;
        
    网络服务:
        network
        NetworkManager 
        
    管理网络服务:
        CentOS 6:  service  network  {start|stop|restart|status}
        CentOS 7:systemctl  {start|stop|restart|status}  network.service
            
    配置文件修改之后,如果要生效,需要重启网络服务;
        CentOS 6:# service  network  restart
        CentOS 7:# systemctl  restart network.service
            
用到非默认网关路由:/etc/sysconfig/network-scripts/route-IFACE
    支持两种配置方式,但不可混用;
        (1) 每行一个路由条目:
            TARGET  via  GW
            192.168.0.0/24 via 172.16.100.110
            
        (2) 每三行一个路由条目:
            ADDRESS#=TARGET
            NETMASK#=MASK
            GATEWAY#=NEXTHOP
            

A plurality of address to the interface:
outside ip addr, ifconfig or configuration files can be;

(1) ifconfig  IFACE_LABEL  IPADDR/NETMASK
[root@node0 ~]# ifconfig eth1:2 172.19.0.3/24
    
(2) 为别名添加配置文件;
    DEVICE=IFACE_LABEL
    BOOTPROTO:网络别名不支持动态获取地址;
        static, none

Guess you like

Origin www.cnblogs.com/type1818/p/11369932.html