Linux系统网络配置与管理

1、图形化网络配置工具

1)system-config-network(或system-config-network-tui)

2)setup

(2)cnetos7下图形化网络配置管理工具

1)nm-connection-editor(需要在图形化界面终端中使用)

2)nmtui

3)nmcli

2、ifconfig命令的使用(centos7上默认没有此命令,需要安装net-tools包)

(1)查看主机ip

1)ifconfig    #查看所有接口网络配置信息

2)ifconfig interface#查看指定接口的网络配置信息

(2)启用或者停用网卡

1)关闭网卡:ifconfig eth0down

2)启用网卡:ifconfig eth0up

(3)修改配置IP地址

1)设置修改IP地址:ifconfig eth1 ipaddress/netmask(临时生效)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:D3:7B:9B

          inet addr:172.16.254.250  Bcast:172.16.255.255  Mask:255.255.0.0

          inet6 addr:fe80::20c:29ff:fed3:7b9b/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500 Metric:1

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

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

          collisions:0 txqueuelen:1000

          RX bytes:2833396 (2.7 MiB)  TX bytes:25918 (25.3 KiB)

(4)配置网卡别名:ifconfig eth0:0 ipaddress/netmaskup

eth0:0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1280

        inet 6.6.6.6  netmask 255.255.255.0  broadcast 6.6.6.255

        ether 00:0c:29:ed:0f:11  txqueuelen 100  (Ethernet)

3、路由管理命令router

(1)查看路由:route –n

Kernel IP routing table

Destination    Gateway        Genmask        Flags Metric Ref    Use Iface

192.168.80.0    0.0.0.0        255.255.255.0  U    1      0        0 eth1

172.16.0.0      0.0.0.0        255.255.0.0    U    1      0        0 eth0

0.0.0.0        172.16.0.1      0.0.0.0        UG  0      0        0 eth0

(2)添加与删除

添加路由:route add[-net|-host] target [netmask Nm] [gwGw] [[dev] If]

删除路由:route del[-net|-host] target [gwGw] [netmask Nm] [[dev] If]

(3)配置路由案例:

Linux配置路由拓扑图

1)Linux1的配置

[root@linux1 ~]# echo 1>  /proc/sys/net/ipv4/ip_forward  #开启主机路由转发功能

[root@linux1 ~]# route add -net192.168.2.0/30 gw 192.168.1.2

[root@linux1 ~]# route add -net10.0.0.0/24 gw 192.168.1.2           

[root@linux1 ~]# route

Kernel IP routing table

Destination    Gateway        Genmask        Flags Metric Ref    Use Iface

default        server.magelinu 0.0.0.0        UG  100    0        0 eth2

10.0.0.0        192.168.1.2    255.255.255.0  UG  0      0        0 eth0

172.16.0.0      0.0.0.0        255.255.0.0    U    100    0        0 eth2

192.168.1.0    0.0.0.0        255.255.255.252 U    100  0        0 eth0

192.168.2.0    192.168.1.2    255.255.255.252 UG    0    0

2)linux2的配置

[root@linux2 ~]# echo 1>  /proc/sys/net/ipv4/ip_forward

[root@linux2 ~]# route add -net172.16.1.0/24 gw 192.168.1.1

[root@linux2 ~]# route add -net10.0.0.0/24 gw 192.168.2.2

[root@linux2 ~]# route

Kernel IP routing table

Destination    Gateway        Genmask        Flags Metric Ref    Use Iface

10.0.0.0        192.168.2.2    255.255.255.0  UG  0      0        0 eth1

172.16.1.0      192.168.1.1    255.255.255.0  UG  0      0        0 eth0

192.168.1.0    0.0.0.0        255.255.255.0  U    100    0        0 eth0

192.168.2.0    0.0.0.0        255.255.255.252 U    100  0

3)linux3配置

[root@linux3 ~] echo 1>  /proc/sys/net/ipv4/ip_forward

[root@linux3 ~]$route add -net192.168.1.0/30 gw 192.168.2.1

[root@linux3 ~]$route add -net172.16.1.0/30 gw 192.168.2.1

[root@linux3 ~]$route

Kernel IP routing table

Destination    Gateway        Genmask        Flags Metric Ref    Use Iface

10.0.0.0        0.0.0.0        255.255.255.0  U    100    0        0 eth1

172.16.1.0      192.168.2.1    255.255.255.252 UG    0    0        0 eth0

192.168.1.0    192.168.2.1    255.255.255.252 UG    0    0        0 eth0

192.168.2.0    0.0.0.0        255.255.255.252 U    100  0        0 eth0

192.168.122.0  0.0.0.0        255.255.255.0  U    0      0

4)linux4的配置(linux4当做客户机时不用配置路由,将网关指向下一跳即可,此处为linux1的eth0接口地址)

[root@linux1 ~]$ echo 1>  /proc/sys/net/ipv4/ip_forward

[root@linux1 ~]$route add –net192.168.1.0/30 gw 172.16.1.1

[root@linux1 ~]$ route add –net192.168.2.0/30 gw 172.16.1.1

[root@linux1 ~]$ route add –net10.0.0.0/24 gw 172.16.1.1

[root@linux1 ~]$route

Kernel IP routing table

Destination    Gateway        Genmask        Flags Metric Ref    Use Iface

192.168.2.0    172.16.1.1    255.255.255.252 UG    0    0        0 eth0

192.168.1.0    172.16.1.1    255.255.255.252 UG    0      0      0 eth0

10.0.0.0        172.16.1.1    255.255.255.0  UG  0      0        0 eth0

172.16.1.0      *              255.255.255.0  U    1      0        0 eth0

default        172.16.1.1    0.0.0.0        UG  0      0        0 eth0

5)linux5的配置(Linux5当做网关使用时不用配置路由,将网关指向下一跳地址即可,此处为linux3的eth1接口地址即可)

[root@linux5 ~]$echo 1>  /proc/sys/net/ipv4/ip_forward

[root@linux5 ~]$ route add -net192.168.2.0/30 gw 10.0.0.254

[root@linux5 ~]$ route add -net192.168.1.0/30 gw 10.0.0.254

[root@linux5 ~]$ route add -net172.16.1.0/24 gw 10.0.0.254

[root@linux5 ~]$route

Kernel IP routing table

Destination    Gateway        Genmask        Flags Metric Ref    Use Iface

192.168.2.0    10.0.0.1      255.255.255.252 UG    0    0        0 eth1

192.168.1.0    10.0.0.1      255.255.255.252 UG    0    0        0 eth1

10.0.0.0        *              255.255.255.0  U    1      0        0 eth1

172.16.1.0      10.0.0.1      255.255.255.0  UG  0      0        0 eth1

172.16.0.0      *              255.255.0.0    U    1      0        0 eth0

default        server.magelinu 0.0.0.0        UG  0      0        0 eth0

(4)Linux主机配置动态路由

1)需要安装包quagga

[root@linux5 ~]$yum installquagga

2)配置:

[root@linux5 ~]$cd /etc/quagga/

[root@linux5 quagga]$cpospfd.conf.sample ospfd.conf  #修改配置文件,时配置文件生效

[root@linux5quagga]$/etc/init.d/ospfd start          #启动ospf路由协议

[root@linux5 ~]$vtysh        #进入配置动态路由

Hello, this is Quagga (version0.99.15).

Copyright 1996-2005 KunihiroIshiguro, et al.

linux5#

linux5#

linux5# enable

% Unknown command.

linux5# configure  terminal

linux5(config)#

4、网络管理命令ip

(1)使用IP命令管理网卡

1)关闭网卡:ip link setdev eth0 down

2)启用网卡:ip link setdev eth0 up

(2)使用ip命令配置网络

1)用法:ip addr{ add |del } IFADDR dev STRING

2)常用选项:

    [label LABEL]:添加地址时指明网卡别名

    [scope {global|link|host}]:指明作用域

      global: 全局可用

      link: 仅链接可用

      host: 本机可用

    [broadcast ADDRESS]:指明广播地址

……

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

    link/ether 00:0c:29:ed:0f:11 brdff:ff:ff:ff:ff:ff

    inet 172.16.250.102/16 brd 172.16.255.255scope global dynamic eth0

      valid_lft 85913sec preferred_lft85913sec

    inet6.6.6.6/24 scope global eth0:0

      valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:feed:f11/64 scope link

      valid_lft forever preferred_lft forever

3)修改网卡MTU(最大传输单元)

    命令:ip link set devIFACE mtu 1500

2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu1000 qdisc pfifo_fast state UP mode DEFAULT qlen 1000

    link/ether 00:0c:29:ed:0f:11 brdff:ff:ff:ff:ff:ff

    RX: bytes packets  errors  dropped overrun mcast 

    13836570  145845  0      0      0      0     

    RX errors: length  crc    frame  fifo    missed

              0        0      0      0      0     

    TX: bytes packets  errors  dropped carrier collsns

    218116    1694    0      0      0      0     

    TX errors: aborted  fifo  window heartbeat

              0        0      0      0

3)改变设备队列传输长度:ip link set dev IFACE txqueuelen

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

(3)使用ip命令配置路由

1)添加路由:ip route addTARGET via GW dev IFACE srcSOURCE_IP

2)删除路由:ip routedelete

3)查看路由:ip route(或ip route (show |list))

4)清空路由表:ip routeflush[dev IFACE] [via PREFIX]

default via 172.16.0.1 deveth0  proto static  metric 100

10.0.0.0/24 via 172.16.0.1 deveth0

172.16.0.0/16 dev eth0  proto kernel scope link  src172.16.250.102  metric 100

192.168.122.0/24 devvirbr0  proto kernel  scope link src 192.168.122.1

5、网络状态查看命令netstat命令使用

(1)用法:netstat[--tcp|-t] [--udp|-u] [--raw|-w][--listening|-l] [--all|-a] [--numeric|-n] [--extend|-e[--extend|-e]][--program|-p]

(2)参数:

1)-t: tcp协议相关

2)-u: udp协议相关

3)-w: raw socket相关

4)-l: 处于监听状态

5)-a: 所有状态

6)-n: 以数字显示IP和端口;

7)-e:扩展格式

8)-p: 显示相关进程及PID

(3)组合使用

Active Internet connections(servers and established)

Proto Recv-Q Send-Q LocalAddress          Foreign Address        State   

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   

Active Internet connections(only servers)

Proto Recv-Q Send-Q LocalAddress          Foreign Address        State   

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 

Active Internet connections(only servers)

Proto Recv-Q Send-Q LocalAddress  Foreign Address      State      PID/Program name 

tcp        0 0 192.168.122.1:53    0.0.0.0:*            LISTEN      1826/dnsmasq     

tcp        0 0 0.0.0.0:22          0.0.0.0:*            LISTEN      1143/sshd 

(4)显示路由表:netstat{--route|-r} [--numeric|-n]

Kernel IP routing table

Destination    Gateway        Genmask        Flags  MSS Window  irtt Iface

0.0.0.0        172.16.0.1      0.0.0.0        UG        0 0          0 eth0

172.16.0.0      0.0.0.0        255.255.0.0    U        0 0          0 eth0

192.168.122.0  0.0.0.0        255.255.255.0  U        0 0          0 virbr0

(5)显示接口统计数据:netstat{--interfaces|-I|-i}[iface] [--all|-a] [--extend|-e] [--program|-p] [--numeric|-n]

Kernel Interface table

Iface      MTU  RX-OK RX-ERR RX-DRP RX-OVR  TX-OK TX-ERR TX-DRP TX-OVR Flg

eth0      1000  158203      0      0 0          2050      0    0      0 BMRU

lo      65536      176    0      0 0          176      0    0      0 LRU

virbr0    1500      0      0      0 0            0      0    0      0 BMU

Kernel Interface table

Iface      MTU  RX-OK RX-ERR RX-DRP RX-OVR  TX-OK TX-ERR TX-DRP TX-OVR Flg

eth0      1000  158301      0      0 0          2055      0    0      0 BMRU

Iface      MTU  RX-OK RX-ERR RX-DRP RX-OVR  TX-OK TX-ERR TX-DRP TX-OVR Flg

eth0      1000  158505      0      0 0          2100      0    0      0 BMRU

6、网络状态查看命令ss命令使用(用法同netstat基本相同)

(1)作用:netstat通过遍历proc来获取socket信息,ss使用netlink与内核tcp_diag模块通信获取socket信息。

(2)用法:ss[OPTION]... [FILTER]

(3)常用选项:

(4)选项:

1)-t: tcp协议相关

2)-u: udp协议相关

3)-w: 裸套接字相关

4)-x:unixsock相关

5)-l: listen状态的连接

6)-a: 所有

7)-n: 数字格式

8)-p: 相关的程序及PID

9)-e: 扩展的信息

10)-m:内存用量

11)-o:计时器信息

(5)组合使用

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

LISTEN  0 5              192.168.122.1:53                        *:*               

LISTEN  0 128                *:22                                    *:*               

LISTEN  0 128                127.0.0.1:631                          *:*               

LISTEN 0 10                  127.0.0.1:25                              *:*

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

LISTEN    0 5            192.168.122.1:53                      *:*               

LISTEN  0 128              *:22                                *:*               

(6)常见用法

  1)ss -l 显示本地打开的所有端口

  2)ss -pl 显示每个进程具体打开的socket

  3)ss -t -a 显示所有tcp socket

  4)ss -u -a 显示所有的UDP Socekt

  5)ss -o stateestablished(tcp状态) '( dport = :ssh or sport = :ssh )' 显示所有已建立的ssh连接

  7)ss -o state established(tcp状态) '( dport = :http or sport = :http )' 显示所有已建立的HTTP连接

  8)ss -s 列出当前socket详细信息

#查看已经建立的ssh链接

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

tcp  0 52      172.16.250.102:ssh        172.16.252.112:49458                timer:(on,331ms,0)

#查看已经关闭的http链接

Netid Recv-Q Send-Q  LocalAddress:Port                      PeerAddress:Port

7、网络相关的配置文件

  通过命令配置的IP地址及通过命令配置的路由信息都是临时的,在系统重启后都会失效,如果想永久生效,需要将设备信息写入配置文件

(1)网卡配置文件(/etc/sysconfig/network-scripts/ifcfg-IFACE)

[root@linux5 ~]$cat/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0    #此配置文件应用到的设备

HWADDR=00:0c:29:d3:7b:9b    #mac地址

TYPE=Ethernet  #接口类型;常见有的Ethernet, Bridge

UUID=73ee782d-02ed-4a8e-acd2-e4f6aec30c5f    #设备的惟一标识

ONBOOT=yes  #在系统引导时是否激活此设备

#NM是NetworkManager的简写,此网卡是否接受NM控制;建议CentOS6为“no”

NM_CONTROLLED=yes

BOOTPROTO=dhcp  #激活此设备时使用的地址配置协议,常用的dhcp, static, none, bootp

USERCTL=no      #普通用户是否可控制此设备

#如果BOOTPROTO的值为“dhcp”,是否允许dhcpserver分配的dns服务器指向信息直接覆盖至/etc/resolv.conf文件中

PEERDNS=yes

IPADDR=10.0.0.10  #指明IP地址

NETMASK=255.255.255.0  #子网掩码

GATEWAY=10.0.0.254      #默认网关

centos6中网卡配置文件与NetworkManager的关系:

1)当NetworkManager服务关闭PEER1DNS=NO时不会生成dns配置文件(/etc/resolv.conf),需要手工创建配置

2)当NetworkManager服务关闭PEERDNS=yes时dns为手工指定的

3)NwtworkManager开启,PEER1DNS=no时,不会从dhcp获取dns

4)NwtworkManager开启,PEER1DNS=yes时, 会从dhcp服务器自动获取dns

(2)路由相关的配置文件(/etc/sysconfig/network-scripts/route-IFACE)

路由配置文件默认是不存在的,需要时需要自己手动创建并配置,配置完成后需要重启网卡才能生效。

1)配置方法一

    TARGET via GW

    如:10.0.0.0/8 via172.16.0.1

2)配置方法二:每三行定义一条路由

    ADDRESS#=TARGET

    NETMASK#=mask

    GATEWAY#=GW

[root@linux5 ~]$cat/etc/sysconfig/network-scripts/route-eth1 #路由配置文件

192.168.2.0/30 via 10.0.0.254

192.168.1.0/30 via 10.0.0.254

172.16.2.0/24 via 10.0.0.254

[root@linux5 ~]$route del -net192.168.2.0/30 gw 10.0.0.254  #删除之前的配置信息

[root@linux5 ~]$route del -net192.168.1.0/30 gw 10.0.0.254

[root@linux5 ~]$route del -net172.16.1.0/24 gw 10.0.0.254

[root@linux5~]$/etc/init.d/network restart    #重启网卡

[root@linux5 ~]$route                      #查看路由表

Kernel IP routing table

Destination    Gateway        Genmask        Flags Metric Ref    Use Iface

192.168.2.0    10.0.0.254      255.255.255.252 UG    0    0        0 eth1

192.168.1.0    10.0.0.254      255.255.255.252 UG    0    0        0 eth1

10.0.0.0        *              255.255.255.0  U    1      0        0 eth1

172.16.1.0      10.0.0.254      255.255.255.0  UG  0      0        0 eth1

172.16.0.0      *              255.255.0.0    U    1      0        0 eth0

default        server.magelinu 0.0.0.0        UG  0      0        0 eth0

(3)网卡别名配置文件

通过命令修改设置的网卡别名或增加的网卡IP地址都是临时生效的,想要永久生效,需要生成独立的配置文件,配置完成后重启网卡生效。

[root@linux5 ~]$cat/etc/sysconfig/network-scripts/ifcfg-eth0:0 #给网卡eth0添加ip地址

DEVICE=eth0:0        #应用到的设备

IPADDR=6.6.6.6      #IP地址

NETMASK=255.0.0.0  #子网掩码信息

ONPARENT=yes

猜你喜欢

转载自www.linuxidc.com/Linux/2017-05/143533.htm