Networking on linux : Practical examples

internetMap

Introduction

This post gives basic (and less basic) networking tips for linux.

 

 

 

 

1 Interactive shell tips

1.1 Routes related commands

  • Activate the ip forwarding
    The IP forwarding allow your computer to behave just like a router, receive a packet and forward it through the appropriate interface (if there is one!),  according to the routing table.

    • Non-persistent (will be lost at next reboot or network service restart)
      echo 1 > /proc/sys/net/ipv4/ip_forward
    • Persistent (will NOT be lost as above)
      set the net.ipv4.ip_forward parameter to “1” in the /etc/sysctl.conffile via the sysctlcommand:

      sysctl -w net.ipv4.ip_forward=1

      then restart the network service to enable changes:

      /etc/init.d/network restart
  • Route file example
    The file declaring the routes for a fedora based box is /etc/sysconfig/network-scripts/route-eth0. This file is organised in descending numerical order numbered from top to bottom (from n to 0)

    • Example
      GATEWAY2=192.168.13.238
      NETMASK2=255.255.255.0
      ADDRESS2=192.168.13.245
      GATEWAY1=192.168.13.231
      NETMASK1=255.255.255.0
      ADDRESS1=192.168.13.230
      GATEWAY0=192.168.13.132
      NETMASK0=255.255.255.255
      ADDRESS0=192.168.33.220

1.2 Network interfaces related commands

  • Create a non-persistentip alias from cli (this example create an alias for eth0) must be done as root
    /sbin/ifconfig eth0:1 192.168.3.33

    Note : This command does NOT create a persistent alias, to do a persistent one see the next tip

  • Create a persistentip alias from cli (this example create an alias for eth0) must be done as root
    echo "
    # alias created by $USER on $(date +%Y_%m_%d)
    TYPE=Ethernet
    DEVICE=eth0:0
    BOOTPROTO=none
    NETMASK=255.255.255.0
    IPADDR=192.168.3.153
    USERCTL=no
    IPV6INIT=no
    ONPARENT=yes
    PEERDNS=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0:1

 

 

2 Miscellaneous tips

2.1 Activate network on startup

On RHel and CentOS box network is disabled by default on startup, the easiest way to activate it is:

  1. Make sure you have the network service activated for your runlevel (typical Desktop computer run on runlevel 5)
    $ chkconfig --list network
    # gives something like:
    network         0:arrêt 1:arrêt 2:marche        3:marche        4:marche        5:marche        6:arrêt
    • If this is ok, jump to next step, if not run the following
      $ chkconfig network on
  2. Check whether or not the network interface configuration file (/etc/sysconfig/network-scripts/ifcfg-eth0 on RHel / CentOS) is correct : for the network to be automatically up on boot you need the ONBOOTparameter to be set on …. “yes“! as shown below (check line n°4)
    $ cat /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    HWADDR=33:d4:39:42:bg:6h
    ONBOOT=yes
    NM_CONTROLLED=no
    IPADDR=192.168.83.22
    BOOTPROTO=none
    NETMASK=255.255.0.0
    TYPE=Ethernet
    GATEWAY=192.168.83.1
    DNS1=8.8.8.8
    IPV6INIT=no
    USERCTL=no
    PREFIX=16
  3. Once you got those two parameters (chkconfig and ONBOOT) set you should be OK.

The dirty one-liner that will display the current state of network service for your current runlevel :

chkconfig --list  | grep "^network" |grep -Eo "$(/sbin/runlevel |cut -d" " -f2):[[:alpha:]]*"
Tagged on: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.