Category: Linux & Open Source

Linux administration on Ubuntu and CentOS: commands, scripting, networking, services (DNS, DHCP, Samba, NFS, Apache, Squid) and LPIC study notes.

  • Create Network Bonding On CentOS 7/6.5

    What is Network bonding?

    Network bonding is a method of combining (joining) two or more network interfaces together into a singleinterface. It will increase the network throughput, bandwidth and will give redundancy. If one interface is down or unplugged, the other one will keep the network traffic up and alive. Network bonding can be used in situations wherever you need redundancy, fault tolerance or load balancing networks.

    Linux allows us to bond multiple network interfaces into single interface using a special kernel module namedbonding. The Linux bonding driver provides a method for combining multiple network interfaces into a single logical “bonded” interface. The behaviour of the bonded interfaces depends upon the mode; generally speaking, modes provide either hot standby or load balancing services. Additionally, link integrity monitoring, may be performed.

    Types of Network Bonding

    According the to the official documentation, here is the types of network bonding modes.

    mode=0 (balance-rr)

    Round-robin policy: It the default mode. It transmits packets in sequential order from the first available slave through the last. This mode provides load balancing and fault tolerance.

    mode=1 (active-backup)

    Active-backup policy: In this mode, only one slave in the bond is active. The other one will become active, only when the active slave fails. The bond’s MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This mode provides fault tolerance.

    mode=2 (balance-xor)

    XOR policy: Transmit based on [(source MAC address XOR’d with destination MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load balancing and fault tolerance.

    mode=3 (broadcast)

    Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

    mode=4 (802.3ad)

    IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.

    Prerequisites:

    – Ethtool support in the base drivers for retrieving the speed and duplex of each slave.
    – A switch that supports IEEE 802.3ad Dynamic link aggregation. Most switches will require some type of configuration to enable 802.3ad mode.

    mode=5 (balance-tlb)

    Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.

    Prerequisite:

    – Ethtool support in the base drivers for retrieving the speed of each slave.

    mode=6 (balance-alb)

    Adaptive load balancing: includes balance-tlb plus receive load balancing (rlb) for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the local system on their way out and overwrites the sourcehardware address with the unique hardware address of one of the slaves in the bond such that different peers use different hardware addresses for the server.

    In this handy tutorial let us see how to setup network bonding on CentOS 7 and CentOS 6.5. Though it was tested on CentOS, it should work on RHEL and Scientific Linux 6.x versions.

    First, we will setup network bonding on CentOS 7.

    1. Setting up Network Bonding on CentOS 7

    I have three network interfaces in my CentOS 7 system, namely:

    1. enp0s3;
    2. enp0s8;
    3. enp0s9.

    Let us combine two NICs (enp0s8, and enp0s9) and make them into one NIC named bond0.

    Configure Bond0 Interface

    In CentOS 7, the bonding module is not loaded by default. Enter the following command as root user to enable it.

    modprobe --first-time bonding

    You can view the bonding module information using command:

    modinfo bonding

    Sample output:

    filename:       /lib/modules/3.10.0-123.el7.x86_64/kernel/drivers/net/bonding/bonding.ko
    alias:          rtnl-link-bond
    author:         Thomas Davis, tadavis@lbl.gov and many others
    description:    Ethernet Channel Bonding Driver, v3.7.1
    version:        3.7.1
    license:        GPL
    srcversion:     E52AE00A79EA6FEFB5BF718
    depends:        
    intree:         Y
    vermagic:       3.10.0-123.el7.x86_64 SMP mod_unload modversions 
    signer:         CentOS Linux kernel signing key
    sig_key:        BC:83:D0:FE:70:C6:2F:AB:1C:58:B4:EB:AA:95:E3:93:61:28:FC:F4
    sig_hashalgo:   sha256
    parm:           max_bonds:Max number of bonded devices (int)
    parm:           tx_queues:Max number of transmit queues (default = 16) (int)
    parm:           num_grat_arp:Number of peer notifications to send on failover event (alias of num_unsol_na) (int)
    parm:           num_unsol_na:Number of peer notifications to send on failover event (alias of num_grat_arp) (int)
    parm:           miimon:Link check interval in milliseconds (int)
    parm:           updelay:Delay before considering link up, in milliseconds (int)
    parm:           downdelay:Delay before considering link down, in milliseconds (int)
    parm:           use_carrier:Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default) (int)
    parm:           mode:Mode of operation; 0 for balance-rr, 1 for active-backup, 2 for balance-xor, 3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, 6 for balance-alb (charp)
    parm:           primary:Primary network device to use (charp)
    parm:           primary_reselect:Reselect primary slave once it comes up; 0 for always (default), 1 for only if speed of primary is better, 2 for only on active slave failure (charp)
    parm:           lacp_rate:LACPDU tx rate to request from 802.3ad partner; 0 for slow, 1 for fast (charp)
    parm:           ad_select:803.ad aggregation selection logic; 0 for stable (default), 1 for bandwidth, 2 for count (charp)
    parm:           min_links:Minimum number of available links before turning on carrier (int)
    parm:           xmit_hash_policy:balance-xor and 802.3ad hashing method; 0 for layer 2 (default), 1 for layer 3+4, 2 for layer 2+3 (charp)
    parm:           arp_interval:arp interval in milliseconds (int)
    parm:           arp_ip_target:arp targets in n.n.n.n form (array of charp)
    parm:           arp_validate:validate src/dst of ARP probes; 0 for none (default), 1 for active, 2 for backup, 3 for all (charp)
    parm:           fail_over_mac:For active-backup, do not set all slaves to the same MAC; 0 for none (default), 1 for active, 2 for follow (charp)
    parm:           all_slaves_active:Keep all frames received on an interfaceby setting active flag for all slaves; 0 for never (default), 1 for always. (int)
    parm:           resend_igmp:Number of IGMP membership reports to send on link failure (int)

    Let us create a bond0 configuration file as shown below.

    Go to the directory where CentOS stores the network configuration files. By default RHEL and its clones such as CentOS, Scientific Linux stores the network configuration files under /etc/sysconfig/network-scripts/directory.

    Log in as root user.

    Create bond0 configuration file:

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

    Add the following lines.

    DEVICE=bond0
    NAME=bond0
    TYPE=Bond
    BONDING_MASTER=yes
    IPADDR=192.168.1.150
    PREFIX=24
    ONBOOT=yes
    BOOTPROTO=none
    BONDING_OPTS="mode=1 miimon=100"

    Note: Here, BONDING_OPTS describes the bonding mode. In our case, we will be configuring mode1(active-backup). Save and close file. 192.168.1.150 is bond0 IP address.

    Configure Network interfaces

    Now, we should modify both(enp0s8 & enp0s9) configuration files as shown below. First, let us start fromenp0s8.

    Edit file /etc/sysconfig/network-scripts/ifcfg-enp0s8,

    vi /etc/sysconfig/network-scripts/ifcfg-enp0s8

    Modify the file as shown below.

    HWADDR="08:00:27:04:03:86"
    TYPE="Ethernet"
    BOOTPROTO="none"
    DEFROUTE="yes"
    PEERDNS="yes"
    PEERROUTES="yes"
    IPV4_FAILURE_FATAL="no"
    IPV6INIT="yes"
    IPV6_AUTOCONF="yes"
    IPV6_DEFROUTE="yes"
    IPV6_PEERDNS="yes"
    IPV6_PEERROUTES="yes"
    IPV6_FAILURE_FATAL="no"
    NAME="enp0s8"
    UUID="a97b23f2-fa87-49de-ac9b-39661ba9c20f"
    ONBOOT="yes"
    MASTER=bond0
    SLAVE=yes

    Then, Edit file /etc/sysconfig/network-scripts/ifcfg-enp0s9,

    vi /etc/sysconfig/network-scripts/ifcfg-enp0s9

    Modify the file as shown below.

    HWADDR=08:00:27:E7:ED:8E
    TYPE=Ethernet
    BOOTPROTO=none
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=yes
    IPV6_AUTOCONF=yes
    IPV6_DEFROUTE=yes
    IPV6_PEERDNS=yes
    IPV6_PEERROUTES=yes
    IPV6_FAILURE_FATAL=no
    NAME=enp0s9
    UUID=e2352c46-e1f9-41d2-98f5-af24b127b3e7
    ONBOOT=yes
    MASTER=bond0
    SLAVE=yes

    Save and close the files.

    Now, activate the Network interfaces.

    ifup ifcfg-enp0s8
    ifup ifcfg-enp0s9

    Now, enter the following command to make Network Manager aware the changes.

    nmcli con reload

    Restart network service to take effect the changes.

    systemctl restart network

    Test Network Bonding

    Now enter the following command to check whether the bonding interface bond0 is up and running:

    cat /proc/net/bonding/bond0

    Sample output:

    Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
    
    Bonding Mode: fault-tolerance (active-backup)
    Primary Slave: None
    Currently Active Slave: enp0s8
    MII Status: up
    MII Polling Interval (ms): 100
    Up Delay (ms): 0
    Down Delay (ms): 0
    
    Slave Interface: enp0s8
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 0
    Permanent HW addr: 08:00:27:5d:ad:75
    Slave queue ID: 0
    
    Slave Interface: enp0s9
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 0
    Permanent HW addr: 08:00:27:48:93:cd
    Slave queue ID: 0

    As you see in the above output, the bond0 interface is up and running and it is configured as active-backup(mode1) mode. In this mode, only one slave in the bond is active. The other one will become active, only when the active slave fails.

    To view the list of network interfaces and their IP address, enter the following command:

    ip addr

    Sample output:

    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
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 08:00:27:1f:3b:20 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic enp0s3
           valid_lft 86130sec preferred_lft 86130sec
        inet6 fe80::a00:27ff:fe1f:3b20/64 scope link 
           valid_lft forever preferred_lft forever
    3: enp0s8: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
        link/ether 08:00:27:5d:ad:75 brd ff:ff:ff:ff:ff:ff
    4: enp0s9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
        link/ether 08:00:27:5d:ad:75 brd ff:ff:ff:ff:ff:ff
    5: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
        link/ether 08:00:27:5d:ad:75 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.150/24 brd 192.168.1.255 scope global bond0
           valid_lft forever preferred_lft forever
        inet6 fe80::a00:27ff:fe5d:ad75/64 scope link 
           valid_lft forever preferred_lft forever

    That’s it.

    Configure multiple IP addresses for bond0

    I want to assign multiple IP addresses to bond0 interface. What should i do? Very simple, just create an alias for the bond0 interface and assign multiple IP addresses.

    Let me make it more clear. Say for example we want to assign IP address 192.168.1.151 to bond0. To create an alias for bond0, copy the existing configuration file(ifcfg-bond0) to a new configuration file(ifcfg-bond0:1).

    cp /etc/sysconfig/network-scripts/ifcfg-bond0 /etc/sysconfig/network-scripts/ifcfg-bond0:1

    Then edit the alias file /etc/sysconfig/network-scripts/ifcfg-bond0:1,

    vi /etc/sysconfig/network-scripts/ifcfg-bond0:1

    Modify the device name and IP address as shown below.

    DEVICE=bond0:1
    NAME=bond0
    TYPE=Bond
    BONDING_MASTER=yes
    IPADDR=192.168.1.151
    PREFIX=24
    ONBOOT=yes
    BOOTPROTO=none
    BONDING_OPTS="mode=1 miimon=100"

    Here,

    • bond0:1 – Device name
    • 192.168.1.151 – IP address of bond0:1

    Save and close the file. Restart network service to take effect the saved changes.

    systemctl restart network

    Now list out the network interfaces and their IP address using the command:

    ip addr

    Sample output:

    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
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host 
           valid_lft forever preferred_lft forever
    2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 08:00:27:1f:3b:20 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic enp0s3
           valid_lft 86388sec preferred_lft 86388sec
        inet6 fe80::a00:27ff:fe1f:3b20/64 scope link 
           valid_lft forever preferred_lft forever
    3: enp0s8: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
        link/ether 08:00:27:5d:ad:75 brd ff:ff:ff:ff:ff:ff
    4: enp0s9: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000
        link/ether 08:00:27:5d:ad:75 brd ff:ff:ff:ff:ff:ff
    5: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
        link/ether 08:00:27:5d:ad:75 brd ff:ff:ff:ff:ff:ff
        inet 192.168.1.150/24 brd 192.168.1.255 scope global bond0
           valid_lft forever preferred_lft forever
        inet 192.168.1.151/24 brd 192.168.1.255 scope global secondary bond0:1
           valid_lft forever preferred_lft forever
        inet6 fe80::a00:27ff:fe5d:ad75/64 scope link 
           valid_lft forever preferred_lft forever

    As you above the alias bond0:1 has been created and it’s up now.

    2. Setting up Network Bonding on CentOS 6.5

    We have seen Network bonding on CentOS 7. Now, we will see how to do it on CentOS 6.x.

    I have three network interfaces, namely eth0, eth1 and eth2 in my CentOS 6.5 system. Let us combine two NICs (eth1 and eth2) and make them into one NIC named bond0.

    Configure Bond0 Interface

    First, let us create a bond0 configuration file as shown below.

    Go to the directory where CentOS stores the network configuration files. By default RHEL and its clones such as CentOS, Scientific Linux stores the network configuration files under /etc/sysconfig/network-scripts/directory.

    Run the following commands as root user.

    Create bond0 configuration file under the above mentioned directory.

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

    Add the following lines.

    DEVICE=bond0
    BOOTPROTO=none
    ONBOOT=yes
    IPADDR=192.168.1.200
    NETWORK=192.168.1.0
    NETMASK=255.255.255.0
    USERCTL=no
    BONDING_OPTS="mode=1 miimon=100"

    Note: Here BONDING_OPTS describes the bonding mode. In our case, we will be configuring mode1(active-backup). Save and close file. 192.168.1.200 is bond0 IP address.

    Next we have to load up the bond0 interface into the kernel. To do that, create a new file/etc/modprobe.d/bonding.conf,

    vi /etc/modprobe.d/bonding.conf

    Add the following line in it.

    alias bond0 bonding

    Save and close the file.

    Configure Network interfaces

    Now we should modify both(eth1 & eth2) configuration files as shown below. First, let us start from eth1.

    Edit file /etc/sysconfig/network-scripts/ifcfg-eth1,

    vi /etc/sysconfig/network-scripts/ifcfg-eth1

    Modify the file as shown below.

    DEVICE=eth1
    MASTER=bond0
    SLAVE=yes
    USERCTL=no
    ONBOOT=yes
    BOOTPROTO=none

    Then Edit file /etc/sysconfig/network-scripts/ifcfg-eth2,

    # vi /etc/sysconfig/network-scripts/ifcfg-eth2

    Modify the file as shown below.

    DEVICE=eth2
    MASTER=bond0
    SLAVE=yes
    USERCTL=no
    ONBOOT=yes
    BOOTPROTO=none

    Save and close the files.

    Enter the following command to load the bonding module.

    modprobe bonding

    Restart network service to take effect the changes.

    service network restart

    Test Network Bonding

    Now enter the following command to check whether the bonding interface bond0 is up and running:

    cat /proc/net/bonding/bond0

    Sample output:

    Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)
    
    Bonding Mode: fault-tolerance (active-backup)
    Primary Slave: None
    Currently Active Slave: eth1
    MII Status: up
    MII Polling Interval (ms): 100
    Up Delay (ms): 0
    Down Delay (ms): 0
    
    Slave Interface: eth1
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 0
    Permanent HW addr: 08:00:27:fe:6f:bf
    Slave queue ID: 0
    
    Slave Interface: eth2
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 0
    Permanent HW addr: 08:00:27:34:17:c0
    Slave queue ID: 0

    As you see in the above output, the bond0 interface is up and running and it is configured as active-backup(mode1) mode. In this mode, only one slave in the bond is active. The other one will become active, only when the active slave fails.

    To view the list of network interfaces and their IP address, enter the following command:

    # ifconfig

    Sample output:

    bond0     Link encap:Ethernet  HWaddr 08:00:27:FE:6F:BF  
              inet addr:192.168.1.200  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fefe:6fbf/64 Scope:Link
              UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
              RX packets:379 errors:0 dropped:0 overruns:0 frame:0
              TX packets:167 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:32354 (31.5 KiB)  TX bytes:24078 (23.5 KiB)
    
    eth0      Link encap:Ethernet  HWaddr 08:00:27:BE:25:49  
              inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:febe:2549/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1402 errors:0 dropped:0 overruns:0 frame:0
              TX packets:904 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:134823 (131.6 KiB)  TX bytes:124938 (122.0 KiB)
    
    eth1      Link encap:Ethernet  HWaddr 08:00:27:FE:6F:BF  
              UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
              RX packets:285 errors:0 dropped:0 overruns:0 frame:0
              TX packets:156 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:24746 (24.1 KiB)  TX bytes:22956 (22.4 KiB)
    
    eth2      Link encap:Ethernet  HWaddr 08:00:27:FE:6F:BF  
              UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
              RX packets:95 errors:0 dropped:0 overruns:0 frame:0
              TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:7674 (7.4 KiB)  TX bytes:1364 (1.3 KiB)
    
    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:16436  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

    As per the above output, bond0 is configured as master; eth1 and eth2 are configured as a slave.

    Configure multiple IP addresses for bond0

    I want to assign multiple IP addresses to bond0 interface. What should i do? Very simple, just create an alias for the bond0 interface and assign multiple IP addresses.

    Let me make it more clear. Say for example we want to assign IP address 192.168.1.201 to bond0. To create an alias for bond0, copy the existing configuration file(ifcfg-bond0) to a new configuration file(ifcfg-bond0:1).

    cp /etc/sysconfig/network-scripts/ifcfg-bond0 /etc/sysconfig/network-scripts/ifcfg-bond0:1

    Then edit the alias file /etc/sysconfig/network-scripts/ifcfg-bond0:1,

    vi /etc/sysconfig/network-scripts/ifcfg-bond0:1

    Modify the device name and IP address as shown below.

    DEVICE=bond0:1
    BOOTPROTO=none
    ONBOOT=yes
    IPADDR=192.168.1.201
    NETWORK=192.168.1.0
    NETMASK=255.255.255.0
    USERCTL=no
    BONDING_OPTS="mode=1 miimon=100"

    Save and close the file. Restart network service to take effect the saved changes.

    service network restart

    Now list out the network interfaces and their IP address using the command:

    ifconfig

    Sample output:

    bond0     Link encap:Ethernet  HWaddr 08:00:27:FE:6F:BF  
              inet addr:192.168.1.200  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fefe:6fbf/64 Scope:Link
              UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
              RX packets:1048 errors:0 dropped:0 overruns:0 frame:0
              TX packets:590 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:88622 (86.5 KiB)  TX bytes:84340 (82.3 KiB)
    
    bond0:1   Link encap:Ethernet  HWaddr 08:00:27:FE:6F:BF  
              inet addr:192.168.1.201  Bcast:192.168.1.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
    
    eth0      Link encap:Ethernet  HWaddr 08:00:27:BE:25:49  
              inet addr:192.168.1.101  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:febe:2549/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1422 errors:0 dropped:0 overruns:0 frame:0
              TX packets:916 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:136317 (133.1 KiB)  TX bytes:126150 (123.1 KiB)
    
    eth1      Link encap:Ethernet  HWaddr 08:00:27:FE:6F:BF  
              UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
              RX packets:942 errors:0 dropped:0 overruns:0 frame:0
              TX packets:581 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:80036 (78.1 KiB)  TX bytes:84266 (82.2 KiB)
    
    eth2      Link encap:Ethernet  HWaddr 08:00:27:FE:6F:BF  
              UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
              RX packets:111 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:8916 (8.7 KiB)  TX bytes:1492 (1.4 KiB)
    
    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:16436  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

    You should see the alias bond0:1 has been created and up.

  • Create Network Bonding On Ubuntu 14.10

    What is Network bonding?

    Network bonding is a method of combining (joining) two or more network interfaces together into a singleinterface. It will increase the network throughput, bandwidth and will give redundancy. If one interface is down or unplugged, the other one will keep the network traffic up and alive. Network bonding can be used in situations wherever you need redundancy, fault tolerance or load balancing networks.

    Linux allows us to bond multiple network interfaces into single interface using a special kernel module namedbonding. The Linux bonding driver provides a method for combining multiple network interfaces into a single logical “bonded” interface. The behaviour of the bonded interfaces depends upon the mode; generally speaking, modes provide either hot standby or load balancing services. Additionally, link integrity monitoring, may be performed.

    Types of Network Bonding

    mode=0 (balance-rr)

    • Round-robin policy: It the default mode. It transmits packets in sequential order from the first available slave through the last. This mode provides load balancing and fault tolerance.

    mode=1 (active-backup)

    • Active-backup policy: In this mode, only one slave in the bond is active. The other one will become active, only when the active slave fails. The bond’s MAC address is externally visible on only one port (network adapter) to avoid confusing the switch. This mode provides fault tolerance.

    mode=2 (balance-xor)

    • XOR policy: Transmit based on [(source MAC address XOR’d with destination MAC address) modulo slave count]. This selects the same slave for each destination MAC address. This mode provides load balancing and fault tolerance.

    mode=3 (broadcast)

    • Broadcast policy: transmits everything on all slave interfaces. This mode provides fault tolerance.

    mode=4 (802.3ad)

    • IEEE 802.3ad Dynamic link aggregation. Creates aggregation groups that share the same speed and duplex settings. Utilizes all slaves in the active aggregator according to the 802.3ad specification.

    Prerequisites:

    – Ethtool support in the base drivers for retrieving the speed and duplex of each slave.
    – A switch that supports IEEE 802.3ad Dynamic link aggregation. Most switches will require some type of configuration to enable 802.3ad mode.

    mode=5 (balance-tlb)

    • Adaptive transmit load balancing: channel bonding that does not require any special switch support. The outgoing traffic is distributed according to the current load (computed relative to the speed) on each slave. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed receiving slave.

    Prerequisite:

    – Ethtool support in the base drivers for retrieving the speed of each slave.

    mode=6 (balance-alb)

    • Adaptive load balancing: includes balance-tlb plus receive load balancing (rlb) for IPV4 traffic, and does not require any special switch support. The receive load balancing is achieved by ARP negotiation. The bonding driver intercepts the ARP Replies sent by the local system on their way out and overwrites the sourcehardware address with the unique hardware address of one of the slaves in the bond such that different peers use different hardware addresses for the server.

    Setting up Network Bonding on Ubuntu 14.10

    I tested this how-to on Ubuntu 14.10, and it worked well.

    We need atleast two network cards. You are free to use n number of NICs.

    I have three network interfaces, namely eth0, eth1 and eth2 in my Ubuntu 14.10 desktop. Let us combine two NICs (eth1 and eth2) and make them into one NIC named bond0.

    Install Bonding Kernel Module

    The following command should be performed with root user privileges.

    sudo su

    First, we have to install bonding kernel module using the command:

    apt-get install ifenslave-2.6

    Now, we have to make sure that the correct kernel module bonding is present, and loaded at boot time.

    Edit /etc/modules file,

    cat /etc/modules

    Add “bonding” at the end.

    # /etc/modules: kernel modules to load at boot time.
    #
    # This file contains the names of kernel modules that should be loaded
    # at boot time, one per line. Lines beginning with "#" are ignored.
    # Parameters can be specified after the module name.
    
    lp
    rtc
    bonding

    Now, stop networking service.

    /etc/init.d/networking stop

    Warning: You should not enter the above command over SSH connection.

    Then load the bonding kernel module:

    sudo modprobe bonding

    Configure Bond0 Interface

    First, let us create a bond0 configuration file as shown below.

    Go to the directory where Debian/Ubuntu stores the network configuration files. By default, Debian and its derivatives stores the network configuration files under /etc/network/ directory.

    Create bond0 configuration file under the above mentioned directory.

    vi /etc/network/interfaces

    Add the following lines marked in red color to create network bond for eth1 and eth2.

    # interfaces(5) file used by ifup(8) and ifdown(8)
    auto lo
    iface lo inet loopback
    
    #eth1 configuration
    auto eth1
    iface eth1 inet manual
    bond-master bond0
    bond-primary eth1
    
    #eth2 configuration
    auto eth2
    iface eth2 inet manual
    bond-master bond0
    
    # Bonding eth1 & eth2 to create bond0 NIC
    auto bond0
    iface bond0 inet static
    address 192.168.1.200
    gateway 192.168.1.1
    netmask 255.255.255.0
    bond-mode active-backup
    bond-miimon 100
    bond-slaves none

    Save and close file.

    Note: Here we will be configuring active-backup mode. 192.168.1.200 is bond0 IP address.

    Next Start/Restart network service to take effect the changes.

    /etc/init.d/networking start

    Bring up bond0:

    ifup bond0

    Note: If you have any problems while bringing up bond0, restart and check again.

    Test Network Bonding

    Now enter the following command to check whether the bonding interface bond0 is up and running:

    cat /proc/net/bonding/bond0

    Sample output:

    Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)
    
    Bonding Mode: fault-tolerance (active-backup)
    Primary Slave: eth1 (primary_reselect always)
    Currently Active Slave: eth1
    MII Status: up
    MII Polling Interval (ms): 100
    Up Delay (ms): 0
    Down Delay (ms): 0
    
    Slave Interface: eth1
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 0
    Permanent HW addr: 08:00:27:33:6e:fc
    Slave queue ID: 0
    
    Slave Interface: eth2
    MII Status: up
    Speed: 1000 Mbps
    Duplex: full
    Link Failure Count: 0
    Permanent HW addr: 08:00:27:7c:b8:02
    Slave queue ID: 0

    As you see in the above output, the bond0 interface is up and running and it is configured as active-backup(mode1) mode. In this mode, only one slave in the bond is active. The other one will become active, only when the active slave fails.

    To view the list of network interfaces and their IP address, enter the following command:

    ifconfig

    Sample output:

    bond0     Link encap:Ethernet  HWaddr 08:00:27:33:6e:fc  
              inet addr:192.168.1.200  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fe33:6efc/64 Scope:Link
              UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
              RX packets:1341 errors:0 dropped:181 overruns:0 frame:0
              TX packets:137 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:943994 (943.9 KB)  TX bytes:10399 (10.3 KB)
    
    eth0      Link encap:Ethernet  HWaddr 08:00:27:09:48:87  
              inet addr:192.168.1.107  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::a00:27ff:fe09:4887/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:957 errors:0 dropped:0 overruns:0 frame:0
              TX packets:829 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:897369 (897.3 KB)  TX bytes:184921 (184.9 KB)
    
    eth1      Link encap:Ethernet  HWaddr 08:00:27:33:6e:fc  
              UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
              RX packets:1143 errors:0 dropped:0 overruns:0 frame:0
              TX packets:137 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:916683 (916.6 KB)  TX bytes:10399 (10.3 KB)
    
    eth2      Link encap:Ethernet  HWaddr 08:00:27:33:6e:fc  
              UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
              RX packets:198 errors:0 dropped:181 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:27311 (27.3 KB)  TX bytes:0 (0.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:653 errors:0 dropped:0 overruns:0 frame:0
              TX packets:653 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0 
              RX bytes:56460 (56.4 KB)  TX bytes:56460 (56.4 KB)

    As per the above output, bond0 is configured as master; eth1 and eth2 are configured as a slave.

  • Setup DHCP Server On CentOS 6.5

    Installation

    To install DHCP server on CentOS 6.5, enter the following command:

    yum install dhcp -y

    Configuration

    DHCP server configuration is not that difficult. First, we have to assign which interface you want your DHCP server to run on. In my case, I have only one Interface on my system (eth0), so I assigned eth0.

    To do that, edit file /etc/sysconfig/dhcpd,

    vi /etc/sysconfig/dhcpd

    Assign the network interface:

    # Command line options here
    DHCPDARGS=eth0

    Save and close the file. Then, copy the sample dhcp configuration file to /etc/dhcp/ directory.

    cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf

    Now, edit dhcpd.conf file,

    vi /etc/dhcp/dhcpd.conf

    Make the changes as shown below.

    Set the domain name and domain-name servers:

    [...]
    
    # option definitions common to all supported networks...
     option domain-name "unixmen.local";
     option domain-name-servers server.unixmen.local;
    
    [...]

    If this DHCP server is the official DHCP server for the local network, you should uncomment the following line:

    [...]
    authoritative;
    [...]

    Define the sunbet, range of ip addresses, domain and domain name servers like below:

    [...]
    # A slightly different configuration for an internal subnet.
     subnet 192.168.1.0 netmask 255.255.255.0 {
     range 192.168.1.20 192.168.1.30;
     option domain-name-servers server.unixmen.local;
     option domain-name "unixmen.local";
     option routers 192.168.1.1;
     option broadcast-address 192.168.1.255;
     default-lease-time 600;
     max-lease-time 7200;
     }
    [...]

    If you want to assign a fixed IP address to your client, you should enter it’s MAC id and the IP address in the following directive. For example, I want to assign a fixed IP address 192.168.1.15 to my Ubuntu client, hence I modified the following directive as shown below.

    [...]
    host ubuntu-client {
     hardware ethernet 00:22:64:4f:e9:3a;
     fixed-address 192.168.1.15;
     }
    [...]

    After making all the changes you want, save and close the file. Be mindful that if you have another unused entries on the dhcpd.conf file, comment them. Otherwise, you’ll have issues while starting dhcpd service.

    Now, start the dhcpd service and make it to start automatically on every reboot.

    service dhcpd start
    chkconfig dhcpd on

    Configure Clients

    Now, go to the client configuration network settings and change the IP settings to Automatic (DHCP).

    Here is my Lubuntu 14.04 settings:

    Editing Wired connection 1_001

    Restart the network or reboot the client system to get IP address automatically from the DHCp server.

    Now, you should see the IP address has been automatically assigned to the clients from the DHCP server.

    Run the following command from the client system Terminal:

    sudo ifconfig

    Sample output:

    sk@sk: ~_002

    As you see in the above picture, My ubuntu client system which has MAC id 00:22:64:4f:e9:3a gets a fixed IP address 192.168.1.15 from the DHCP server.

    That’s it. DHCP server is up and ready.

  • Setup DHCP Server On Ubuntu 14.04 LTS Server

    Installation

    To install DHCP server on Ubuntu 14.04 LTS, enter the following command:

    sudo apt-get install isc-dhcp-server -y

    Configuration

    DHCP server configuration is not that difficult. First, we have to assign on what interfaces should the DHCP server (dhcpd) serve DHCP requests. In my case, I have only one Interface on my system (eth0), so I assignedeth0.

    To do that, edit file /etc/default/isc-dhcp-server,

    sudo nano /etc/default/isc-dhcp-server

    Assign the network interface:

    [...]
    INTERFACES="eth0"

    Save and close the file.

    Now, edit dhcpd.conf file,

    sudo nano /etc/dhcp/dhcpd.conf

    Make the changes as shown below.

    Set the domain name and domain-name servers:

    [...]
    
    # option definitions common to all supported networks...
     option domain-name "unixmen.local";
     option domain-name-servers server.unixmen.local;
    
    [...]

    If this DHCP server is the official DHCP server for the local network, you should uncomment the following line:

    [...]
    authoritative;
    [...]

    Define the sunbet, range of ip addresses, domain and domain name servers like below:

    [...]
    # A slightly different configuration for an internal subnet.
     subnet 192.168.1.0 netmask 255.255.255.0 {
     range 192.168.1.20 192.168.1.30;
     option domain-name-servers server.unixmen.local;
     option domain-name "unixmen.local";
     option routers 192.168.1.1;
     option broadcast-address 192.168.1.255;
     default-lease-time 600;
     max-lease-time 7200;
     }
    [...]

    If you want to assign a fixed IP address to your client, you should enter it’s MAC id and the IP address in the following directive. For example, I want to assign a fixed IP address 192.168.1.15 to my Ubuntu client, hence I modified the following directive as shown below.

    [...]
    host ubuntu-client {
     hardware ethernet 00:22:64:4f:e9:3a;
     fixed-address 192.168.1.15;
     }
    [...]

    After making all the changes you want, save and close the file. Be mindful that if you have another unused entries on the dhcpd.conf file, comment all of them. Otherwise, you’ll get issues while starting dhcp service.

    Now, restart dhcp service and make it to start automatically on every reboot.

    sudo service isc-dhcp-server restart

    Likewise, you can start/stop dhcp service as shown below:

    sudo service isc-dhcp-server start
    sudo service isc-dhcp-server stop

    Configure Clients

    Now, go to the client configuration network settings and change the IP settings to Automatic (DHCP).

    Here is my Lubuntu 14.04 settings:

    Editing Wired connection 1_001

    Restart the network or reboot the client system to get IP address automatically from the DHCP server.

    Now, you should see the IP address has been automatically assigned to the clients from the DHCP server.

    Run the following command from the client system Terminal:

    sudo ifconfig

    Sample output:

    sk@sk: ~_002

    As you see in the above picture, My ubuntu client system which has MAC id 00:22:64:4f:e9:3a gets a fixed IP address 192.168.1.15 from the DHCP server.

    That’s it. DHCP server is up and ready.

  • Regular Expressions in Linux Explained with Examples (RegEx / Regex)

    regex

    Regular expressions (Regexp)is one of the advanced concept we require to write efficient shell scripts and for effective system administration. Basically regular expressions are divided in to 3 types for better understanding.

    1)Basic Regular expressions

    2)Interval Regular expressions (Use option -E for grep and -r for sed)

    3)Extended Regular expressions (Use option -E for grep and -r for sed)

    Some FAQ’s before starting Regular expressions

    What is a Regular expression?

    A regular expression is a concept of matching a pattern in a given string.

    Which commands/programming languages support regular expressions?
    vi, tr, rename, grep, sed, awk, perl, python etc.

    Basic Regular Expressions

    Basic regular expressions: This set includes very basic set of regular expressions which do not require any options to execute. This set of regular expressions are developed long time back.

    ^ –Caret/Power symbol to match a starting at the beginning of line.

    $ –To match end of the line

    * –0 or more occurrence of previous character.

    . –To match any character

    [] –Range of character

    [^char] –negate of occurrence of a character set

    –Actual word finding

    –Escape character

    Lets start with our Regexp with examples, so that we can understand it better.

    ^ Regular Expression

    Example 1: Find all the files in a given directory

    ls -l | grep ^-

    As you are aware that the first character in ls -l output, – is for regular files and d for directories in a given folder. Let us see what ^- indicates. The ^ symbol is for matching line starting, ^- indicates what ever lines starts with -, just display them. Which indicates a regular file in Linux/Unix.

    If we want to find all the directories in a folder use grep ^d option along ls -l as shown below

    ls -l | grep ^d

    How about character files and block files?

    ls -l | grep ^c

    ls -l | grep ^b

    We can even find the lines which are commented using ^ operator with below example

    grep ‘^#’ filename

    How about finding lines in a file which starts with ‘abc’

    grep ‘^abc’ filename

    We can have number of examples with this ^ option.

    $ Regular Expression

    Example 2: Match all the files which ends with sh

    ls -l | grep sh$

    As $ indicates end of the line, the above command will list all the files whose names end with sh.

    how about finding lines in a file which ends with dead

    grep ‘dead$’ filename

    How about finding empty lines in a file?

    grep ‘^$’ filename

     * Regular Expression

    Example 3: Match all files which have a word twt, twet, tweet etc in the file name.

    ls -l | grep ‘twe*t’

    How about searching for apple word which was spelled wrong in a given file where apple is misspelled as ale, aple, appple, apppple, apppppple etc. To find all patterns

    grep ‘ap*le’ filename

    Readers should observe that the above pattern will match even ale word as * indicates 0 or more of previous character occurrence.

    . Regular Expression

    Example 4: Filter a file which contains any single character between t and t in a file name.

    ls -l | grep ‘t.t’

    Here . will match any single character. It can match tat, t3t, t.t, t&t etc any single character between t and t letters.

    How about finding all the file names which starts with a and end with x using regular expressions?

    ls -l | grep ‘a.*x’

    The above .* indicates any number of characters

    Note: .* in this combination . indicates any character and it repeated(*) 0 or more number of times.
    Suppose you have files as..
    awx
    awex
    aweex
    awasdfx
    a35dfetrx
    etc.. it will find all the files/folders which start with a and ends with x in our example.

    [] Square braces/Brackets Regular Expression

    Example 5: Find all the files which contains a number in the file name between a and x

    ls -l | grep ‘a[0-9]x’

    This will find all the files which is
    a0xsdf
    asda1xsdfas
    ..
    ..
    asdfdsara9xsdf
    etc.

    So where ever it finds a number it will try to match that number.

    Some of the range operator examples for  you.

    [a-z] –Match’s any single char between a to z.

    [A-Z] –Match’s any single char between A to Z.

    [0-9] –Match’s any single char between 0 to 9.

    [a-zA-Z0-9] – Match’s any single character either a to z or A to Z or 0 to 9

    [!@#$%^] — Match’s any ! or @ or # or $ or % or ^ character.

    You just have to think what you want match and keep those character in the braces/Brackets.

    [^char] Regular Expression

    Example6: Match all the file names except a or b or c in its filenames

    ls | grep  ‘[^abc]’

    This will give output all the file names except files which contain a or b or c.

    Regular expression

    Example7: Search for a word abc, for example I should not get abcxyz or readabc in my output.

    grep ” filename

    Escape Regular Expression

    Example 8: Find files which contain [ in its name, as [ is a special charter we have to escape it

    grep “[” filename

    or

    grep ‘[[]’ filename

    Note: If you observe [] is used to negate the meaning of [ regular expressions, so if you want to find any specail char keep them in [] so that it will not be treated as special char.

    Note: No need to use -E to use these regular expressions with grep. We have egrep and fgrep which are equal to “grep -E”. I suggest you just concentrate on grep to complete your work, don’t go for other commands if grep is there to resolve your issues.

  • Understanding the Top command on Linux

    Know what is happening in “real time” on your systems is in my opinion the basis to use and optimize your OS. On ArchLinux or better on GNU/Linux in general the top command can help us, this is a very useful system monitor that is really easy to use, and that can also allows us to understand why our OS suffers and which process use most resources. The command to be run on the terminal is:

    $ top

    And we’ll get a screen similar to the one on the right:

    Let’s see now every single row of this output to explain all the information found within the screen.

    1° Row — top

    1

    This first line indicates in order:

    • current time (11:37:19)
    • uptime of the machine (up 1 day, 1:25)
    • users sessions logged in (3 users)
    • average load on the system (load average: 0.02, 0.12, 0.07) the 3 values refer to the last minute, five minutes and 15 minutes.

    2° Row – task

    2

    The second row gives the following information:

    • Processes running in totals (73 total)
    • Processes running (2 running)
    • Processes sleeping (71 sleeping)
    • Processes stopped (0 stopped)
    • Processes waiting to be stoppati from the parent process (0 zombie)

    3° Row – cpu

    3

    The third line indicates how the cpu is used. If you sum up all the percentages the total will be 100% of the cpu. Let’s see what these values indicate in order:

    • Percentage of the CPU for user processes (0.3%us)
    • Percentage of the CPU for system processes (0.0%sy)
    • Percentage of the CPU processes with priority upgrade nice (0.0%ni)
    • Percentage of the CPU not used (99,4%id)
    • Percentage of the CPU processes waiting for I/O operations(0.0%wa)
    • Percentage of the CPU serving hardware interrupts (0.3% hi — Hardware IRQ
    • Percentage of the CPU serving software interrupts (0.0% si — Software Interrupts
    • The amount of CPU ‘stolen’ from this virtual machine by the hypervisor for other tasks (such as running another virtual machine) this will be 0 on desktop and server without Virtual machine. (0.0%st — Steal Time)

    4° and 5° Rows – memory usage

    4

    The fourth and fifth rows respectively indicate the use of physical memory (RAM) and swap. In this order: Total memory in use, free, buffers cached.

    Following Rows — Processes list

    5

    And as last thing ordered by CPU usage (as default) there are the processes currently in use. Let’s see what information we can get in the different columns:

    • PID – l’ID of the process(4522)
    • USER – The user that is the owner of the process (root)
    • PR – priority of the process (15)
    • NI – The “NICE” value of the process (0)
    • VIRT – virtual memory used by the process (132m)
    • RES – physical memory used from the process (14m)
    • SHR – shared memory of the process (3204)
    • S – indicates the status of the process: S=sleep R=running Z=zombie (S)
    • %CPU – This is the percentage of CPU used by this process (0.3)
    • %MEM – This is the percentage of RAM used by the process (0.7)
    • TIME+ –This is the total time of activity of this process (0:17.75)
    • COMMAND – And this is the name of the process (bb_monitor.pl)

    Conclusions

    Now that we have seen in detail all the information that the command “top” returns, it will be easier to understand the reason of excessive load and/or the slowing of the system.

    A good alternative to “ TOP ” is “ HTOP “, an evolution of top with features really amazing.