Jump directly to main content

How to set a static IP



For those that don't know a static IP is an IP address that doesn't change on reboots, etc. The opposite of a dynamic IP, which can change on reboots, and is the default mode for computers to get an IP (via DHCP).

Get your network

If you don't know what network/range of ports you're on, check first by following the below. If you do, and this is a flash-card of sorts, ignore this section.

ip a

You can likely gauge from this, what network you're on, as most routers default to a single range, with the router being the first address on that range

So... If your ip a shows 192.168.1.xxx (after the last dot doesn't matter), I assume your address will need to be 192.168.1.x, and your gateway will be 192.168.1.1

Set your static IP

Open up the /etc/network/interfaces file with your editor of choice

sudo vim /etc/network/interfaces

And edit the file to look a little something like below. If that's a little difficult to understand, read on

auto enp2s0
iface enp2s0 inet static
        address 192.168.1.226
        netmask 255.255.255.0
        gateway 192.168.1.1
        dns-namespaces 192.168.1.1 8.8.8.8 8.8.4.4

1) Set static network device

Change the iface for your network device from

iface enp2s0 inet dhcp

to

iface enp2s0 inet static

Substituting enp2s0 for whatever your device is called. This device will pre-exist in the file, but can also be found with the following command

ip a

2) Add your network details

Below the editted line, add the following, based on your network (slightly mentioned in the very first section)

address 192.168.1.220

This is the IP that you want your PC to have, the number after the final dot, can be between (inclusively) 2, and 254

Followed by (on a new line)

netmask 255.255.255.0

This is an assumption about the average home network

Then, again on a new line

gateway 192.168.1.1

Another assumption, but this is your routers IP, that traditionally sits at the first IP

3) Add your dns details

I've seperated this out, as this is a common point of failure for setting a static IP, so make sure this is gucci

So below the address, etc. Add this line

dns-namespaces 8.8.8.8 8.8.4.4

This basically tells your PC where to look to find domain names

Restart your networking service

Now restarting your networking service (or entire PC if you so desire) will set your computer's IP to that in the /etc/network/interfaces file.

Simply use the command below, and voila

sudo systemctl restart networking