• The vast majority of network programming in Linux is done using the socket interface. Thus, standards-compliant programs should require little massage to work properly with Linux.


  • Note, however, there are many enhancements and new features in the Linux networking implementation, such as new kinds of address and protocol families.


  • For example, Linux offers the netlink interface, which permits opening up socket connections between kernel sub-systems and applications (or other kernel sub-systems).


  • This has been effectively deployed to implement firewall and routing applications.


  • Historically, the wired Ethernet network devices have been known by a name such as eth0, eth1, etc., while wireless devices have had names like wlan0, wlan1, etc.


  • Basic information about active network interfaces on your system is gathered through the ifconfig utility:


  •  
    $ /sbin/ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:22:15:2B:64:A6
              inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:163529 errors:0 dropped:0 overruns:0 frame:0
              TX packets:112693 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:183642176 (175.1 MiB)  TX bytes:12101864 (11.5 MiB)
              Interrupt:18
    eth1      Link encap:Ethernet  HWaddr 00:22:15:2B:63:BE
              inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:162597 errors:0 dropped:0 overruns:0 frame:0
              TX packets:56710 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:206698846 (197.1 MiB)  TX bytes:75532637 (72.0 MiB)
              Interrupt:17
              
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:15115 errors:0 dropped:0 overruns:0 frame:0
              TX packets:15115 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:126793920 (120.9 MiB)  TX bytes:126793920 (120.9 MiB)
              
    
  • Information displayed includes information about the hardware MAC address, the MTU (maximum transfer unit), and the IRQ the device is tied to. Also displayed are the number of packets and bytes transmitted, received, or resulting in errors.


  • This machine has two network cards bound to eth0 and eth1, and the loopback interface, lo, which handles network traffic bound to the machine.


  • Note you can see the statistical information in abbreviated form by looking at /proc/net/dev, and in one quantity per line display in /sys/class/net/eth0/statistics:


  •  
    $ ls -l /sys/class/net/eth0/statistics
    total 0
    -r--r--r-- 1 root root 4096 Mar 26 17:21 collisions
    -r--r--r-- 1 root root 4096 Mar 26 17:30 multicast
    -r--r--r-- 1 root root 4096 Mar 26 17:20 rx_bytes
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_compressed
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_crc_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_dropped
    -r--r--r-- 1 root root 4096 Mar 26 17:20 rx_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_fifo_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_frame_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_length_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_missed_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 rx_over_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:20 rx_packets
    -r--r--r-- 1 root root 4096 Mar 26 17:30 tx_aborted_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:20 tx_bytes
    -r--r--r-- 1 root root 4096 Mar 26 17:30 tx_carrier_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 tx_compressed
    -r--r--r-- 1 root root 4096 Mar 26 17:30 tx_dropped
    -r--r--r-- 1 root root 4096 Mar 26 17:21 tx_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 tx_fifo_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:30 tx_heartbeat_errors
    -r--r--r-- 1 root root 4096 Mar 26 17:20 tx_packets
    -r--r--r-- 1 root root 4096 Mar 26 17:30 tx_window_errors
     
    
  • We will now consider how network interface devices are actually named. There was a time when this was a very static situation. You had one or more network devices attached to your computer, they did not change with time, they were generally wired devices, not wireless.


  • And so, assigning a specific name to it and sticking to it, and using that name in all your system administration scripts was perfectly acceptable.


  • However, these days, the situation is far from static,especially when you consider wireless.


  • You may have a number of different wireless networks available, you have to pick one, etc. Also, in terms of even Ethernet connections, or network cards, you may have multiple ones attached to your machine.


  • And if you just use a name scheme such as eth0, eth1, eth2, etc. It can be problematic if you don't know what order these devices are found when the system starts.


  • And this can change according to kernel updates, according to distribution differences, etc.


  • So, it is important to have a more systematic way to name things, which doesn't just depend on what order they were found in, or something like that.


  • There's always been approaches for dealing with this, but the modern approach adopted by all distributions is called the Predictable Network Interface Device scheme.


  • The five possible names devices can have. It could be provided by the firmware in the system, or the BIOS. It could depend on where it's plugged into the system, like if it's in a PCI Express lock.


  • They can use the actual geographical description, or the physical location of the connection, the so-called MAC address, which is unique to every hardware device you have, whether it's a wireless card or an Ethernet card.


  • Or you can go back to the old simple order of its filesystem like eth0, eth1, wlan0, wlan1, etc..


  • This can all be changed through administration. You can decide what you want.


  • In order to do system administration on network devicese you have to know their names.


  • To bring a network connection up and assign a static address, you can do:


  •  
    $ sudo /sbin/ifconfig eth0 up 192.168.1.100
     
    
  • To bring it up and get it an assigned address from a DHCP server, you can do:


  •  
    $ sudo /sbin/ifconfig eth0 up
    $ sudo /sbin/dhclient eth0
     
    
  • While ifconfig has been used reliably for many years, the ip utility is newer (and far more versatile). On a technical level, it is more efficient because it uses netlink sockets, rather than ioctl system calls.


  • ip can be used for a wide variety of tasks. It can be used to display and control devices, routing, policy-based routing, and tunneling. The basic syntax is:


  •  
    ip [ OPTIONS ] OBJECT { COMMAND | help }
     
    
  • Some examples: Show information for all network interfaces:


  •  
    $ ip link
     
    
  • Show information for the eth0 network interface:


  •  
    $ ip -s link show eth0
     
    
  • Set the IP address for eth0:


  •  
    $ sudo ip addr add 192.168.1.7 dev eth0
     
    
  • Bring eth0 down:


  •  
    $ sudo ip link set eth0 down
     
    
  • Set the MTU to 1480 bytes for eth0:


  •  
    $ sudo ip link set eth0 mtu 1480
     
    
  • Set the networking route:


  •  
    $ sudo ip route add 172.16.1.0/24 via 192.168.1.5 
    
    Note: You may have to use a different network interface name than eth0. Also note that you can most easily do this exercise with nmtui or your system’s graphical interface.


  • We will present a command line solution, but beware, details may not exactly fit your distribution flavor or fashion.


  • Show your current IP address and default route for eth0. Bring down eth0 and reconfigure to use a static address instead of DCHP, using the information you just recorded.


  • Bring the interface back up. Make sure your configuration works after a reboot.


  • You will probably want to restore your configuration when you are done.


  • Solution


  • 1.


  •  
    $ ip addr show eth0
    
    
  • or


  •  
    $ ifconfig eth0
    
    
  • 2. Assuming the address was 192.168.1.100:


  •  
    $ sudo ip link set eth0 down 
    $ sudo ip addr add 192.168.1.100 dev eth0 
    $ sudo ip link set eth0 up
    
    
  • or


  •  
    $ sudo ifconfig eth0 down 
    $ sudo ifconfig eth0 up 192.168.1.100
    
    
  • 3.


  •  
    $ sudo ip link set eth0 up 
    $ sudo dhclient eth0 
    
    
  • or


  •  
    $ sudo ifconfig eth0 up 
    $ sudo dhclient eth0
    
    
  • 4.


  •  
    $ sudo reboot