Setting a static IP in Ubuntu is useful for a lot of things. You may want to forward ports from your router to serve web pages, use SSH from outside your local network or set up some sort of media server.
For whatever reason, Ubuntu is trying to make our lives easier by introducing graphical tools to be used by default but kind of making our terminal life a bit harder!
The quick answer to setting up a static IP in Ubuntu the proper way is:
- Disable the graphical management of your network connection in /etc/NetworkManager/NetworkManager.conf
- Gather the information for the static IP (interface, IP to be used, default gateway, subnet, DNS)
- Modify /etc/network/interfaces to include the information above.
- Restart networking and network-manager services
These instructions assume you are using a wired ethernet port to set a static IP on and already have an internet connection to create a static IP for (otherwise you would be finding it hard to read this right now!).
Disabling visual (nm-tool) network management
Using Ubuntu’s graphical interface to set a static IP (as described in my previous article about setting a static IP using nm-tool), uses some underlying tools that disable the manual / terminal method which most Linux users are more adapted to.
To regain control of our network interfaces we first have to disable nm-tools’ management of them. Type/copy the following command:
gksudo gedit /etc/NetworkManager/NetworkManager.conf
Your file should look something like this:
[main] plugins=ifupdown,keyfile dns=dnsmasq [ifupdown] managed=false
A few things to do:
- Disable dnsmasq by commenting out the line with a # (hash) at the beginning of the line.
- Set managed (under the ifupdown section) to true.
Dnsmasq offers local DNS caching and network DHCP naming, both of which aren’t necessary on a standard workstation (Some may argue that local DNS caching is useful but I’ve always found Google’s DNS servers to be fast enough). More information on dnsmasq is available from Ubuntu Documentation [https://help.ubuntu.com/community/Dnsmasq].
I find the terminology for ‘managed’ a little confusing here because I have to set this to true to get Network Manager to recognise that I am actually managing this connection manually!
So our NetworkManager.conf now looks like this….
[main] plugins=ifupdown,keyfile #dns=dnsmasq [ifupdown] managed=true
Which connection do I use for my static IP?
So, lets have a look at our current network information, open a terminal and type/copy the following command:
You will now be presented with all the available network connections (whether connected or not). Your output should look something like this:
ifconfig eth0 Link encap:Ethernet HWaddr 11:26:5d:97:t5:e9 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) Interrupt:43 Base address:0xc000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:4390 errors:0 dropped:0 overruns:0 frame:0 TX packets:4390 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:448014 (448.0 KB) TX bytes:448014 (448.0 KB) wlan0 Link encap:Ethernet HWaddr e9:f2:44:e7:b6:a1 inet addr:192.168.0.10 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fa80::fed5:22fe:fec6:a6b9/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:92540 errors:0 dropped:0 overruns:0 frame:0 TX packets:60313 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:107589287 (107.5 MB) TX bytes:9187835 (9.1 MB)
What are the ifconfig connections?
To elaborate on the connections here:
- lo – this is a loopback connection, a virtual connection used to connect to the machine (itself) only
- eth0 – an ethernet interface (wired port)
- wlan0 – a wireless interface (device)
You may well have more eth interfaces, especially in servers where the machine may need to be connected to the network and other machines. You may well have more wlan interfaces if for some reason you have multiple wireless devices connected to your machine.
Setting a static IP for a wireless interface is a bit different to the instructions contained here (to be covered in the future).
I will be using eth0.
What static IP should I use?
There are some IP ranges reserved for private use. These are:
- 10.0.0.0 – 10.255.255.255
- 172.16.0.0 – 172.31.255.255
- 192.168.0.0 – 192.168.255.255
The exact reason for these ranges is explained *more clearly* on wikipedia.
My personal preference is to use IP’s between 192.168.0.100 – 192.168.0.200. Steer clear of using an IP at the beginning and end of any of the ranges as they are usually used for network administration devices.
So, the IP I’m going to use is 192.168.0.101.
What is my Network Gateway?
Your Network Gateway is the gateway between your network and the Internet, the connection between 2 networks – your network and the worlds!
In most residential broadband set-ups, this will be the router provided by your Internet supplier. In commercial scenario’s, this could be a firewall or other routing device.
To find your current gateway type/copy this command:
ip route show
The output will show a line containing the word ‘default’, the IP on this line will be the IP of your Network Gateway… sometimes used interchangeably with Default Gateway (sometimes incorrectly).
My default/network gateway is 192.168.0.1 as shown below on the first line.
default via 192.168.0.1 dev wlan0 proto static 169.254.0.0/16 dev wlan0 scope link metric 1000 192.168.0.0/24 dev wlan0 proto kernel scope link src 192.168.0.10 metric 2
What is my Netmask / Subnet?
A subnet is used to divide a network into logical groups – subnetworks. A Netmask specifies the boundaries of a subnetwork. The exact methods in which it does this are outside the scope of this of this post (maybe one for the future!).
To find your subnet, use ifconfig again. Type/copy the following: (REPLACING <iface> with the interface currently connected to the internet eg. wlan0 or eth0)
ifconfig <iface> | grep Mask
Your subnet is the value after Mask, in 99% of residential router’s this value will be 255.255.255.0.
My subnet is 255.255.255.0.
What about DNS?
DNS (Domain Name Servers) are like indexes which resolve the domain name to an IP. I primarily use Google’s Public DNS but you may choose to use your gateway IP to let your router deal with DNS.
The name servers I will be using are 126.96.36.199 and 188.8.131.52.
Setting the static IP
My /etc/network/interfaces file (on every distro I’ve tried so far) was pretty much empty by default (as below), if you have something more you should be careful with removing entries, start by commenting out lines (using #) to see exactly what you are using.
My current /etc/network/interfaces file looks like this:
auto lo iface lo inet loopback
What do I change in /etc/network/interfaces?
There are 5 items to add to this file:
- Static declaration (using <interface> chosen above): iface <interface> inet static
- Static IP address: address <chosen ip>
- Network gateway: gateway <gateway ip>
- Netmask / subnet: netmask <netmask ip>
- DNS nameservers: dns-nameservers <dns ip’s>
Open your /etc/network/interfaces file for editing by typing/copying the following:
gksudo gedit /etc/network/interfaces
Adding my chosen network interface, static IP, default gateway IP, subnet and nameservers (separated by a space), my /etc/network/interfaces file now looks like this:
auto lo iface lo inet loopback iface eth0 inet static address 192.168.0.101 gateway 192.168.0.1 netmask 255.255.255.0 dns-nameservers 184.108.40.206 220.127.116.11
Restart your networking services
Finally, restart your networking services to put your new settings into effect.
Type/copy the following:
sudo service network-manager restart sudo service networking restart sudo service resolvconf restart
This should restart your network interfaces and services. Use ifconfig to confirm your new static IP!