Configure network settings on the command line

This topic shows you how to configure network settings on the command line. It includes the following:

Add a virtual IP address on the command line

This example shows how to configure a virtual IP address on the network interface eth0.

For the eth0 interface there is a file /etc/sysconfig/network/ifcfg-eth0:

# vi /etc/sysconfig/network/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.0.255
HWADDR=00:0C:29:C8:AA:7C
IPADDR=192.168.0.180
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet

To create the virtual interface eth0:0 with the IP address 192.168.0.101, create the file /etc/sysconfig/network/ifcfg-eth0:0:

# vi /etc/sysconfig/network/ifcfg-eth0:0
DEVICE=eth0:0
BOOTPROTO=static
BROADCAST=192.168.0.255
IPADDR=192.168.0.101
NETMASK=255.255.255.0
NETWORK=192.168.0.0
ONBOOT=yes
TYPE=Ethernet
Note   You can leave out the HWADDR line as it is the same physical network card.

Next, restart the network:

# service network restart

Add a persistent static route on the command line

You can add a static route using commands similar to:

# route add -net …

However, static routes added in this fashion are cleared if the machine is rebooted.

To have static routes persist across reboots, add a file:

/etc/sysconfig/network/ifroute-<network-interface>

The name of the file corresponds to the interface you wish to configure the static routes for. For example, for eth0 the file is named:

/etc/sysconfig/network/ifroute-eth0

And for eth1 the file is named:

/etc/sysconfig/network/ifroute-eth1

For a route added with the following command:

# route add -net 10.0.7.0 netmask 255.255.255.0 gw 192.168.0.9

You can configure it persistently with the following file (/etc/sysconfig/network/ifroute-eth1):

# Destination Dummy/Gateway Netmask Device
#
192.168.200.0 192.168.200.254 255.255.255.0 eth1

You can configure a second route on the same interface by adding a new entry similar to existing route, for example:

19.16.20.0 19.16.20.1 255.255.0.0 eth1

To activate the routes, save the file and run:

# service network restart

View network settings on the command line

To view the current IP address information use the command ifconfig. This outputs the network information for the currently enabled interfaces:

# ifconfig
eth0    Link encap:Ethernet  HWaddr 14:FE:B5:D8:9B:C7 
          inet addr:192.168.0.165  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: 2002:a00:701:0:16fe:b5ff:fed8:9bc7/64 Scope:Global
          inet6 addr: fe80::16fe:b5ff:fed8:9bc7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:624808047 errors:0 dropped:0 overruns:0 frame:0
          TX packets:591961015 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:575050398793 (535.5 GiB)  TX bytes:579919838748 (540.0 GiB)
          Interrupt:36 Memory:d2000000-d2012800

eth1    Link encap:Ethernet  HWaddr 14:FE:B5:D8:9B:C9 
          inet addr:192.168.200.200  Bcast:192.168.255.255  Mask:255.255.0.0
          inet6 addr: 2002:a00:701:0:16fe:b5ff:fed8:9bc9/64 Scope:Global
          inet6 addr: fe80::16fe:b5ff:fed8:9bc9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6373411 errors:0 dropped:0 overruns:0 frame:0
          TX packets:43 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:5507122252 (5.1 GiB)  TX bytes:2858 (2.7 KiB)
          Interrupt:48 Memory:d4000000-d4012800

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:175076 errors:0 dropped:0 overruns:0 frame:0
          TX packets:175076 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:95412457 (90.9 MiB)  TX bytes:95412457 (90.9 MiB)

Passing the -a switch to the command lists all interfaces on the system (whether they are configured or not):

# ifconfig -a
eth0    Link encap:Ethernet  HWaddr 14:FE:B5:D8:9B:C7 
          inet addr:192.168.0.165  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: 2002:a00:701:0:16fe:b5ff:fed8:9bc7/64 Scope:Global
          inet6 addr: fe80::16fe:b5ff:fed8:9bc7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:624808092 errors:0 dropped:0 overruns:0 frame:0
         TX packets:591961027 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:575050403673 (535.5 GiB)  TX bytes:579919841844 (540.0 GiB)
          Interrupt:36 Memory:d2000000-d2012800

eth1    Link encap:Ethernet  HWaddr 14:FE:B5:D8:9B:C9 
          inet addr:192.168.200.200  Bcast:192.168.255.255  Mask:255.255.0.0
          inet6 addr: 2002:a00:701:0:16fe:b5ff:fed8:9bc9/64 Scope:Global
          inet6 addr: fe80::16fe:b5ff:fed8:9bc9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6373439 errors:0 dropped:0 overruns:0 frame:0
          TX packets:43 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:5507125544 (5.1 GiB)  TX bytes:2858 (2.7 KiB)
          Interrupt:48 Memory:d4000000-d4012800

eth2    Link encap:Ethernet  HWaddr 14:FE:B5:D8:9B:CB 
          BROADCAST MULTICAST  MTU:1500  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:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:32 Memory:d6000000-d6012800

eth3    Link encap:Ethernet  HWaddr 14:FE:B5:D8:9B:CD 
          BROADCAST MULTICAST  MTU:1500  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:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:42 Memory:d8000000-d8012800

eth4    Link encap:Ethernet  HWaddr 00:10:18:BA:F1:08 
          BROADCAST MULTICAST  MTU:1500  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:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:38 Memory:da000000-da012800

eth5    Link encap:Ethernet  HWaddr 00:10:18:BA:F1:0A 
          BROADCAST MULTICAST  MTU:1500  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:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:45 Memory:dc000000-dc012800

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:175076 errors:0 dropped:0 overruns:0 frame:0
          TX packets:175076 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:95412457 (90.9 MiB)  TX bytes:95412457 (90.9 MiB)

To see any routing information use the command ip route show:

# ip route show
192.168.0.0/24 dev eth0  proto kernel  scope link  src 192.168.0.165
169.254.0.0/16 dev eth1  scope link
192.168.0.0/16 dev eth1  proto kernel  scope link  src 192.168.200.200
default via 192.168.0.3 dev eth0

When using keepalived for failover, use the command ip addr show to see if a particular interface is serving the virtual IP address:

# ip addr show
1: lo: mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc mq qlen 1000
    link/ether 14:fe:b5:d8:9b:c7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.165/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.221/24 brd 192.168.0.255 scope global eth0 extra virtual
    inet6 2002:a00:701:0:16fe:b5ff:fed8:9bc7/64 scope global dynamic
       valid_lft 2591881sec preferred_lft 604681sec
    inet6 fe80::16fe:b5ff:fed8:9bc7/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc mq qlen 1000
    link/ether 14:fe:b5:d8:9b:c9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.200/16 brd 192.168.255.255 scope global eth1
    inet6 2002:a00:701:0:16fe:b5ff:fed8:9bc9/64 scope global dynamic
       valid_lft 2591881sec preferred_lft 604681sec
    inet6 fe80::16fe:b5ff:fed8:9bc9/64 scope link
       valid_lft forever preferred_lft forever
4: eth2: mtu 1500 qdisc noop qlen 1000
    link/ether 14:fe:b5:d8:9b:cb brd ff:ff:ff:ff:ff:ff
5: eth3: mtu 1500 qdisc noop qlen 1000
    link/ether 14:fe:b5:d8:9b:cd brd ff:ff:ff:ff:ff:ff
6: eth4: mtu 1500 qdisc noop qlen 1000
    link/ether 00:10:18:ba:f1:08 brd ff:ff:ff:ff:ff:ff
7: eth5: mtu 1500 qdisc noop qlen 1000
    link/ether 00:10:18:ba:f1:0a brd ff:ff:ff:ff:ff:ff
#

You can view the NIC statistics for a particular interface using the command ethtool. This can be useful to detect any errors or dropped packets at the network level.

# ethtool -S eth0 
NIC statistics:
     rx_bytes: 575050777107
     rx_error_bytes: 0
     tx_bytes: 579919858796
     tx_error_bytes: 0
     rx_ucast_packets: 623336409
     rx_mcast_packets: 104953
     rx_bcast_packets: 1370388
     tx_ucast_packets: 591960894
     tx_mcast_packets: 5
     tx_bcast_packets: 223
     tx_mac_errors: 0
     tx_carrier_errors: 0
     rx_crc_errors: 0
     rx_align_errors: 0
     tx_single_collisions: 0
     tx_multi_collisions: 0
     tx_deferred: 0
     tx_excess_collisions: 0
     tx_late_collisions: 0
     tx_total_collisions: 0
     rx_fragments: 0
     rx_jabbers: 0
     rx_undersize_packets: 0
     rx_oversize_packets: 0
     rx_64_byte_packets: 822932
     rx_65_to_127_byte_packets: 192118010
     rx_128_to_255_byte_packets: 2436169
     rx_256_to_511_byte_packets: 44374203
     rx_512_to_1023_byte_packets: 25331444
     rx_1024_to_1522_byte_packets: 359736870
     rx_1523_to_9022_byte_packets: 0
     tx_64_byte_packets: 3730692
     tx_65_to_127_byte_packets: 105086741
     tx_128_to_255_byte_packets: 3744087
     tx_256_to_511_byte_packets: 95324821
     tx_512_to_1023_byte_packets: 67990101
     tx_1024_to_1522_byte_packets: 316084680
     tx_1523_to_9022_byte_packets: 0
     rx_xon_frames: 3939
     rx_xoff_frames: 3939
     tx_xon_frames: 0
     tx_xoff_frames: 0
     rx_mac_ctrl_frames: 0
     rx_filtered_packets: 551606
     rx_ftq_discards: 0
     rx_discards: 0
     rx_fw_discards: 0

Restart the network on the command line

If a network restart is required, you can use the service command:

# service network restart

Related Links