top of page

Configure an IP address in Linux

Jun 15, 2024

2 min read

0

18

To configure an IP address in Linux, you typically use the `ip` command or modify configuration files depending on whether you're using a dynamic (DHCP) or static IP address.


Here's how you can do it:

Configuring IP Address Using `ip` Command


#### For a Dynamic IP Address (DHCP)

1. Check Network Interface Name:

Use the `ip addr` command or `ifconfig` command to find the name of your network interface. For example, `eth0`, `enp0s3`, `wlan0`, etc.


2. Request IP Address from DHCP Server:

Use the `dhclient` command to request an IP address from the DHCP server:

sudo dhclient <interface_name>

Replace `<interface_name>` with your actual network interface name (e.g., `eth0`, `enp0s3`, `wlan0`).


3. Verify IP Configuration:

Check if you have received an IP address:

ip addr show <interface_name>

Example output:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0

valid_lft 345600sec preferred_lft 345600sec

inet6 fe80::a00:27ff:fe8b:6d59/64 scope link

valid_lft forever preferred_lft forever

Here, `192.168.1.10` is the assigned dynamic IP address.


#### For a Static IP Address

1. Edit Network Configuration File:

Open the network configuration file for editing. Depending on your Linux distribution, the file may vary:

- Ubuntu/Debian: `/etc/network/interfaces`

- CentOS/RHEL: `/etc/sysconfig/network-scripts/ifcfg-<interface_name>`

Example using `nano` editor:


sudo nano /etc/network/interfaces # For Ubuntu/Debian

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0 # For CentOS/RHEL

2. Configure Static IP Address:

Add or modify the following lines in the configuration file:

auto <interface_name>

iface <interface_name> inet static

address 192.168.1.100 # Replace with your desired IP address

netmask 255.255.255.0 # Replace with your subnet mask

gateway 192.168.1.1 # Replace with your gateway IP address (optional)

dns-nameservers 8.8.8.8 # Replace with your DNS server IP address (optional)

Replace `<interface_name>`, `address`, `netmask`, `gateway`, and `dns-nameservers` with your network interface name and the desired IP configuration details.


3. Apply Configuration Changes:

Save and close the file.

- Ubuntu/Debian: Restart networking services or reboot your system:

sudo systemctl restart networking

- CentOS/RHEL: Restart the network service:

sudo systemctl restart network

4. Verify IP Configuration:

Verify that the static IP address is configured correctly:

ip addr show <interface_name>

Example output:

2: eth0: <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 eth0

valid_lft forever preferred_lft forever

inet6 fe80::a00:27ff:fe8b:6d59/64 scope link

valid_lft forever preferred_lft forever

Here, `192.168.1.100` is the assigned static IP address.


- Always replace `<interface_name>`, `address`, `netmask`, `gateway`, and `dns-nameservers` with your actual values.

- Ensure network interface names and configuration file paths match your Linux distribution.

- For servers and permanent configurations, use static IP addresses to avoid IP changes upon reboots.

- DHCP is generally used for dynamic IP address assignments, suitable for client systems or when IP addresses change frequently.


By following these steps, you can effectively configure IP addresses on Linux systems using either dynamic (DHCP) or static IP address assignments. Adjust the commands and paths according to your specific Linux distribution and networking requirements.


#linuxdevicedrivers #ldd #linuxlovers



Jun 15, 2024

2 min read

0

18

bottom of page