Skip to content

Networking in Linux

image.png

image.png

  • /etc/hosts → This file is used to manually map hostnames to IP addresses. It provides a static mapping, and the system checks this file first when resolving a hostname., those cant be resolved by DNS, eg: localhost

  • /etc/resolv.conf: → This file is used to configure DNS servers that the system should use to resolve domain names into IP addresses. When the system cannot resolve a name via /etc/hosts, it queries the DNS servers listed in this file.

    • search → means Automatically append example.com to any unqualified domain names (like server1) when attempting to resolve them. eg: when you ping server1, it is translated to server1.example.com
  • /etc/sysconfig/network→ This file contains global network configuration settings for the entire system. It is used to configure network-wide parameters that affect the system’s networking behavior.

    • Typical Contents of /etc/sysconfig/network:

      Terminal window
      NETWORKING=yes
      HOSTNAME=localhost.localdomain
      • NETWORKING=yes: This setting controls whether the networking service is enabled at boot. If set to no, the network interfaces will not be brought up at boot.

      • HOSTNAME=localhost.localdomain: This setting specifies the system’s hostname. The hostname is used to identify the system on a network.

        Other settings in this file might include:

        • NETWORKING_IPV6=yes: Enables or disables IPv6 networking.
        • GATEWAY=192.168.1.1: Sets the default gateway for the system (typically used when there is no route for a packet).
  • /etc/sysconfig/network-scripts/ifcfg-*→ This directory contains configuration files for each network interface on the system. The files are named ifcfg-*, where * is the name of the network interface (e.g., ifcfg-eth0, ifcfg-enp0s3, ifcfg-ens33, etc.). Each file describes how a specific interface should be configured.

For example:

  • /etc/sysconfig/network-scripts/ifcfg-eth0 (older naming convention)
  • /etc/sysconfig/network-scripts/ifcfg-enp0s3 (modern naming convention using Predictable Network Interface Names)
Terminal window
DEVICE=enp0s3
BOOTPROTO=dhcp
ONBOOT=yes
IPV6INIT=no
  • DEVICE=<interface>: Specifies the network interface name (e.g., eth0, enp0s3, ens33, etc.).
  • BOOTPROTO=dhcp: Defines how the interface will obtain an IP address. Common options are:
    • dhcp: The system will use DHCP (Dynamic Host Configuration Protocol) to automatically obtain an IP address.
    • static: The system will use a static IP address specified by other directives (e.g., IPADDR, NETMASK).
  • ONBOOT=yes: This directive tells the system to bring up this interface during boot. If set to no, the interface will not be brought up automatically at boot.
  • IPADDR=<ip_address>: Specifies a static IP address if BOOTPROTO=static is used.
  • NETMASK=<netmask>: Specifies the subnet mask for the static IP address.
  • GATEWAY=<gateway_ip>: Specifies the default gateway for this interface (if using a static IP).
  • DNS1=<dns_ip> and DNS2=<dns_ip>: Specifies the DNS servers that should be used when resolving domain names.
  • IPV6INIT=no: This setting controls whether IPv6 should be enabled for this interface. no disables IPv6, yes enables it.

image.png

image.png