Add static IP Configuration to the network configuration file. Restart Ubuntu Networking Service. Check Available Network Interfaces on Ubuntu Server. First of all you need to get the list of available network interfaces on your Ubuntu Server 16.04. We can use ip link show command to find available network interfaces on Ubuntu Linux. This tutorial explains how to set a static IP on an Ubuntu system from the command line. It covers the network configuration for all recent Ubuntu versions and includes instructions to configure a static IP address, set the hostname and configure name resolving. The network configuration has been.
Ubuntu installs with a default setting for DHCP on the primary network adapter. Setting a static IP address is quick and easy in Ubuntu, just not intuitive.
6 Steps total
Step 1: Understand network configurations in Ubuntu
Ubuntu/Debian, like most Linux distributions and unlike Windows, is configured mostly through text files.
In Windows, the TCP/IP properties dialog of a network connection shows you the basic IP address configuration including DNS settings. They can be viewed or changed in that dialog, given that you have the appropriate permissions to do so.
In Ubuntu, the IP configuration and DNS settings are configured in three different files: /etc/hosts, /etc/resolv.conf, and /etc/network/interfaces.
The /etc/hosts file is what DNS replaced. In the 70's, if you had several computers networked, you had to have a configured hosts file on each computer describing the location of all the other computers on the network! DNS was simply a way for all the computers to reference a centrally maintained list. It is still wise to have a hosts file configured with at least the parameters of the machine itself, for backup and for applications that need to resolve the local hostname when DNS isn't available.
Open /etc/hosts for editing. (I use vim, see my How-To on installing vim)
:~$ sudo vim /etc/hosts
If your DHCP server hands out the domain name with the IP assignment, the default configuration will look something like this:
127.0.0.1 localhost
127.0.1.1 myhost.mydomain.com myhost
# The following lines are desirable for IPv6 capable hosts
[A lot of IPv6 stuff I'm not going to get into]
Step 2: Edit /etc/hosts
NOTE: I use vim with syntax highlighting, so instructions are referenced from that perspective.
Comment the second line in the default hosts file, which should begin with 127.0.1.1. Comment by adding a # to the beginning of the line. The commented line (in vim) should turn a light blue.
Now insert a new line below it including your selected static IP address:
[example]
192.168.100.100 myhost.mydomain.com myhost
Save and close the file.
Step 3: Edit /etc/resolv.conf
The /etc/resolv.conf file in Unix/Linux performs the same function as the 'Preferred DNS server:' fields in Windows network configuration.
In an Active Directory domain the DNS suffix is automatically configured. You have to tell Linux where it lives. Edit the /etc/resolv.conf file:
:~$ sudo vim /etc/resolv.conf
Depending on how your DHCP is configured, there may be nothing to do. A default resolv.conf file looks something like:
search mydomain.com
nameserver 192.168.100.1
if you are configuring your Ubuntu machine to be on a different domain that the one it recieved the DHCP configuration from, change the search line. This should probably be the same domain that was configured in /etc/hosts:
# search mydomain.com
search myotherdomain.com
Change the nameserver IP address if you want to use a different DNS server.
Save and close the file.
Step 4: Inspect /etc/network/interfaces
The /etc/network/interfaces file is where the nitty gritty configuration takes place. Open the file for editing:
:~$ sudo vim /etc/network/interfaces
If you only have one network card in your Ubuntu machine, the file should have the following lines:
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet dhcp
Step 5: Edit /etc/network/interfaces
Now comes the really difficult part. Find and comment the line:
iface eth0 inet dhcp
Now insert these new lines below it, using your own IP, subnet mask, and default gateway, so that it now looks like:
# iface eth0 inet dhcp
iface eth0 inet static
address 192.168.100.100
netmask 255.255.255.0
gateway 192.168.100.254
That's it. Done. You basically type 'Here's the address', 'Here's the subnet mask', 'Here's the gateway'....ok, go.
Routing in Linux can be squirelly, so I like to make sure that the routing entry for the default gateway is there every time the interface comes up. At the end of my /etc/network/interfaces file will be a line like:
up route add default gw 192.168.100.254
If you change your IP to static and then cannot apt-get or otherwise access the Internet, you might try adding that additional line (with the address of YOUR router/gateway) to the end of your interfaces file.
NOTE: We aren't touching the loopback interface lines, they stay as-is during the configuration.
Step 6: Make Ubuntu recognize the new configuration
Ubuntu is running happily along with the configuration it read the last time it booted or the interfaces were brought up. We need to make it read the new one. You can either reboot or cycle the network interfaces at the command line.
:~$ sudo /etc/init.d/networking restart
If you get any funny output try rebooting.
Go to it!
Ubuntu makes a great server platform, but the default DHCP configuration for a server is generally regarded as a
Bad Thing(TM)
2 Comments
- Chipotlemitrum Aug 31, 2010 at 02:36am
Nice article!
- Cayennedrehfeldt May 27, 2011 at 08:25am
This is a good reference. It's also good practice to backup a file prior to editing it. For example, I generally have a working directory, but your home directory would do, then the command:
$sudo cp /path/to/file/filename . (yes, that is a single period, which puts the file in directory from which you are working)
$sudo cp /path/to/file/filename /path/to/file/filename.bak
From the office of, 'If it's not broken don't fix it' comes this: In Ubuntu Server, there's a brand new method of setting IP addresses. Gone are the days of manually editing the flat text /etc/network/interfaces file. In its place is netplan. That's right, Ubuntu fans, the method you've known for years is now a thing of the past. Instead of a very simple text file, Ubuntu Server requires editing a .yaml file (complete with proper adherence to correct code indent for each line of the block), in order to configure your IP addressing.
More about Networking
Before you panic, it's not all that challenging. In fact, it's really just a matter of understanding the layout of these .yaml files and how networking is now restarted. I'm going to show you just that, such that you can configure a static IP address in Ubuntu Server 18.04 as easily as you could in 16.04.
The new method
Open up a terminal window on your Ubuntu 18.04 server (or log in via secure shell). Change into the /etc/netplan directory with the command cd /etc/netplan. Issue the command ls and you should see a file named 50-cloud-init.yaml. If you don't also see a file named 01-netcfg.yaml, create it with the command sudo touch 01-netcfg.yaml. Before we edit that file, we need to know the name of our networking interface. Issue the command ip a and you should see your system network interface listed by name (Figure A).
Figure A
Now we're going to create a new netplan configuration file. If you don't see the 01-netcfg.yaml file, create one with the command sudo nano 01-netcfg.yaml. Our file is going to look like that which you see in Figure B.
Figure B
What's crucial about the layout of this file is not using the exact same spacing as my example, but that you're consistent. If you're not consistent with your indents, the file will not work. What you see in that sample file is all you need to configure that static IP address. Do notice, you aren't setting the address is the same fashion as you did with Ubuntu 16.04. With the old method, you set IP address and netmask like so:
With netplan, these are set with a single line:
Restarting/testing networking
With the new method, you must restart networking using netplan. So once you've configured your interface, issue the command:
The above command will restart networking and apply the new configuration. You shouldn't see any output. If networking fails to function properly, you can issue the command:
The output of the command (Figure C) should give you some indication as to what's going wrong.
Figure C
That's all there is to it
There ya go. That's all there is to configuring a static IP address in Ubuntu Server 18.04. Remember, you'll have to do this for each interface you have on your server. Make sure to name the files something like 01-netcfg.yaml and 02-netcfg-yaml. It's not terribly difficult, once you're used to not working with that old-school interfaces file.
Open Source Weekly Newsletter
You don't want to miss our tips, tutorials, and commentary on the Linux OS and open source applications. Delivered Tuesdays
Sign up today Sign up todayAlso see
- How to install Ubuntu Server 18.04 (TechRepublic)
- How to enable remote desktop connections in Ubuntu 18.04 (TechRepublic)
- How to enable an automatic purge of temp files in Ubuntu 18.04 (TechRepublic)
- How to connect Ubuntu 18.04 to your Google account (TechRepublic)
- Ubuntu 18.04 LTS: The Linux for AI, clouds, and containers (ZDNet)