How to Change Netmask Without Altering IP Address Using ifconfig in Linux


4 views

When administering Linux systems, we often need to modify network interface parameters. A common requirement is to change the netmask without affecting the existing IP address. The standard ifconfig syntax typically requires specifying both IP and netmask:

ifconfig eth0 10.10.10.10 netmask 255.255.255.0

When attempting to change just the netmask:

ifconfig eth0 netmask 255.255.255.0

You'll encounter the error:

ifconfig: ioctl (SIOCAIFADDR): Invalid argument

This occurs because ifconfig's underlying implementation expects both IP and netmask parameters when making changes.

Method 1: Using iproute2

The modern ip command from iproute2 package provides more flexibility:

ip addr change 10.10.10.10/24 dev eth0

Or to keep the existing IP while changing netmask:

current_ip=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
ip addr change $current_ip/24 dev eth0

Method 2: Using ifconfig with Current IP

First get current IP, then apply changes:

current_ip=$(ifconfig eth0 | grep -o 'inet addr:[^ ]*' | cut -d: -f2)
ifconfig eth0 $current_ip netmask 255.255.255.0

Method 3: Network Manager (for Desktop Systems)

For systems with NetworkManager:

nmcli connection modify eth0 ipv4.netmask 255.255.255.0
nmcli connection up eth0

For permanent changes, modify network configuration files:

Debian/Ubuntu (/etc/network/interfaces)

auto eth0
iface eth0 inet static
address 10.10.10.10
netmask 255.255.255.0

RHEL/CentOS (/etc/sysconfig/network-scripts/ifcfg-eth0)

DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
IPADDR=10.10.10.10
NETMASK=255.255.255.0

After making changes, verify with:

ifconfig eth0
# or
ip addr show eth0

When managing network interfaces in Unix/Linux systems, administrators often need to modify the netmask while keeping the existing IP address intact. The standard ifconfig syntax requires both parameters to be specified together:

ifconfig eth0 192.168.1.100 netmask 255.255.255.0

Attempting to change only the netmask parameter results in an error:

ifconfig eth0 netmask 255.255.255.0
ifconfig: ioctl (SIOCAIFADDR): Invalid argument

This occurs because the command interprets the netmask as an IP address when no IP parameter is provided.

Method 1: Using Current IP Address

First retrieve the current IP, then reapply it with new netmask:

CURRENT_IP=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | awk '{print $2}')
ifconfig eth0 $CURRENT_IP netmask 255.255.255.128

Method 2: Using iproute2 Tools

The modern ip command allows cleaner netmask changes:

ip addr change dev eth0 192.168.1.100/25

Method 3: Temporary Interface Down

For older systems where other methods fail:

ifconfig eth0 down
ifconfig eth0 up netmask 255.255.255.192
  • Always verify changes with ifconfig eth0 or ip addr show eth0
  • For persistent changes, modify network configuration files:
    /etc/network/interfaces (Debian/Ubuntu)
    /etc/sysconfig/network-scripts/ifcfg-eth0 (RHEL/CentOS)
  • Consider using CIDR notation (/24, /25 etc.) for better readability

If changes don't take effect:

  1. Check for NetworkManager conflicts
  2. Verify interface existence with ip link show
  3. Test with both IPv4 and IPv6 disabled temporarily