网络配置——命令家族

Linux网络属性配置命令:

  • ifcfg家族:ifconfig,route,netstat


ifconfig命令:接口及地址查看和管理

         ·ifconfig [INTERFACE]

             #ifconfig -a:显示所有接口,包括lnactive状态(不是up状态)的接口

        

         ·ifconfig interface [aftype] options | address…

                 #ifconfig IFACE IP/MASK [up|down]

                 #ifconfig IFACE IP netmask NETMASC [up|down]

                

                 options:

                     [-]promisc

                

                 注意:立即送往内核中的TCP/IP协议栈,并生效

        

         ·管理IPv6地址:

                 add addr/prefixlen

                 del addr/prefixlen

route命令:路由表查看及管理

         ·路由条目类型:

                 网络路由:到网络的路由条目,目标地址为IP网络

                 主机路由:到主机的路由条目,目标地址为单个IP

                 默认路由:0.0.0.0/0.0.0.0,目标地址为任意主机

        

         ·查看:

             route -n

        

         ·添加:

                 route add  [-net|-host]  target  [netmask  Nm] [gw Gw] [[dev] If]

                     eg;route add -net 192.168.0.0/24 gw 198.168.1.1 dev eth1

                     route add -net 0.0.0.0/0.0.0.0 gw 192.168.1.1

                     route add default gw 192.168.1.1

        

         ·删除:

                 route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]

                     eg: route del -net 192.168.0.0/24

                     route del -default

netstat命令:

         Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

        

         ·显示路由:netstat -rn

                 -r:显示内核路由表

                 -n: 数字格式

        

         ·显示网络链接:

             netstat [–tcp|-t] [–udp|-u]

                     -t:TCP协议的相关链接,链接均有其状态;FSM(Finate State Machine)

                     -u:UDP相关的链接

                     -w:raw socket相关的链接

                     -l:处于监听状态的链接

                     -a:所有状态

                     -n:以数字格式显示IP和Port

                     -e:扩展格式

                     -p:显示相关的进程及PID

                

             常见的组合:

                 -tan, -uan, -tnl, -unl, -tunlp

        

         ·传输层协议:

                 tcp;面向链接的协议;通信开始之前,要建议一个虚连链接,通信完成后还有拆除此链接

                 udp:无链接的协议;直接发送数据报文

        

         ·显示接口的统计数据

             netstat  {–interfaces|-I|-i}  [iface]  [–all|-a]  [–extend|-e]  [–verbose|-v]  [–program|-p] [–numeric|-n]

        

         ·所有接口:

             netstat -i

         ·指定接口:

             netstat -I<IFACE>

ifup/ifdown命令:

  注意:通过配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE来识别接口并旺财配置

  • 配置主机名:


hostname命令:

        查看:hostname

        配置:hostname HOSTNAME

            当前系统有效,重启无效

hostnamectl命令:

        hostnamectl status;显示当前主机名的信息

        hostanemctl set-nostname:设定主机名,永久有效

        

配置文件:

        CentOS6:/etc/sysconfig/network

        CentOS7:/etc/hostanme

                HOSTNAME=<HOSTNAME>

                    注意:此方法设置的不会立即生效,但下一次重启后永久有效


  • 配置DNS服务器指向:


配置文件:/etc/resolv.conf

        nameserver DNS_SERVER_IP

        

        如何测试:

                #dig -t A FQDN

                    FQDN–>IP

                

                #dig -x IP

                    IP–>FQDN

  • iproute2家族


ip命令:

        show / manipulate routing, devices, policy routing and tunnels

        

        ip [ OPTIONS ] OBJECT { COMMAND | help }

                OBJECT = { link | addr | route  }

                注意OBJECT可简写,格OBJECT的子命令也可简写

                

                ·ip link:network device configuration

                

                        ip link set – change device attributes

                                dev NAME (default):指明要管理的设备,dev关键字可省略

                                up 和 down:启动或关闭网络接口

                                multicast on 或 multicast off:启用或关闭多播功能

                                name NAME:重命名接口     //必须先down掉接口,才能该名

                                mtu NUMBER:设置MTU的大小,默认为1500

                                netns PID:ns为namespace,用于将接口移动到指定的网络名称空间

                        

                        ip link show – display device attributes

                        

                        ip link help – 显示简要使用帮助

                

                ·ip netns:ip – manage network namespaces

                

                        ip netns list:列出所有的netns

                        ip netns add NAME:创建指定的netns

                        ip netns del NAME:删除指定的netns

                        ip netns exec NAME COMMAND:在指定的netns中运行命令

                

                ·ip address-protocol address management

                

                        ip address add – add new protocol address

                            ip addr add IFADDR dev IFACE

                            [label NAME]:为额外添加的地址指明接口别名

                                  在ifconfig命令中,没有别名的ip地址默认不显示

                            [broadcast ADDRESS]:广播地址,不设置会根据IP和NETMASK自动计算得到

                            [scope SCOPE_VELUE]:

                                    global:全局可用

                                    link:接口可用

                                    host:仅主机可用

                        

                        ip address del – delete protocol address

                              ip addr delete IFADDR dev IFACE

                        

                        ip address show – look at protocol addresses

                               [IFACE]:仅显示指定接口的地址

                        

                        ip address flush – flush protocol address

                              ip addr fulsh dev IFACE

                

                ·ip route – routing tabel 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]

                                

                                      eg: #ip route add 192.158.0.0/24 via 10.0.0.1 dev eth1 src 10.0.20.100

                                       #ip route add default via GW

                        

                        ip route delete – delete route

                                ip route del TYPE PREFIX

                                       eg: #ip route delete 192.168.1.0/24

                        

                        ip route show – list routes

                              TYPE PREFIX

                        

                        ip route flush – flush routing tables

                               TYPE PREFIX

                        

                        ip route get – get a single route

                                ip route get TYPE PREFIX

                                      eg: #ip route get 192.168.0.0/24

ss命令:

        ss [options] [FILTER]

                选项:

                        -t:TCP协议的相关链接

                        -u:UDP协议的相关链接

                        -w:raw socket相关的链接

                        -l:监听状态的链接

                        -a:所有的链接

                        -n:数字格式

                        -p:相关的进程及其PID

                        -e:扩展格式信息

                        -m:内存用量

                        -o:计时器信息

                

                FILTER= [ state TCP-STATE ] [ EXPRESSION ]

                        TCP的常见状态:

                                TCP FSM:

                                LISTEN:监听

                                ESTABLISEHD:建立的链接

                                FIN_WAIT_1

                                FIN_WAIT_2

                                SYN_SENT

                                SYN_RECV

                                CLOSED:关闭的链接

                        

                        EXPRESSION:

                                dport=

                                sport=

                                        示例:'( dport = :22 or sport = :22 )'

                                                #ss -tan '( dport = :22 or sport = :22 )'

                                                #ss -tan state ESTABLISHED

  • 配置文件:


·IP/NETMASK/GW/DNS等属性的配置文件:/etc/sysconfig/network-scripts/ifcfg-IFACE

    IFACE:接口名称

·路由的相关配置文件(默认无此文件):/etc/sysconfig/network-scripts/route-IFACE

·配置文件/etc/sysconfig/network-scritps/ifcfg-IFACE通过大量参数来定义接口的属性;其可通过vim等配置文本编辑器直接修改,也可以使用专用的命令进行修改

    CentOS6:system-config-network(setup)

    CentOs7: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:子网掩码;CentOS7支持使用PREFIX

        GATEWAY:默认网关

        USERCTL:是否允许普通用户控制此设备

        PEERDNS:如果BBOTPROTO的值为“dhcp”,是否运行dhcp server分配的dns服务器指向覆盖本地手动指向的DNS服务器指向,默认为允许

        HWADDR:设备MAC地址

        

        NM_CONTROLLED:是否使用NetworkManager服务来控制接口


·网卡配置文件中必须要有的选项

         DEVICE

         IPADDR

         PREFIX / NETWORK

         GATEWAY

         DNS1

         DNS2


·网络服务:

    network,NetworkManager

    

    管理网络服务:

        CentOS6:service SERVICE {start|stop|restart|status}

        CentOS7:systemctl {start|stop|restart|status} SERVICE[.service]

    

    配置文件修改之后,如果要生效,需要重启网络服务

        CentOS6:#service network restart

        CentOS7:#systemtcl restart network.service

·网关路由文件:/etc/sysconfig/network-scripts/route-IFACE

    支持两种配置方式,但不可混用

        (1)每行一个路由条目:

            TARGRT via GW

        

        (2)每三行一个路由条目

            ADDRESS#=TARGRT

            NETMASK#=MASK

            GATEWAY#=NEXTHOP

            #:0,1,2,3


原创文章,作者:megedugao,如若转载,请注明出处:http://www.178linux.com/43966

(0)
megedugaomegedugao
上一篇 2016-09-07
下一篇 2016-09-07

相关推荐

  • 计算机组成及Linux基础

    计算机组成及Linux基础

    Linux干货 2018-03-04
  • DHCP服务

    dhcp server: DHCP:Dynamic Host Configuration Protocol     arp –> address resolving Protocol     &nbsp…

    Linux干货 2016-11-01
  • LVS之nat&dr

    Evernote Export 负载均衡集群设计时的要点:        (1)session保持            session sticky(ip hash)            sess…

    Linux干货 2016-12-01
  • 魔术方法

    魔术方法 属性 含义 __name__ 类、函数、方法等的名字 __module__ 类定义所在的模块名 __class__ 对象或类所属的类 __bases__ 类的基类的元组,舒徐为他们在基类列表中出现的顺序 __doc__ 类、函数的文档字符串,如果没有定义则为None __mro__ 类的mro,class.mro()返回的结果保存在__mro__中…

    Linux干货 2017-11-21
  • The first work’s homework

    一、描述计算机的组成及其功能     计算机是由硬件系统(hardware system)和软件系统(software system)两部分组成的。     1.硬件系统:       根据冯诺依曼计算机体系结构模型,中…

    Linux干货 2016-12-05
  • Centos6.5上搭建openvpn

    一、openvpn原理 二、安装openvpn 三、制作相关证书     3.1 制作CA证书     3.2 制作Server端证书     3.3 制作Client端证书 四、配置Server端 五、配置C…

    Linux干货 2016-04-28