To configure a correct IP address and subnet mask on a Linux server is fundamental for network communication. Whether connecting to a local area network (LAN) or the public internet, the IP address acts like a device’s “house number,” while the subnet mask helps distinguish between the “community address” (network part) and the “specific room” (host part). Let’s break down how to configure them step by step.

I. What are IP Addresses and Subnet Masks?

  • IP Address: Consists of 32-bit binary numbers, typically represented in “dotted decimal” format (e.g., 192.168.1.100). It is divided into a network part and a host part.
  • Subnet Mask: Also a 32-bit binary number that tells the system “which part of the IP address is the network address and which is the host address.” In binary, “1” represents the network part, and “0” represents the host part (e.g., 255.255.255.0).

Example:
- IP Address: 192.168.1.100
- Subnet Mask: 255.255.255.0
- Binary Breakdown:
IP: 11000000.10101000.00000001.01100100
Mask: 11111111.11111111.11111111.00000000
Network part: First 3 bytes (192.168.1), Host part: Last 1 byte (100).

II. Check Current Network Configuration

Before configuring, confirm the current IP address and subnet mask. Two common commands are:

  1. ip addr (Recommended, Modern Linux)
    Execute the command directly:
   ip addr

Sample output showing all network interface details:

   2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
       link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
       inet 192.168.1.50/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
           valid_lft 86399sec preferred_lft 86399sec
       inet6 fe80::211:22ff:fe33:4455/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever

Here, eth0 is the network interface name, and inet 192.168.1.50/24 indicates the IP address is 192.168.1.50 with a subnet mask of /24 (equivalent to 255.255.255.0).

  1. ifconfig (Traditional Command, Requires Installation on Some Systems)
    If the ip command is not pre-installed, use ifconfig (install first with yum install net-tools on RHEL/CentOS):
   ifconfig

In the output, the line for eth0 will show:
inet addr:192.168.1.50 Bcast:192.168.1.255 Mask:255.255.255.0 (IP and subnet mask).

III. Temporarily Set IP Address and Subnet Mask

Temporary settings only apply to the current session and will reset after restarting the network service or server (suitable for testing).

# Format: ip addr add <IP>/<prefix> dev <interface>
ip addr add 192.168.1.100/24 dev eth0
  • /24 is equivalent to the subnet mask 255.255.255.0 (24-bit prefix length).
  • To delete the temporary configuration:
  ip addr del 192.168.1.100/24 dev eth0

2. Using the ifconfig Command

# Format: ifconfig <interface> <IP> netmask <subnet_mask>
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

IV. Permanently Set IP Address and Subnet Mask

Temporary settings are for testing; production environments require permanent configurations, which vary by Linux distribution.

1. CentOS/RHEL 7+ (Using network-scripts)

  • Step 1: Confirm the interface name
    Use ip addr to check the interface name (e.g., ens33).

  • Step 2: Edit the interface configuration file
    Configuration file path: /etc/sysconfig/network-scripts/ifcfg-<interface> (e.g., ifcfg-ens33).
    Edit with vim and set the following parameters:

  TYPE=Ethernet
  BOOTPROTO=static  # Set to static for fixed IP
  NAME=ens33        # Must match the interface name
  DEVICE=ens33      # Interface name
  IPADDR=192.168.1.100  # Static IP address
  NETMASK=255.255.255.0 # Subnet mask (or PREFIX=24, equivalent)
  GATEWAY=192.168.1.1    # Gateway (optional, if internet access is needed)
  ONBOOT=yes        # Enable on system boot
  • Step 3: Restart the network service
  systemctl restart network

2. Ubuntu/Debian 18.04+ (Using netplan)

  • Step 1: Confirm the interface name
    Use ip addr to identify the interface name (e.g., ens33).

  • Step 2: Edit the Netplan configuration file
    Configuration file path: /etc/netplan/01-netcfg.yaml (filename can be customized, starting with a number).
    Edit with vim and add:

  network:
    version: 2
    renderer: networkd
    ethernets:
      ens33:  # Interface name
        dhcp4: no  # Disable DHCP for static IP
        addresses: [192.168.1.100/24]  # IP/subnet mask
        gateway4: 192.168.1.1         # Gateway (optional)
        nameservers:
          addresses: [8.8.8.8, 8.8.4.4] # DNS servers (optional)
  • Step 3: Apply the configuration
  sudo netplan apply

V. Verify the Configuration

After configuration, verify success with:
1. Check IP Address: Run ip addr to confirm the IP and subnet mask are active.
2. Test Network Connectivity:
- Ping the local IP: ping 192.168.1.100 (should receive responses).
- Ping a device in the same subnet: ping 192.168.1.50 (responds if another device exists).
- Ping the gateway: ping 192.168.1.1 (responds if a gateway is configured).

VI. Key Considerations

  1. IP Uniqueness: No duplicate IPs in the same subnet to avoid conflicts.
  2. Subnet Mask Matching: Ensure the IP and subnet mask are consistent (e.g., 192.168.1.100 with 255.255.0.0 may require cross-subnet configuration).
  3. Interface Name Accuracy: Interface names vary by system (e.g., eth0, ens33, ens160); confirm with ip addr.
  4. Permissions: Network changes require root privileges; use sudo or root user.

By following these steps, you now understand the basic configuration of IP addresses and subnet masks on Linux servers. Next, explore more advanced configurations like gateways and DNS.

Xiaoye