本次操作的环境实在CentOS67的平台上。

linux系统上,对于网络属性的配置命令有很多。各有各的喜爱,本人就对目前linux系统上一些常用配置linux网络属性的命令进行简单总结。常用的命令有ifcfgipssnmcli

等命令。

网络属性配置之ifconfig

先对linux的老贵族网络属性管理的命令ifcfg进行简析说明。对于这个命令只要接触linux系统,应该都会有所了解。虽然它年代久远,但是仍旧不要抹去它给大家带来的便捷。对于ifconfigconfigure a network interface)命令,我个人的解释是配置网络接口。Ifconfig命令所常用的选项参数其实不多。常用的如下。

ifconfig [interface]

 #ifconfig iface [up|down]


ifconfig interface [aftype] options | address ...

# ifconfig iface ip/mask [up]

# ifconfig iface ip netmask mask 

对上面的一些面料进行简单的演示说明使用方法。如下:

[root@www ~]# ifconfig -a———显示系统上所有网络服务的使用情况。

eth0      Link encap:Ethernet  HWaddr 00:0C:29:D9:CC:36  

          inet addr:10.1.1.238  Bcast:10.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fed9:cc36/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:506005 errors:0 dropped:0 overruns:0 frame:0

          TX packets:102167 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:348119379 (331.9 MiB)  TX bytes:39244293 (37.4 MiB)

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:20 errors:0 dropped:0 overruns:0 frame:0

          TX packets:20 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:1470 (1.4 KiB)  TX bytes:1470 (1.4 KiB)

[root@www ~]# ifconfig eth0——显示单个网络的使用状态

eth0      Link encap:Ethernet  HWaddr 00:0C:29:D9:CC:36  

          inet addr:10.1.1.238  Bcast:10.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fed9:cc36/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:506060 errors:0 dropped:0 overruns:0 frame:0

          TX packets:102186 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:348124441 (331.9 MiB)  TX bytes:39248151 (37.4 MiB)

此外ifconfig命令还可以为网卡配置IP地址,但是ifconfig命令配置的ip地址是零时的,只能零时生效,一丹服务器重启,配置的ip地址就会消失。格式如下:

ifconfig interface [aftype] options | address ...

# ifconfig iface ip/mask [up]

# ifconfig iface ip netmask mask

实际演示如下:

[root@www ~]#ifconfig eth0 10.1.1.248/24 up

[root@www ~]# ifconfig eth0

\eth0      Link encap:Ethernet  HWaddr 00:0C:29:D9:CC:36  

          inet addr:10.1.1.248  Bcast:10.1.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fed9:cc36/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:506603 errors:0 dropped:0 overruns:0 frame:0

          TX packets:102390 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:348182070 (332.0 MiB)  TX bytes:39264902 (37.4 MiB)

为ip地址创建多个IP地址:如下

[root@www ~]# ifconfig eth0:0 192.168.1.2/24 

[root@www ~]# 

[root@www ~]# 

[root@www ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:D9:CC:36  

          inet addr:172.16.249.253  Bcast:172.16.255.255  Mask:255.255.0.0

          inet6 addr: fe80::20c:29ff:fed9:cc36/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:41402 errors:0 dropped:0 overruns:0 frame:0

          TX packets:21063 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:3707658 (3.5 MiB)  TX bytes:2713673 (2.5 MiB)

 

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:D9:CC:36  

          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

 

可以发现,IP地址配置已经完成生效了。对于ifconfig命令其实很简单,没过多的使用要求。

网络属性配置之ip

对于ipshow / manipulate routing, devices, policy routing and tunnel)命令我个人的翻译是显示路由器设备,策略路由等配置信息。Ip命令,我个人认为它非常的强大。它拥有很多的选项参数。接下来,对它一些常用的选项参数进行简单的说明。

 ip [ OPTIONS ] OBJECT { COMMAND | help }

       OBJECT := { link | addr | route | rule | neigh | tunnel | maddr | mroute | monitor }

       OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] |  inet |  ipx  link } 

 

1、首先,先说说link能够跟的options.对于ip link(network device configuration),我个人的翻译是网络服务配置。接下对其一些常用的进行简单的解释说明,如下:

ip [ OPTIONS ] link  { COMMAND | help }

ip link delete DEVICE type TYPE [ ARGS ]——删除链接

ip link set { DEVICE } { up | down  }

     alias NAME ——设备添加别名

ip link show [ DEVICE | group GROUP ]——查看设备连接情况。

接下来对上面的简单解析,演示一下。

[root@localhost ~]# 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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000

    link/ether 00:0c:29:1c:70:8e brd ff:ff:ff:ff:ff:ff

[root@www ~]# ip link set eth0 alias eth0:0

[root@www ~]# 

[root@www ~]# ip link 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

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:d9:cc:36 brd ff:ff:ff:ff:ff:ff

alias eth0:0——设备的别名创建成功。

[root@www ~]#

2、接下来说明ip OBJECT 之addr的一些常用选项参数。想来ip命令使用最多也就是ipaddr的配置等一下操作,因为对其要重点的掌握。Ip addr(ip address - protocol address management.)个人翻译的意思是地址配置协议管理。

首选说明其添加网络属性的一些命令的解释,如下:

 ip addr { add | del } IFADDR dev STRING

 ip addr { show | flush } [ dev STRING ] [ scope SCOPE-ID ][ label PATTERN]

 [ scope SCOPE-ID ]有四个作用域:

    global - the address is globally valid.——应用于全局

site - (IPv6 only) the address is site local, i.e. it is valid inside this site.

这项是对ipv6才有效,所以没必要去过多了解。

    link - the address is link local, i.e. it is valid only on this device.——仅连接有效

    host - the address is valid only inside this host.——仅对主机本身有效

接下来对上面的命令进行简单的演示:

[root@www ~]# ip addr add  192.168.1.22/32 dev eth0——添加ip地址。

[root@www ~]# 

[root@www ~]# ip addr show eth0——查看ip地址命令

1: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:d9:cc:36 brd ff:ff:ff:ff:ff:ff

    inet 172.16.249.253/16 brd 172.16.255.255 scope global eth0

    inet 172.16.30.2/32 scope global eth0——能够发现,在没有指定作用域的时候,默认是属于全局的。

    inet6 fe80::20c:29ff:fed9:cc36/64 scope link 

       valid_lft forever preferred_lft forever

既然能添加IP地址,那么相对应的也能删除ip地址。使用格式如下;

ip addr del IPDDR dev eth0

 

[root@www tmp]# ip addr del 172.16.249.253 dev eth0

Warning: Executing wildcard deletion to stay compatible with old scripts.

         Explicitly specify the prefix length (172.16.249.253/32) to avoid this warning.

         This special behaviour is likely to disappear in further releases,

         fix your scripts!

 

Ip地址删除之后,原来的主要ip就会被删除。之前配置的全局IP地址就会被顶上。

[root@www ~]# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:d9:cc:36 brd ff:ff:ff:ff:ff:ff

    inet 172.16.30.2/32 scope global eth0

    inet6 fe80::20c:29ff:fed9:cc36/64 scope link 

       valid_lft forever preferred_lft forever

 

对于删除IP地址的命令除了del之外,还可以使用flush选项参数。它也是删除命令之意。执行这条命令之后,对应网络接口的ip地址都会被清楚。使用而是如下:

ip addr flush IPADDR

[root@www ~]#ip addr flush dev eth0 secondary

 

3、既然能可以管理端口的ip地址,那么相对应的也可以管理route。能够为服务器指定路由条路。其命令的使用格式如下:

ip route add TARFET via GW dev IFACE SRC SOURCE_IP

[root@www ~]# ip route add 192.168.1.1/32 via 172.16.0.1 dev eth0

[root@www ~]# 实际使用格式如上

[root@www ~]# 

[root@www ~]# ip route show——查看route详细信息

192.168.1.1 via 172.16.0.1 dev eth0 

172.16.0.0/16 dev eth0  proto kernel scope link  src 172.16.249.253  metric 1 

default via 172.16.0.1 dev eth0  proto static 

[root@www ~]# 

其实除了能够配置路由之外,还可以为网络端口指定默认网络。使用格式如下:

ip route add default via GW dev IFACE

[root@www ~]# ip route add default via 172.16.0.1 dev eth0

[root@www ~]# 实际使用格式如上

[root@www ~]# ip route show

192.168.1.1 via 172.16.0.1 dev eth0 

172.16.0.0/16 dev eth0  proto kernel scope link  src 172.16.249.253  metric 1 

default via 172.16.0.1 dev eth0——添加默认路由成功。

 

既然能添加ip地址和路由地址,那么也可以删它们。接下来就去删除它们。

[root@www ~]# ip add del 192.168.1.1 dev eth0

Warning: Executing wildcard deletion to stay compatible with old scripts.

Explicitly specify the prefix length (192.168.1.1/32) to avoid this warning.

 This special behaviour is likely to disappear in further releases,fix your scripts!

[root@www ~]# 

[root@www ~]# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:d9:cc:36 brd ff:ff:ff:ff:ff:ff

inet 172.16.249.253/16 brd 172.16.255.255 scope global eth0

通过查看IP地址表,就能很好的发现。已经被删除了,至于路由删除其实也ip地址删除类似。其实ip命令还有很多选项参数,这里只显示了一部分,其他可以通过man命令查看其使用情况。

 

网络属性配置之查看命令ss

 

接下来说说网络状态查看工具,其实工具也有几个。如netstat和ss命令。其实这另个命名的使用方法很接近。Netstat只早先用的命令,ss后起之秀。接下我主要说说ss的命令简单使用方法。

先来对ss - another utility to investigate sockets命令解释下。翻译成中文的意思是

另外设备控制显示工具。

格式:ss [option]...[FILTER]

选项:

-t TCP

-u UDP

-a ALL

-l LISTEN

-n 数字格式

-p 所有进程PID

-m 内存

下面是ss一些常用格式

ss -t -a

     Display all TCP sockets.

显示所有的tcp套接字信息

ss -u -a

      Display all UDP sockets.

显示所有的UDP套接字信息

ss -o state established ’( dport = :ssh or sport = :ssh )’

      Display all established ssh connections.

显示所有的连接到ssh源端口或者目的端套接字信息

 

接下来对上面简单说明的进行演示:

[root@www etc]# ss -tan

State         Recv-Q  Send-Q     Local Address:Port              Peer Address:Port 

LISTEN      0         128        :::22                         :::*  

LISTEN      0         128        *:22                         *:*  

LISTEN      0         52       172.16.249.253:22              172.16.250.168:52871  

LISTEN      0         0        172.16.249.253:22              172.16.250.168:52713

对于ss的用法其实和netstat很想死,两者掌握一个即可。

 

网络属性配置之配置文件

就个人而言,其实网络属性的配置很简单。但是还是对其里面的参数做简单的说明介绍。网络配置文件的所在位置如下:

[root@www network-scripts]# pwd

/etc/sysconfig/network-scripts

[root@www network-scripts]# ll ifcfg-eth0 

-rw-r--r--. 3 root root 174 Aug 15 18:31 ifcfg-eth0

centOS6上,网络属性的配置文件是ifcfg-eth0,但是在centOS7上,文件名不一样。这里就不过多介绍。接下来对其里面的配置进行简单说明:

[root@www network-scripts]# vim ifcfg-eth0 

DEVICE=eth0——设备名称

BOOTPROTO=dhcp——网卡以dhcp协议的方式获取IP,有三种选择{static|none|dhcp}

HWADDR=00:0c:29:d9:cc:36——网卡的物理地址

NM_CONTROLLED=yes——NMNetworkManager的简写;此网卡是否接受NM控制;

ONBOOT=yes——开机是否启动网卡服务

TYPE=Ethernet——网络类型

UUID="c6c05d52-767a-47cc-9e5d-59a8e4cd8b7c"——网卡的ID

IPV6INIT=no——是否开启ipv6的功能

USERCTL=no——普通用户是否可以控制;

PEERDNS=yes——是否允许dhcp server分配的dns服务器指向信息直接覆盖至/etc/resolv.conf文件中

除此之外,如何bootproto采取的是none或者static的时候,配置文件中还会有一些其他的选项。如下

IPADDR=192.168.10.10——手动配置的ip地址
NETMASK=255.255.255.0——ip地址的子网掩码

GATEWAY=192.168.10.1——默认网关

DNS1=202.106.0.20——第一DNS

DNS2=8.8.8.8——备用DNS

 

如果在网络属性的配置文件里设置了DNS,那么在/etc/resolv.conf就不会生效。因为它的优先级没有网卡的高

[root@www network-scripts]# ll /etc/resolv.conf 

-rw-r--r-- 1 root root 103 Aug 25 15:57 /etc/resolv.conf

 

既然网卡的IP地址配置解释了,顺带把路由的配置文件也简单的提下。默认情况下,centos6上是没有提供路由的配置文件,需要自己去创建。/etc/sysconfig/network-scripts/route-IFACE

两种风格:

(1) TARGET via GW

(2) 每三行定义一条路由

ADDRESS#=TARGET

NETMASK#=mask

GATEWAY#=GW

[root@www ~]# ip route show

192.168.0.0/24 via 172.16.0.1 dev eth0  proto static