# Setting a Static IP Address on Linux


Connecting to the internet is essential for most Linux systems. While many home networks use dynamic IP addressing, sometimes you may want to set a static IP address that doesn't change.

Setting a static IP address can make it easier to access other devices on your local network, allow incoming connections to your system, and ensure your Raspberry Pi always has the same address.

Luckily, configuring a static IP address on Linux is straightforward using a simple command. Here's what you need to know.

## The Basics of IP Addresses

Every device connected to a network is assigned an IP (Internet Protocol) address. This unique string of numbers allows the device to communicate with others on the network.

Dynamic IP addresses are assigned automatically by the router using DHCP (Dynamic Host Configuration Protocol). They can change periodically. This works fine for client devices like laptops.

However, devices like servers, printers, and Raspberry Pis often need a static address that never changes. This allows you to always know how to access them. Setting a static IP prevents problems if the router assigns that address to another device later.

## Checking the Current IP Address

Before setting a static IP, you'll want to check your Linux system's current address. Knowing this allows you to set the static one to the same address.

To find your IP address, open a terminal and use the `ip addr show` command. This displays information about all network interfaces.

Look for the section on your main network interface, likely called `eth0` or `enp2s0`. The line starting with `inet` shows your IP address:

```plaintext
inet 192.168.1.22/24 brd 192.168.1.255 scope global noprefixroute eth0
```

Here, the IP address is `192.168.1.22`. Yours will likely be different.

You may also see an `inet6` line showing an IPv6 address if your network uses it. IPv6 is a newer standard; for now, focus on the IPv4 address starting with 192, 10, or 172.

## Using the `ip` Command to Configure Static IP

With your current IP address in hand, you can set the static one using the `ip` command.

The basic syntax is:

```plaintext
sudo ip addr add your_ip/netmask dev interface
```

- `your_ip` - Your desired static IP address
- `netmask` - Subnet mask, usually 255.255.255.0
- `interface` - Network interface name like `eth0`

For example, to set an address of 192.168.1.25 on interface `eth0`, you would run:

```plaintext
sudo ip addr add 192.168.1.25/24 dev eth0
```

This sets 192.168.1.25 as your IP address each time you connect to the network. The `/24` specifies the `255.255.255.0` netmask.

You can confirm it worked using `ip addr show`. You should see your new static IP address.

## Making the Change Persistent

One catch of using `ip addr` is that it only sets the static IP for the current boot session. If you restart, the interface could get a dynamic address again.

To make the change persistent across reboots, you need to edit a configuration file.

Open this file in your preferred text editor:

```plaintext
/etc/dhcpcd.conf
```

At the bottom, add a new section like:

```plaintext
interface eth0
static ip_address=192.168.1.25/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
```

Replace eth0 with your interface name, 192.168.1.25 with your desired static IP, and 192.168.1.1 with your router's IP.

Now your system will use this static configuration every time it boots!

## Setting Static IP on Raspberry Pi

Raspberry Pi OS uses a different syntax to configure networking. Edit this file instead:

```plaintext
/etc/dhcpcd.conf
```

And add:

```plaintext
interface eth0
static ip_address=192.168.1.105/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
```

The parameters are the same as above. Restart your Pi, and it will now have a fixed address.

> Also read - [**How to Get a List of All Open Ports in Linux**](https://developnsolve.com/how-to-get-a-list-of-all-open-ports-in-linux)

## Potential Downsides to Static IPs

While static IP addresses have benefits, a few downsides exist too:

- Requires checking for IP conflicts if the router assigns your static one to another device.
- Minor additional router configuration complexity.
- Loss of connectivity if the network parameters change.

In most home and small business scenarios, these risks are minor compared to the rewards. However, on large corporate networks, extensive DHCP and DNS systems reduce the need for static addressing.

> Also read - [**Mounting Drives in Linux Made Simple**](https://developnsolve.com/mounting-drives-in-linux-made-simple)

## Summary of Key Points

Setting a static IP address on Linux is handy for servers, Raspberry Pis, printers, and devices that you always want to be able to locate reliably on your network.

The key steps are:

1. Check your current dynamic IP with `ip addr show`
2. Set a static IP with `sudo ip addr add ip/netmask dev interface`
3. Make it persistent by adding a static configuration section in `/etc/dhcpcd.conf`

And remember to pick an IP outside the router's DHCP allocation pool to avoid conflicts.

With the power of the Linux `ip` command, you're now ready to boost connectivity and access for your home lab by setting static IP addresses. No GUI is required.

So try it out on your Raspberry Pi, server, or other Linux system. Having a reliable static address makes networking and administering Linux so much easier.

