3011.io

Static IP addresses on a local network

This is a post describing how to set up static IP addresses on Linux machines using NetworkManager. It also mirrors my own setup quite a bit, so in YMMV in case you're trying to use this as a general guide.

IP Ranges

For the most part, private networks usually use a reserved address range1, usually with a 24 bit mask2. On my networks, I usually use address spaces like 10.X.0.0/24, where X is usually a single digit number corresponding to the nesting level of the network behind NATs. Addresses between 10.X.0.50 and 10.X.0.200 (inclusive) are left for the DHCP to give out to devices that don't need a static IP address.

My setup

I use the following IP ranges in my current network. All the ranges are inclusive.

NetworkManager configuration

My preference is setting the static IP address on each computer separately. The other option is to set the IP address in the router directly. That keeps all the configuration in one place, but also requires messing with router settings and using MAC addresses, which is also less than ideal.

NOTE! The configuration you set up here must match up with the subnet configuration on the your router, otherwise you're asking for trouble.

Anyway, firstly, get the name of the connection (assuming the computer is already connected to the network via DHCP).

$ sudo nmcli -o connection show

Next set the IP address, and the other network information.

$ sudo nmcli c mod $CONNECTION_NAME ipv4.addresses 10.1.0.220/24 ipv4.method manual
$ sudo nmcli c mod $CONNECTION_NAME ipv4.gateway 10.1.0.1
$ sudo nmcli c mod $CONNECTION_NAME ipv4.dns 10.1.0.1 # This can be a comma separated list

Of course, don't forget to substitute the $CONNECTION_NAME with the actual name of the connection you want to change. Also, multiple IP addresses can be specified via a comma separated list in quotes.

Finally, after all the setup is done, restart the connection so it binds to the configured IP. This will disconnect any remote connection.

$ sudo nmcli c down $CONNECTION_NAME && sudo nmcli c up $CONNECTION_NAME

To review information about the network after it's set up, use the following.

$ nmcli -p connection show $CONNECTION_NAME

In case the static IP address is no longer needed, it can be reset using the following command. Keep in mind that the connection needs to be restarted same as above for the changes to show.

$ sudo nmcli c mod $CONNECTION_NAME ipv4.method auto

nmap scan for occupied addresses

nmap is a powerful, but often confusing tool. The simplest, and most reliable way to check which IP addresses are already in use is the following (IME, YMMV).

$ sudo nmap -sV -T4 -O -F --version-light 10.1.0.0/24

1: You can check the reserved ranges here on Wikipedia

2: The reserved address space is technically 10.0.0.0/8, I'm just using its subsets for my subnets.