Linux网络属性配置——ifcfg命令家族

ifconfig命令

  • 功能:可以手动启动、查看和修改网络接口的相关参数。
  • 用法:
    • ifconfig                                     //查看默认显示激活状态的网卡信息
    • ifconfig etho                       //显示指定网卡的信息
    • ifconfig -a                                //显示所有网卡的信息
    • ifconfig INTERFACE up|down]   //禁用或者启动某个网卡
    • ifconfig INTERFACE IP               //设定IP    
  • eg:

    # 查看所有网卡信息

     

 

    # 查看指定网卡信息

 

[li@centos7 ~]$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.22.12.233  netmask 255.255.0.0  broadcast 172.22.255.255
        inet6 fe80::7bf5:16a7:f734:b7c4  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:0f:5d:ea  txqueuelen 1000  (Ethernet)
        RX packets 287788  bytes 20207460 (19.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 993  bytes 197981 (193.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  # 给出指定的IP地址并激活

    1、方式一:#ifconfig IFACE  IP/MASK [up]

[root@localhost~]# ifconfig eno16777736 192.168.10.100/24 up
[root@localhost~]# ifconfig     # 发现地址已经激活
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.100  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fee5:2d6a  prefixlen 64 scopeid 0x20<link>
        ether 00:0c:29:e5:2d:6a  txqueuelen 1000  (Ethernet)
        RX packets 126  bytes 44856 (43.8 KiB)
        RX errors 0  dropped 0 overruns 0  frame 0
        TX packets 341  bytes 63010 (61.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0 collisions 0

[root@localhost~]# ping 192.168.10.100 # 查看是否能用这个地址

    2、方式二:#ifconfig IFACEIP netmask NETMASK

[root@localhost~]# ifconfig eno16777736 192.168.10.101 netmask 255.255.255.0
[root@localhost~]# ifconfig 
eno16777736:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.101  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fee5:2d6a  prefixlen 64 scopeid 0x20<link>
        ether 00:0c:29:e5:2d:6a  txqueuelen 1000  (Ethernet)
        RX packets 126  bytes 44856 (43.8 KiB)
        RX errors 0  dropped 0 overruns 0  frame 0
        TX packets 343  bytes 63150 (61.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0 collisions 0

  注意:ifconfig命令的网络配置只对当前系统有效

    在远程ssh时最好不要使用,可能网络环境不同,一旦不生效,可能要跑到机房重新设置

  • 为同一个网络接口配置多个网络IP地址:给接口名起别名的方式
    • 动态方式:

        ifconfig eth0:# IP netmask MASK

          如ifconfig eth0:0 192.168.1.177 netmask 255.255.255.0

    • 静态方式

         创建/etc/sysconfig/network-scripts/ifcfg-eth0:0文件,并配置即可。注意,文件中的DEVICE=eth0:0,且该接口处的IP地址不能动态的获取,需要手动设定。

    注意:当一块网卡上配置多个IP地址时,如eth0、eth0:0,如果禁掉eth0:0,eth0上的网卡依然生效。但直接禁掉物理网卡时(即eth0),其后面的配置(eth0:0)等都将被删除掉。

  • 网卡的混杂模式

    功能:将来实现网络监控的的重要一步,每个主机如果不是发送给自己的报文数据就会拒绝接受,启用混杂模式后不是自己的也可以接受

    • 实现:# ifconfig eth0 promisc
    • 取消:# ifconfig eth0 -promisc   

 

配置网关:route命令

  • 网关/路由:

    为了使主机能够访问到另一个子网,需要在设备里增加路由到子网络。两部主机之间一定要有路由才能互通TCP/IP的协定,否则就无法连线。

  • route功能:

    用来查看、修改路由的信息

  • 路由条目类型
    • 主机路由 :路由用来连接某台主机
    • 网络路由 :路由用来连接某个网络
    • 默认路由:匹配所有的包,但IP地址和子网掩码全部是零
  • 补充:
    • 主机的路由表:本主机与外部其他主机通信
    • 路由的路由表:网络报文的转发
  • 查看

    # route -n  :表示以数字格式显示路由信息

[li@centos7 ~]$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.18.1     0.0.0.0         UG    100    0        0 ens33
172.22.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

      显示信息:

      • destination,genmask:分别是network和netmask,也就是ip和子网掩码,组合成一个完整的网域
      • gateway:该网域是通过哪个gateway连接出去?如果显示0.0.0.0表示该路由是直接由本机传送,亦即可以透过区域网路的 MAC 直接传讯;如果有显示 IP 的话,表示该路由需要经过路由器 (通讯闸的帮忙才能够传送出去。
      • Flags:总共有多个旗标,代表的意义如下:                        

          U (route is up):该路由是启动的                                          

          G (use gateway):需要透过外部的主机 (gateway) 来转递封包

        

 

  • 添加

    # route add [-net|-host] target [netmask Nm] [gw GW] [[dev] if] 

  • 删除

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

 

  eg:如何使用route命令添加路由:

    # 主机路由 

      目标:192.168.1.3  经由的网关:172.16.0.1

[root@localhost ~]# route add -host 192.168.1.3 gw 172.16.0.1 dev eth0
  # dev:存在多个网卡时,表示经由哪个网

    

    # 网络路由 

      目标: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.0 dev eth0

    # 默认路由

       网关:172.16.0.1

[root@localhost ~]# route add -net 0.0.0.0 netmask 255.255.255.0 gv 172.16.0.1
或者
[root@localhost ~]# route add default gw 172.16.0.1

 

  eg:删除

    # 网络路由

      目标:192.168.0.0  网关:172.16.0.1

[root@localhost ~]# route del -net 192.168.0.0 gw 172.16.0.1

    # 默认路由

[root@localhost ~]# route del default

 

  

netstat命令

  • 用于显示网络相关信息,如网络连接,路由表,接口状态等
  • 显示路由信息

    # netstat -rn

      -r :显示路由表

      -n :数字格式显示

    eg:   

[root@centos7 ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.22.18.1     0.0.0.0         UG        0 0          0 ens33
172.22.0.0      0.0.0.0         255.255.0.0     U         0 0          0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0

  解释:ens33这个网络接口的默认网关(即路由)是172.22.18.1。gateway为0.0.0.0表示本地网络,不需要任何网关。

  • 显示网络连接:

  格式:

  # 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;

    常用组合:

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

    传输层协议:
      tcp:面向连接的协议;通信开始之前,要建立一个虚链路;通信完成后还要拆除连接;
      udp:无连接的协议;直接发送数据报文;

    eg:

      # 以数字格式显示所有的tcp连接    

[root@centos7 ~]# netstat -tan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 172.22.12.233:22 172.22.10.22:50118 ESTABLISHED
tcp 0 52 172.22.12.233:22 172.22.10.22:50119 ESTABLISHED
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:631 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN
[root@centos7 ~]#

  • 显示接口的统计数据

    格式:

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

    eg:

      # 显示所有接口    

[root@centos7 ~]# netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500   158566      0      0 0           383      0      0      0 BMRU
lo       65536        0      0      0 0             0      0      0      0 LRU
virbr0    1500        0      0      0 0             0      0      0      0 BMU

      # 显示指定接口 netstat -I<IFACE>

[root@centos7 ~]# netstat -Iens33
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
ens33     1500   159509      0      0 0           409      0      0      0 BMRU

 

配置主机名

  给自己的当前主机设置一个名字

  方式:

     1、指定命令

     2、配置文件:/etc/sysconfig/network

      • 指定命令    

          1、hostname

            查看:# hostnam

        •   配置:# hostname HOSTNAME

           注意:只对当前系统有效,重启后无效

        •     2、在CentOS7上的配置主机名的命令 : hostnamectl命令
        •      查看 : #hostnamectl  status:显示当前主机信息;
          •  配置:# hostnamectl set-hostname:设定主机名,永久有效

    eg:

      # 在CentOS7上显示主机信息

[root@centos7 ~]# hostnamectl status
   Static hostname: centos7
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 8e1320e3a6294bc3bc7fac06de8008a0
           Boot ID: f77ed060007342c3a8df8df3564ba596
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-514.el7.x86_64
      Architecture: x86-64
[root@centos7 ~]# 
      • 配置文件:重启后永久有效

        # 可以手动编辑/etc/sysconfig/network里面的信息

[root@centos7 sysconfig]# cat /etc/sysconfig/network
# Created by anaconda
HOSTNAME=CENTOS7
[root@centos7 sysconfig]# 

 

配置DNS服务器指向

  •  配置文件:
    •   /etc/resolv.conf
      • 因为可以有3个DNS,所以可以编辑3个

          nameserve DNS_SERVER_IP1

          nameserve DNS_SERVER_IP2

          nameserve DNS_SERVER_IP3       

    eg:

      # 可以手动编辑DNS,前提是DNS服务器可用

 

[root@centos7 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search corp.longshine.com
nameserver 172.22.1.10
[root@centos7 ~]# 

 

  如何测试:

    命令:host ,nslookup ,dig

    如果系统没有安装,则使用# yum -y install bind-utils即可

      1、# dig -t A FQDN(域名)

        正向解析:将域名解析为IP地址

      2、 # dig -x IP

        反向解析:将ip地址解析为域名 

      eg:

        # 使用dig和nslookup解析百度域名

[root@centos7 ~]# dig -t A www.baidu.com

; <<>> DiG 9.9.4-RedHat-9.9.4-37.el7 <<>> -t A www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42115
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;www.baidu.com.            IN    A

;; ANSWER SECTION:
www.baidu.com.        713    IN    CNAME    www.a.shifen.com.
www.a.shifen.com.    209    IN    A    61.135.169.125
www.a.shifen.com.    209    IN    A    61.135.169.121

;; Query time: 3 msec
;; SERVER: 172.22.1.10#53(172.22.1.10)
;; WHEN: 五 6月 16 20:14:19 AST 2017
;; MSG SIZE  rcvd: 101

[root@centos7 ~]# 
[root@centos7 ~]# nslookup www.baidu.com
Server:        172.22.1.10
Address:    172.22.1.10#53

Non-authoritative answer:
www.baidu.com    canonical name = www.a.shifen.com.
Name:    www.a.shifen.com
Address: 61.135.169.121
Name:    www.a.shifen.com
Address: 61.135.169.125

[root@centos7 ~]# 

  

一些补充:

  eg:

    # 查询升级`ip命令的程序包在哪里`(注意是反引号)

[root@localhost ~]# rpm -qf `which ip`
iproute-3.10.0-74.el7.x86_64

    # 显示ip及其有关命令在哪个文件下

[root@localhost ~]# rpm -ql iproute
/etc/iproute2
/etc/iproute2/ematch_map
/etc/iproute2/group
/etc/iproute2/nl_protos
/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
/usr/lib64/tc
......

 

    

 

 

 

 

 

 

 

  

 

 

  

 

posted @ 2017-06-16 10:49  FernLi  阅读(3892)  评论(1编辑  收藏  举报