Configuring IP Addresses on Network Interfaces
After understanding the basic concepts of Linux network interfaces, let's dive into the process of configuring IP addresses on these interfaces.
Static IP Address Configuration
To configure a static IP address on a network interface, you can use the ip
command. Here's an example:
$ sudo ip addr add 192.168.1.100/24 dev enp0s3
$ sudo ip link set enp0s3 up
This command sets the IP address 192.168.1.100
with a subnet mask of /24
(255.255.255.0) on the enp0s3
interface and then brings the interface up.
Dynamic IP Address Configuration (DHCP)
Alternatively, you can configure the network interface to obtain an IP address dynamically using DHCP (Dynamic Host Configuration Protocol). This is commonly used in environments where IP addresses are managed by a DHCP server. To configure a network interface to use DHCP, you can use the following command:
$ sudo dhclient enp0s3
This command will request an IP address from the DHCP server and configure the enp0s3
interface accordingly.
Persistent IP Address Configuration
To make the IP address configuration persistent across system reboots, you can modify the network configuration files. The location and format of these files may vary depending on your Linux distribution. For example, on Ubuntu 22.04, you can edit the /etc/netplan/00-installer-config.yaml
file and add the following configuration:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: true
This configuration will make the enp0s3
interface use DHCP to obtain an IP address automatically on system boot.
Alternatively, you can use a static IP address configuration by modifying the same file:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
This configuration sets a static IP address of 192.168.1.100
with a subnet mask of /24
, a gateway of 192.168.1.1
, and two DNS servers.
After making the changes, you can apply the new configuration using the sudo netplan apply
command.