- Posted on
- • Getting Started
Networking: Configuring IP Addresses and Subnets
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Mastering Networking with Linux Bash: Configuring IP Addresses and Subnets
When working with Linux systems, especially in server environments, configuring network settings such as IP addresses and subnets is a fundamental skill. Let’s explore how to manage these configurations effectively using Linux Bash, covering different package managers and distributions.
Understanding IP Addresses and Subnets
Before diving into configurations, it's vital to understand what IP addresses and subnets are. An IP address is a unique address that identifies a device on the internet or a local network. The subnet mask, on the other hand, defines the network portion of an IP address, allowing the possibility to differentiate the network segment, the device is on, from other devices.
Pre-Requisite Tools
To manage our network configurations from the command line, we need the appropriate tools installed on our Linux system. The most common tool for this task under modern Linux distributions is ip
, part of the iproute2
package. Here's how to ensure it's installed on your Linux distribution:
Debian/Ubuntu (using apt
)
sudo apt update && sudo apt install iproute2
Fedora (using dnf
)
sudo dnf install iproute2
openSUSE (using zypper
)
sudo zypper install iproute2
With the iproute2
package installed, we can start configuring our network settings.
Configuring IP Address with ip
Command
To configure an IP address on a Linux system, you primarily use the ip
command followed by addr
and add
keywords.
1. Check current IP configurations
First, check your current IP configuration with:
ip addr show
2. Assign a new IP address
Specify the IP address and the subnet mask. For instance, to set the IP address 192.168.1.100
with a subnet mask of 255.255.255.0
on eth0
, you would use:
sudo ip addr add 192.168.1.100/24 dev eth0
This command sets the IP address and the /24
indicates the subnet mask.
3. Verify the changes
Verify that the IP was added:
ip addr show eth0
Persistent Network Configuration
The above method sets the IP address temporarily. It will revert after a reboot. To make it permanent, you have to edit network configuration files, which vary by distribution.
Debian/Ubuntu
On Debian-based systems, edit the /etc/network/interfaces
file:
sudo nano /etc/network/interfaces
Add the following lines to set the static IP:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
Fedora
For Fedora, the configuration files are found under /etc/sysconfig/network-scripts/
. Edit the script for your interface, typically named ifcfg-eth0
:
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
Configure the script like so:
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPADDR=192.168.1.100
PREFIX=24
GATEWAY=192.168.1.1
DEVICE=eth0
ONBOOT=yes
openSUSE
On openSUSE, use Wicked
, which is a network management suite:
sudo nano /etc/sysconfig/network/ifcfg-eth0
Add these lines:
BOOTPROTO='static'
IPADDR='192.168.1.100/24'
STARTMODE='auto'
Conclusion
Understanding and configuring IP addresses and subnets on various Linux distributions is crucial for effective network management. Whether it's a temporary change with ip
command or a permanent setup via configuration files, Linux offers the flexibility needed for efficient network administration. Always ensure that you have backups of configuration files before you make changes to prevent downtime due to misconfiguration.
Happy networking! 🌐