linux高級網絡配置 ip別名,接口綁定


[root@py ~]# mii-tool eth0     #查看網卡狀態
eth0: negotiated 100baseTx-FD, link ok       #物理連接是否ok,寬帶.....
[root@py ~]# ethtool eth0
Settings for eth0:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: off (auto)
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000007 (7)
                   drv probe link
    Link detected: yes
[root@py ~]# ethtool -i eth0
driver: e1000
version: 7.3.21-k8-NAPI
firmware-version: 
bus-info: 0000:02:01.0
supports-statistics: yes
supports-test: yes
supports-eeprom-access: yes
supports-register-dump: yes
supports-priv-flags: no
[root@py ~]# ethtool -S eth0
NIC statistics:
     rx_packets: 463
     tx_packets: 107
     rx_bytes: 70460
     tx_bytes: 10258
     rx_broadcast: 0
     tx_broadcast: 0
     rx_multicast: 0
     tx_multicast: 0
     rx_errors: 0
     tx_errors: 0
     tx_dropped: 0
     multicast: 0
     collisions: 0
     rx_length_errors: 0
     rx_over_errors: 0
     rx_crc_errors: 0
     rx_frame_errors: 0
     rx_no_buffer_count: 0
     rx_missed_errors: 0
     tx_aborted_errors: 0
     tx_carrier_errors: 0
     tx_fifo_errors: 0
     tx_heartbeat_errors: 0
     tx_window_errors: 0
     tx_abort_late_coll: 0
     tx_deferred_ok: 0
     tx_single_coll_ok: 0
     tx_multi_coll_ok: 0
     tx_timeout_count: 0
     tx_restart_queue: 0
     rx_long_length_errors: 0
     rx_short_length_errors: 0
     rx_align_errors: 0
     tx_tcp_seg_good: 0
     tx_tcp_seg_failed: 0
     rx_flow_control_xon: 0
     rx_flow_control_xoff: 0
     tx_flow_control_xon: 0
     tx_flow_control_xoff: 0
     rx_long_byte_count: 70460
     rx_csum_offload_good: 193
     rx_csum_offload_errors: 0
     alloc_rx_buff_failed: 0
     tx_smbus: 0
     rx_smbus: 0
     dropped_smbus: 0

centos或rhel系統默認會啟用networkmanager對網卡管理,以方便用戶使用,但是如果使用子接口需要禁用networkmanager

[root@py ~]# service NetworkManager stop
Stopping NetworkManager daemon:                            [  OK  ]
[root@py ~]# chkconfig NetworkManager off

創建ip別名

[root@py ~]# ip addr add 192.168.1.200/24 dev eth0 label eth0:0
[root@py ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:73:C3:A9  
          inet addr:192.168.1.105  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe73:c3a9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:581 errors:0 dropped:0 overruns:0 frame:0
          TX packets:125 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:78780 (76.9 KiB)  TX bytes:11500 (11.2 KiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:73:C3:A9  
          inet addr:192.168.1.200  Bcast:0.0.0.0  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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:35 errors:0 dropped:0 overruns:0 frame:0
          TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1918 (1.8 KiB)  TX bytes:1918 (1.8 KiB)

永久添加ip別名

如果需要永久添加ip別名,則可以在/etc/sysconfig/network-scripts/下添加別名配置文件:

配置文件名:ifcfg-eth0

內容:

DEVICE

IPADDR

PREFIX

ONPARENT

[root@py ~]# cd /etc/sysconfig/network-scripts/
[root@py network-scripts]# ls
ifcfg-eth0   ifdown-ib    ifdown-ppp     ifup-aliases  ifup-ipv6   ifup-ppp       init.ipv6-global
ifcfg-lo     ifdown-ippp  ifdown-routes  ifup-bnep     ifup-isdn   ifup-routes    net.hotplug
ifdown       ifdown-ipv6  ifdown-sit     ifup-eth      ifup-plip   ifup-sit       network-functions
ifdown-bnep  ifdown-isdn  ifdown-tunnel  ifup-ib       ifup-plusb  ifup-tunnel    network-functions-ipv6
ifdown-eth   ifdown-post  ifup           ifup-ippp     ifup-post   ifup-wireless
[root@py network-scripts]# vim ifcfg-eth0:0
[root@py network-scripts]# ls
ifcfg-eth0    ifdown-eth   ifdown-post    ifup          ifup-ippp   ifup-post    ifup-wireless
ifcfg-eth0:0  ifdown-ib    ifdown-ppp     ifup-aliases  ifup-ipv6   ifup-ppp     init.ipv6-global
ifcfg-lo      ifdown-ippp  ifdown-routes  ifup-bnep     ifup-isdn   ifup-routes  net.hotplug
ifdown        ifdown-ipv6  ifdown-sit     ifup-eth      ifup-plip   ifup-sit     network-functions
ifdown-bnep   ifdown-isdn  ifdown-tunnel  ifup-ib       ifup-plusb  ifup-tunnel  network-functions-ipv6
[root@py network-scripts]# cat ifcfg-eth0:0
DEVICE=eth0:0
IPADDR=192.168.1.200
PREFIX=24
ONPARENT=yes

多網卡綁定

Linux支持多塊物理網卡綁定為一個邏輯網卡,綁定后的邏輯網卡可以並行使用組成其的所有物理網卡,通過這樣的方式用以提高帶寬以及穩定性,綁定后物理網卡不直接使用,ip地址配置在綁定后的邏輯網卡上。

linux支持以下網卡綁定模式:

--模式0、平衡輪訓

--模式1、主動備份

--模式3、廣播

網卡綁定配置

綁定后的邏輯網卡命名為bondn,n為編號,如/dev/bond0/、/dev/bond1

創建綁定網卡的配置文件:

/etc/sysconfig/network-scripts/ifcfg-bond0

DEVICE=bond0   #設備名稱
IPADDR=192.168.1.200  #地址
PREFIX=24 #前綴 掩碼24
ONBOOT=yes  #開機是否啟動
BOOTPROTO=none  #因為是手動配置ip地址,啟動協議匹配為空
USERCTL=no   #用戶控制
BONDING_OPTS="mode=1 miimon=50"  #模塊 驅動   引號內容 參數

我這塊兒卡主了,配置邏輯網卡的時候電腦沒電了,bond0設置完生效了,本來准備配置邏輯網卡,但是只有一塊網卡可以用,添加了一塊進來,沒起作用反復重啟配置終於搞定了,得重新搞了

通過setup可以添加網卡配置文件

DEVICE=eth0
BOOTPROTO=none
HWADDR=00:0c:29:73:c3:a9
ONBOOT=yes
MASTER=bond0  #從屬於bond0
SLAVE=yes
UUID="8375ffd0-fa14-4154-aaa2-1e39c117e9c1"
TYPE=Ethernet
USERCTL=no

第二塊也一樣

然后bond網卡添加驅動支持

alias bond0 bonding  #配置文件 告訴我們現在bond0這個設備所使用的的驅動是bonding
[root@py network-scripts]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:73:C3:A9  
          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe73:c3a9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:360 (360.0 b)  TX bytes:1656 (1.6 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:73:C3:B3  
          inet addr:192.168.1.112  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe73:c3b3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:60 (60.0 b)  TX bytes:758 (758.0 b)

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:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:480 (480.0 b)  TX bytes:480 (480.0 b)

[root@py network-scripts]# vim ifcfg-bond0
[root@py network-scripts]# ls
ifcfg-bond0  ifdown-ippp    ifup          ifup-plip      init.ipv6-global
ifcfg-eth0   ifdown-ipv6    ifup-aliases  ifup-plusb     net.hotplug
ifcfg-eth1   ifdown-isdn    ifup-bnep     ifup-post      network-functions
ifcfg-lo     ifdown-post    ifup-eth      ifup-ppp       network-functions-ipv6
ifdown       ifdown-ppp     ifup-ib       ifup-routes
ifdown-bnep  ifdown-routes  ifup-ippp     ifup-sit
ifdown-eth   ifdown-sit     ifup-ipv6     ifup-tunnel
ifdown-ib    ifdown-tunnel  ifup-isdn     ifup-wireless
[root@py network-scripts]# vim ifcfg-eth0
[root@py network-scripts]# vim ifcfg-eth1
[root@py network-scripts]# cd /etc/modprobe.d/
[root@py modprobe.d]# ls
anaconda.conf   dist-alsa.conf  libcxgb3.conf  mlx4.conf      vmware-tools.conf
blacklist.conf  dist.conf       libcxgb4.conf  network.conf
bonding.conf    dist-oss.conf   libmlx4.conf   openfwwf.conf
[root@py modprobe.d]# vim bonding.conf 
[root@py modprobe.d]# ifup bond0
./network-functions: line 522: /sys/class/net/bond0/bonding/miimom: No such file or directory
./network-functions: line 522: /sys/class/net/bond0/bonding/miimom: No such file or directory
./network-functions: line 522: /sys/class/net/bond0/bonding/miimom: No such file or directory
Determining if ip address 192.168.1.200 is already in use for device bond0...
RTNETLINK answers: File exists
Error adding address 192.168.1.200 for bond0.
RTNETLINK answers: File exists
[root@py modprobe.d]# ifdown eth0
[root@py modprobe.d]# ifdown eth1
[root@py modprobe.d]# service network restart
Shutting down interface bond0:                             [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface bond0:  ./network-functions: line 522: /sys/class/net/bond0/bonding/miimom: No such file or directory
./network-functions: line 522: /sys/class/net/bond0/bonding/miimom: No such file or directory
Determining if ip address 192.168.1.200 is already in use for device bond0...
                                                           [  OK  ]
[root@py modprobe.d]# ifconfig 
bond0     Link encap:Ethernet  HWaddr 00:0C:29:73:C3:A9  
          inet addr:192.168.1.200  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe73:c3a9/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:18 errors:0 dropped:0 overruns:0 frame:0
          TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1080 (1.0 KiB)  TX bytes:2162 (2.1 KiB)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:73:C3:A9    #現在我們可以看到這塊網卡已經屬於bond0了
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:54 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:420 (420.0 b)  TX bytes:3672 (3.5 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:73:C3:A9   #同樣屬於bond0
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:19 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1140 (1.1 KiB)  TX bytes:974 (974.0 b)

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:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:480 (480.0 b)  TX bytes:480 (480.0 b)

從外部ping一下看看效果,這塊兒我在虛擬機上做的,現在遇到問題從外面ping不通,目前bond0網卡的配置是這樣的

[root@py modprobe.d]# ethtool bond0
Settings for bond0:
    Supported ports: [ ]
    Supported link modes:   Not reported
    Supported pause frame use: No
    Supports auto-negotiation: No
    Advertised link modes:  Not reported
    Advertised pause frame use: No
    Advertised auto-negotiation: No
    Speed: 1000Mb/s
    Duplex: Full
    Port: Other
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: off
    Link detected: yes
[root@py modprobe.d]# ethtool -S bond0
no stats available
[root@py modprobe.d]# ethtool -i bond0
driver: bonding
version: 3.7.1
firmware-version: 2
bus-info: 
supports-statistics: no
supports-test: no
supports-eeprom-access: no
supports-register-dump: no
supports-priv-flags: no

 虛擬機兩個網卡設置橋接就通了,我去,汗....................................


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM