“Linux is only free if your time is worth nothing.”
- Jamie Zawinski
Configure an IPv4 Address in Linux Server
Overview
This guide provides step-by-step instructions on how to configure an IPv4 address on various Linux distributions.
Prerequisites
- Access to a Linux server (physical or virtual).
- Root or sudo privileges.
- Basic understanding of Linux command line.
Step 1: Identify Network Interface Name
First, you need to identify the network interface you want to configure. Run the following command:
ip link show
This command lists all network interfaces. Look for the interface name (e.g., eth0, ens33, etc.).
Step 2: Configure the IPv4 Address
To configure a static IPv4 address, you need to edit the network configuration files. The location and syntax of these files may vary depending on the Linux distribution.
1. Ubuntu/Debian (using Netplan)
For Ubuntu 17.10 and later, configurations are managed via Netplan:
sudo nano /etc/netplan/01-netcfg.yaml
Add the following lines to configure the static IP:
network:
version: 2
renderer: networkd
ethernets:
:
dhcp4: no
addresses:
- /24
gateway4:
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
Example:
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
2. Ubuntu/Debian (using /etc/network/interfaces)
For older systems or to use the traditional method:
sudo nano /etc/network/interfaces
Add the following lines:
autoiface inet static address netmask gateway dns-nameservers
Example:
auto eth0
iface eth0 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
3. CentOS/RHEL/Fedora
Edit the configuration file for the specific network interface:
sudo nano /etc/sysconfig/network-scripts/ifcfg-
For Ubuntu/Debian (using /etc/network/interfaces)
sudo systemctl restart networking
For CentOS/RHEL/Fedora
sudo systemctl restart network
For Arch Linux
sudo systemctl restart systemd-networkd
Step 4: Verify the Configuration
Run the following command to verify your new IP address:
ip addr show
Check that the interface displays the new IP address you configured.
Summary
By following the steps in this guide, you will have successfully configured a static IPv4 address on your Linux server in various distributions. Always ensure your configuration is correct by verifying connectivity with tools like ping.