Networking Basics in Linux: Network Settings and Multiple IPs

Basic Networking Linux
Table of Contents

Setting up multiple IP addresses on a single network interface, allowing for more flexible and efficient management of services and resources is a common task in Linux Administration.

In this article, we’ll explore networking basics in Linux, including how to configure network settings, and focus on how to configure a server to use multiple IP addresses on a single interface.

Understanding Linux Networking Basics

Before diving into configuring multiple IPs on a single interface, it’s essential to have a good understanding of how Linux networking works.

IP Addresses, Subnets, and Interfaces

Every device on a network must have an IP address, which is a unique identifier allowing it to communicate with other devices. IP addresses can be either:

  • IPv4 (e.g., 192.168.1.10)
  • IPv6 (e.g., 2001:db8::1)

Each IP address belongs to a subnet, which defines the range of IPs that can communicate without routing.

Devices are connected through network interfaces, which are physical or virtual cards like eth0, enp0s3, or wlp2s0. These interfaces manage network traffic to and from the device.

Network Configuration Files in Linux

Linux systems use various methods to configure network settings, including:

  • NetworkManager: A dynamic and user-friendly tool for managing network interfaces.
  • Netplan (for Ubuntu): YAML-based configuration files to define networking.
  • ifconfig/ip tools: For manual or legacy configurations.
  • Systemd-networkd: A native systemd service to handle networking.

For the purposes of this guide, we’ll focus on manual network configuration using standard tools and files, such as:

  • /etc/network/interfaces (Debian/Ubuntu-based)
  • /etc/sysconfig/network-scripts/ifcfg-* (RHEL/CentOS-based)

Configuring Network Settings in Linux

Linux systems provide both command-line utilities and configuration files to manage network settings. Let’s start with basic configuration steps.

Viewing and Managing Network Interfaces

You can view network interfaces using the ip command, which is the modern replacement for ifconfig.

ip addr show

This command will list all network interfaces, along with their assigned IP addresses. The output might look like this:

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.1.100/24 brd 192.168.1.255 scope global dynamic enp0s3
    inet6 fe80::a00:27ff:fe5a:ca90/64 scope link

To assign a static IP address temporarily, you can use:

sudo ip addr add 192.168.1.200/24 dev enp0s3

This adds the IP 192.168.1.200 with a /24 subnet mask to the enp0s3 interface. The IP will be lost after a reboot unless made persistent, which we’ll address later.

Static and Dynamic IP Address Assignment

In most scenarios, you’ll either use DHCP (dynamic assignment) or manually configure a static IP. Here’s an example of how to configure static IPs in different Linux distros.

Note: to view the current gateway IP use the command:  ip route | grep default

  • Debian/Ubuntu: Edit /etc/network/interfaces to assign a static IP:
auto enp0s3
iface enp0s3 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4
  • RHEL/CentOS: Edit /etc/sysconfig/network-scripts/ifcfg-enp0s3:
DEVICE=enp0s3
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8

Restart networking with:

sudo systemctl restart networking  # Debian/Ubuntu
sudo systemctl restart network     # RHEL/CentOS

Setting up Multiple IP Addresses on a Single Interface

Why Use Multiple IPs on a Single Interface?

There are several reasons why you might want to assign multiple IP addresses to a single interface:

  • Hosting multiple websites on the same server (with different IPs).
  • Load balancing or high availability configurations.
  • Network segmentation or service isolation.

Configuring Multiple IP Addresses with ip Command

You can easily add additional IP addresses using the ip command:

sudo ip addr add 192.168.1.101/24 dev enp0s3
sudo ip addr add 192.168.1.102/24 dev enp0s3

In this case, we’re assigning two additional IPs (192.168.1.101 and 192.168.1.102) to the enp0s3 interface. These will take effect immediately but will not persist after a reboot.

Persistent Configuration of Multiple IPs

To make these IP assignments persistent, you need to configure the network settings files.

  • Debian/Ubuntu: In /etc/network/interfaces, you can define alias interfaces like this:
auto enp0s3
iface enp0s3 inet static
    address 192.168.1.100
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4

# Additional IPs as aliases
auto enp0s3:1
iface enp0s3:1 inet static
    address 192.168.1.101
    netmask 255.255.255.0

auto enp0s3:2
iface enp0s3:2 inet static
    address 192.168.1.102
    netmask 255.255.255.0

Each alias (enp0s3:1, enp0s3:2) represents a new IP address.

  • RHEL/CentOS: You can create additional configuration files for each IP. For example, in /etc/sysconfig/network-scripts/, create a new file ifcfg-enp0s3:1 for the first additional IP:
DEVICE=enp0s3:1
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.101
NETMASK=255.255.255.0

Similarly, create ifcfg-enp0s3:2 for the second additional IP.

Testing and Verifying Network Configuration

After configuring multiple IP addresses, it’s essential to verify the setup.

  1. Check Network Interfaces: Use the ip command to confirm that all IPs are correctly assigned:
    ip addr show
  2. Ping the IPs: Test connectivity by pinging the newly configured IP addresses:
    ping 192.168.1.101
    ping 192.168.1.102
  3. Check Service Binding: If you’re using multiple IPs to host services, ensure that they are correctly bound to the respective IPs. For instance, you can configure Apache or Nginx to listen on specific IP addresses.

Conclusion

Configuring network settings in Linux is an essential skill for any system administrator, and managing multiple IP addresses on a single interface allows for more versatile network setups.

Whether you’re hosting multiple websites, creating virtual environments, or segmenting network traffic, understanding how to configure multiple IPs using both temporary and persistent methods is key to efficient server management.

By using tools like the ip command for temporary changes and modifying configuration files for persistent setups, you can take full control of network interfaces and their assigned IP addresses.

Regular testing and verification of network configurations ensure that your settings are functioning as expected.

Latest articles
Picture of Endri Bedini
Endri Bedini

Endri Bedini is a laureate in Mechanical Engineering with over 20 years of experience in various technology fields, including Electronics, IT, and Healthcare Equipment. Throughout his career, Endri has honed his skills and expertise, earning a reputation for his exceptional problem-solving abilities and innovative thinking. In addition to his work in technology, Endri has a deep interest in Science, Astronomy, AI, Psychology, Sociology, Nature, and Evolution. He is committed to staying up-to-date with the latest developments in these fields, and his insights are informed by his broad range of knowledge and interests.

Read also

Receive new posts and updates at your e-mail address.

Subscription Form
Scroll to Top