天天看點

Linux網絡管理你懂多少

一、基本網絡配置 

1、配置網絡

2、實作網絡組 

3、測試網絡 

4、網絡工具

二、網絡配置

1)靜态指定

ifcfg:ifconfig,route,netstat  ip:object {link,addr,route},ss,setup 

centos7新增實用工具:nmcli nmtui   

2)動态配置設定  

DHCP: Dynamic Host Configuration Protocol

ifconfig指令: 

    ifconfig [interface]

        ifconfig -a     #顯示所有網卡資訊

        ifconfig {eth0|eth1} [up|down]    #關閉或開啟某網卡

    ifconfig interface [aftype] options | address ...

        ifconfig IFACE IP/mask [up]  #臨時添加一個ip位址 如:ifconfig eth0:0 10.1.1.1/16

        ifconfig IFACE IP netmask MASK  #臨時添加一個ip位址 如:ifconfig eth0:0 10.1.1.1 255.255.0.0

注意:此指令立即生效   

代碼示範:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<code>root@centos6 ~]</code><code># ifconfig eth1</code>

<code>eth1      Link encap:Ethernet  HWaddr 00:0C:29:7C:55:97</code>

<code>          </code><code>inet addr:192.168.226.133  Bcast:192.168.226.255  Mask:255.255.255.0</code>

<code>          </code><code>BROADCAST MULTICAST  MTU:1500  Metric:1</code>

<code>          </code><code>RX packets:2922 errors:0 dropped:0 overruns:0 frame:0</code>

<code>          </code><code>TX packets:726 errors:0 dropped:0 overruns:0 carrier:0</code>

<code>          </code><code>collisions:0 txqueuelen:1000</code>

<code>          </code><code>RX bytes:201495 (196.7 KiB)  TX bytes:71868 (70.1 KiB)</code>

<code>[root@centos6 ~]</code><code># ifconfig eth1 down</code>

<code>[root@centos6 ~]</code><code># ifconfig eth1 up</code>

<code>[root@centos6 ~]</code><code># ifconfig eth0:1</code>

<code>eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:7C:55:8D</code>

<code>          </code><code>inet addr:10.1.1.2  Bcast:10.1.255.255  Mask:255.255.0.0</code>

<code>          </code><code>UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1</code>

<code>[root@centos6 ~]</code><code>#</code>

三、網路配置常用指令

route指令:

檢視路由:route -n  

添加路由:route add  

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

示例:route add -net 192.168.1.10/24 gw 10.1.0.1  dev eth0   

      route add -net 192.168.0.2 netmask 255.255.255.0 gw 172.16.0.1 dev eth0 

      route add -net 192.168.0.2/24 gw 172.16.0.1 dev eth0

預設路由:route add -net 0.0.0.2 netmask 0.0.0.0 gw 172.16.0.1   

          route add default gw 172.16.0.1   

删除路由:route del   

          route del 192.168.1.3

          route del -net 192.168.0.1 netmask 255.255.255.0

示範:

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<code>[root@centos7 ~]</code><code># route -n</code>

<code>Kernel IP routing table</code>

<code>Destination     Gateway         Genmask         Flags Metric Ref    Use Iface</code>

<code>0.0.0.0         10.1.249.1      0.0.0.0         UG    100    0        0 eth0</code>

<code>0.0.0.0         192.168.226.2   0.0.0.0         UG    101    0        0 eth1</code>

<code>10.1.0.0        0.0.0.0         255.255.0.0     U     100    0        0 eth0</code>

<code>192.168.226.0   0.0.0.0         255.255.255.0   U     100    0        0 eth1</code>

<code>[root@centos7 ~]</code><code># route add -net 192.168.12.0/24 gw 10.1.249.1</code>

<code>192.168.12.0    10.1.249.1      255.255.255.0   UG    0      0        0 eth0</code>

<code>[root@centos7 ~]</code><code># route add -host 10.1.250.1 gw 10.1.249.1</code>

<code>[root@centos7 ~]</code><code># route add -host 10.1.24.23 gw 10.1.0.1</code>

<code>10.1.24.23      10.1.0.1        255.255.255.255 UGH   0      0        0 eth0</code>

<code>10.1.250.1      10.1.249.1      255.255.255.255 UGH   0      0        0 eth0</code>

<code>[root@centos7 ~]</code><code># route del -net  192.168.12.0 netmask 255.255.255.0</code>

<code>[root@centos7 ~]</code><code># route del -host 10.1.24.23</code>

<code>[root@centos7 ~]</code><code># route del -host 10.1.250.1</code>

<code>[root@centos7 ~]</code><code>#</code>

netstat指令:

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

    選項:

        -t:tcp協定相關

        -u:udp協定相關

        -w:raw socket 

        -l:處于監聽狀态listen  

        -a:所有狀态 

        -n:以數字顯示ip和端口 

        -e:擴充格式

        -p:顯示相關程序及其pid  

常用組合: -tan, -uan, -tnl, -unl     

ss指令:

 ss [OPTION]... [FILTER]

netstat通過周遊proc來擷取socket資訊,ss使用netlink與核心tcp_diag 子產品通信擷取socket資訊,效率比netstat高。 

    選項: 

        -t:tcp相關協定

        -x:unix sock相關

        -l:listen監聽狀态

        -a:所有

        -n:數字格式  

        -p:相關的程式及pid

        -e:擴充資訊 

        -m:記憶體資訊

ip指令: 

配置linux網絡屬性:ip - show / manipulate routing, devices, policy routing and tunnels    

ip link - network device configuration 

    set dev IFACE  如:ip link set dev eth0 up/down 

ip addr { add | del } IFADDR dev STRING [label LABEL]

ip address flush  #清除ip位址 ip address flush dev eth0 

ip addr add 192.168.100.1/24 dev eth0 label eth1:1 #添加一條臨時ip位址

ip addr del 172.16.100.13/16 dev eth0 label eth0:0 #删除臨時添加的ip位址

ip addr flush dev eth0 label eth0:0  #删除eth0這條ip記錄

ip route - routing table management 

添加路由:ip route add

  ip route add TARGET via GW dev IFACE src SOURCE_IP

  ip route add 192.168.0.0/24 via 172.16.0.1

  ip route add 192.168.1.2 via 172.16.0.1  

删除路由:ip route delete  target  

顯示路由:ip route show|list    

清空路由:ip route flush # ip route flush dev eth0   

四、網絡配置檔案

1)IP、MASK、GW、DNS相關配置檔案: /etc/sysconfig/network-scripts/ifcfg-IFACE 

2)路由相關的配置檔案: /etc/sysconfig/network-scripts/route-IFACE 

/etc/sysconfig/network-scripts/ifcfg-IFACE: 配置檔案說明如下  

DEVICE:此配置檔案應用到的裝置  

BOOTPROTO:{staic|dhcp|none}激活此裝置時使用的位址配置協定

ONBOOT:在系統引導時是否激活此裝置 

TYPE:接口類型

UUID:裝置的惟一辨別

HWADDR:mac位址 

IPADDR:指明IP位址

NETMASK:子網路遮罩

GATEWAY: 預設網關

DNS1:第一個DNS伺服器指向

DNS2:第二個DNS伺服器指向 

PEERDNS:如果BOOTPROTO的值為“dhcp”,是否允許 dhcp server配置設定的dns伺服器指向資訊直接覆寫至 /etc/resolv.conf檔案中

3)本地解析器

解析器執行正向和逆向查詢 :/etc/hosts 本地主機名資料庫和IP位址的映像 對小型獨立網絡有用通常,在使用DNS前檢查

getent hosts  #檢視/etc/hosts 内容

4)dns域名解析:/etc/resolv.conf 

   nameserver dns_server_ip1 

   nameserver dns_server_ip2

   nameserver dns_server_ip3

配置本地解析及域名解析優先級:/etc/nsswitch.conf

<a href="http://s5.51cto.com/wyfs02/M02/86/EB/wKioL1fOvJny0zwIAABImrjqj1Q896.png" target="_blank"></a>

5)網卡别名

ifconfig指令: ifconfig eth0:0 192.168.1.100/24 up 

ip指令:

ip addr add 192.168.1.2/24 dev eth0

ip addr add 192.168.1.3/24 dev eth0 label eth0:0  

ip addr del 192.168.1.3/24 dev eth0 label eth0:0  

ip addr flush dev eth0 label eth0:0   

6)配置主機名  

Linux網絡屬性配置的tui(text user interface): system-config-network-tui setup 注意:記得重新開機網絡服務方能生效 

配置目前主機的主機名: hostname [HOSTNAME] /etc/sysconfig/network HOSTNAME=

7)網卡名稱

網絡接口識别并命名相關的udev配置檔案   

/etc/udev/rules.d/70-persistent-net.rules 解除安裝網卡驅動: modprobe -r e1000 裝載網卡驅動: modprobe e1000 (centos6.x)

<code># PCI device 0x8086:0x100f (e1000)</code>

<code>SUBSYSTEM==</code><code>"net"</code><code>, ACTION==</code><code>"add"</code><code>, DRIVERS==</code><code>"?*"</code><code>, ATTR{address}==</code><code>"00:0c:29:7c:55:97"</code><code>, ATTR{</code><code>type</code><code>}==</code><code>"1"</code><code>, KERNEL==</code><code>"eth*"</code><code>, NAME=</code><code>"eth1"</code>

<code># PCI device 0x8086:0x100f (e1000) (custom name provided by external tool)</code>

<code>SUBSYSTEM==</code><code>"net"</code><code>, ACTION==</code><code>"add"</code><code>, DRIVERS==</code><code>"?*"</code><code>, ATTR{address}==</code><code>"00:0c:29:7c:55:8d"</code><code>, ATTR{</code><code>type</code><code>}==</code><code>"1"</code><code>, KERNEL==</code><code>"eth*"</code><code>, NAME=</code><code>"eth0"</code>

使用傳統命名方式: 在centos7.x中需使用eth0,1,2...表示可修改/etc/boot/grub2/grub.cfg配置檔案  

<a href="http://s1.51cto.com/wyfs02/M01/86/EC/wKioL1fOwJGyx_kwAABklo80IUg270.png" target="_blank"></a>

五、網絡客服端工具  

1)檔案傳輸工具及其下載下傳工具

lftp, ftp, lftpget, wget 

    lftp [-p port] [-u user[,password]] SERVER

子指令: get、 mget、ls、help

2)lftpget URL # lftp 192.168.1.1  

3)wget [option]... [URL]... 

-q: 靜默模式 

-c: 斷點續傳 

-O: 儲存位置 

--limit-rates=: 指定傳輸速率

如:wget ftp://10.1.0.1/pub/alren/xiaoerduowget 

         http://www.xiaoerduo/pub/alren/xiaoerduo

本文轉自chengong1013 51CTO部落格,原文連結:http://blog.51cto.com/purify/1847002,如需轉載請自行聯系原作者

繼續閱讀