# How to Assign an IP Address in Linux


Assigning an IP address in Linux is a fundamental skill for managing networks. Whether you’re setting up a server, configuring a desktop, or troubleshooting connectivity, knowing how to assign IP addresses manually or automatically is essential. In this guide, I’ll walk you through the different ways to assign an IP address in Linux, from quick command-line methods to permanent configurations.

You’ll learn how to use tools like `ip`, `ifconfig`, and network configuration files. I’ll also explain the difference between static and dynamic IP addresses and how to make your changes stick after a reboot. By the end, you’ll feel confident managing IP addresses on any Linux system.

## Understanding IP Addresses in Linux

Before diving into the commands, it’s helpful to understand what an IP address is and why it matters. An IP address is a unique identifier for a device on a network. It allows your Linux machine to communicate with other devices, access the internet, or be accessed remotely.

There are two main types of IP addresses you can assign:

- **Static IP Address:** Manually set and does not change unless you modify it.
- **Dynamic IP Address:** Automatically assigned by a DHCP server and can change over time.

Choosing between static and dynamic depends on your needs. For servers or devices that require a fixed address, static IPs are best. For most desktops or laptops, dynamic IPs are more convenient.

## Assigning an IP Address Temporarily Using the Command Line

If you want to assign an IP address quickly without making permanent changes, the command line is your best friend. This method is useful for testing or temporary setups.

### Using the `ip` Command

The `ip` command is the modern and preferred tool for network configuration in Linux. It replaces the older `ifconfig` command and offers more features.

To assign an IP address temporarily, follow these steps:

1. Identify your network interface name using:
   ```bash
   ip link show
   ```
   Common interface names include `eth0`, `enp3s0`, or `wlan0`.

2. Assign the IP address:
   ```bash
   sudo ip addr add 192.168.1.100/24 dev eth0
   ```
   Replace `192.168.1.100/24` with your desired IP and subnet mask, and `eth0` with your interface name.

3. Bring the interface up if it’s down:
   ```bash
   sudo ip link set eth0 up
   ```

4. Verify the assignment:
   ```bash
   ip addr show eth0
   ```

This change lasts until the next reboot or network restart.

### Using the `ifconfig` Command

Though deprecated, `ifconfig` is still available on many systems and works similarly:

```bash
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
```

This assigns the IP and brings the interface up. Like with `ip`, this is temporary.

## Assigning a Static IP Address Permanently

Temporary assignments are lost after reboot. To keep your IP address fixed, you need to edit network configuration files. The method depends on your Linux distribution and network manager.

### For Systems Using NetworkManager

NetworkManager is common on desktop Linux distributions like Ubuntu, Fedora, and CentOS.

1. Open the NetworkManager configuration GUI or use the `nmcli` command-line tool.

2. To set a static IP with `nmcli`:

```bash
sudo nmcli con show
```
Find your connection name, then:

```bash
sudo nmcli con mod "Wired connection 1" ipv4.addresses 192.168.1.100/24
sudo nmcli con mod "Wired connection 1" ipv4.gateway 192.168.1.1
sudo nmcli con mod "Wired connection 1" ipv4.dns 8.8.8.8
sudo nmcli con mod "Wired connection 1" ipv4.method manual
sudo nmcli con up "Wired connection 1"
```

Replace the IP, gateway, and DNS with your network details.

### Editing `/etc/network/interfaces` on Debian/Ubuntu

On older Debian-based systems or servers without NetworkManager, you configure static IPs by editing `/etc/network/interfaces`.

Example configuration:

```bash
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
```

After saving, restart networking:

```bash
sudo systemctl restart networking
```

### Configuring Static IP on Red Hat/CentOS with `ifcfg` Files

Red Hat-based systems use files in `/etc/sysconfig/network-scripts/`.

Edit or create `/etc/sysconfig/network-scripts/ifcfg-eth0`:

```bash
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
```

Restart the network service:

```bash
sudo systemctl restart network
```

## Assigning a Dynamic IP Address Using DHCP

Most Linux systems get IP addresses automatically via DHCP. If your interface is set to use DHCP, it will request an IP from the network’s DHCP server.

### Using `dhclient`

You can manually request a DHCP lease with:

```bash
sudo dhclient eth0
```

This command asks the DHCP server for an IP address and applies it to the interface.

### Configuring DHCP in NetworkManager

To set DHCP via `nmcli`:

```bash
sudo nmcli con mod "Wired connection 1" ipv4.method auto
sudo nmcli con up "Wired connection 1"
```

This switches the connection to dynamic IP assignment.

## Troubleshooting IP Address Assignment Issues

Sometimes, assigning an IP address doesn’t work as expected. Here are common problems and solutions:

- **Interface not found:** Check the interface name with `ip link show`.
- **Permission denied:** Use `sudo` to run commands with root privileges.
- **IP conflict:** Ensure no other device uses the same IP.
- **Network service not restarting:** Use `sudo systemctl restart NetworkManager` or `network` depending on your system.
- **Firewall blocking traffic:** Check firewall settings with `sudo ufw status` or `iptables -L`.

## Useful Commands to Verify IP Address and Network Status

After assigning an IP, verify your settings with these commands:

- `ip addr show` — Displays all interfaces and their IPs.
- `ip route` — Shows routing table and default gateway.
- `ping 8.8.8.8` — Tests internet connectivity.
- `nmcli device status` — Lists network devices and their states.
- `systemctl status NetworkManager` — Checks if NetworkManager is running.

## Summary Table: Assigning IP Addresses in Linux

| Method                     | Command/File Location                          | Temporary or Permanent | Notes                          |
|----------------------------|-----------------------------------------------|-----------------------|-------------------------------|
| Temporary IP with `ip`      | `sudo ip addr add IP/Mask dev interface`      | Temporary             | Modern, preferred method       |
| Temporary IP with `ifconfig`| `sudo ifconfig interface IP netmask Mask up`  | Temporary             | Deprecated but still used      |
| Static IP with NetworkManager| `nmcli con mod` commands                      | Permanent             | GUI or CLI options             |
| Static IP with `/etc/network/interfaces` | Edit file directly                   | Permanent             | Debian/Ubuntu older systems    |
| Static IP with `ifcfg` files| Edit `/etc/sysconfig/network-scripts/ifcfg-*`| Permanent             | Red Hat/CentOS systems         |
| Dynamic IP with DHCP        | `sudo dhclient interface` or NetworkManager   | Temporary/Permanent    | Automatic IP assignment        |

## Conclusion

Assigning an IP address in Linux can be simple or detailed depending on your needs. For quick tests, using the `ip` command is fast and effective. For permanent setups, editing configuration files or using NetworkManager ensures your IP stays consistent after reboots.

Understanding the difference between static and dynamic IPs helps you choose the right method. Also, knowing how to troubleshoot common issues saves time and frustration. With these tools and tips, you can confidently manage IP addresses on any Linux system.

---

### FAQs

#### How do I find my network interface name in Linux?

You can find your interface name by running `ip link show`. It lists all network interfaces, such as `eth0`, `enp3s0`, or `wlan0`.

#### Can I assign multiple IP addresses to one interface?

Yes, you can assign multiple IPs using the `ip addr add` command multiple times with different addresses on the same interface.

#### How do I make IP changes permanent on Ubuntu?

Edit `/etc/network/interfaces` or use NetworkManager’s GUI or `nmcli` commands to set static IPs permanently.

#### What is the difference between `ip` and `ifconfig`?

`ip` is the modern tool with more features and better support, while `ifconfig` is older and deprecated but still available on many systems.

#### How do I release and renew a DHCP IP address?

Use `sudo dhclient -r interface` to release and `sudo dhclient interface` to renew the DHCP lease.
