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.

Network configuration on Ubuntu 17.10 and Ubuntu 18.04 LTS

The network configuration has been changed completely with Ubuntu 17.10. A new tool named Netplan has been introduced by Canonical (the company that develops the Ubuntu Linux distribution) for network setting management which will be used in all new Ubuntu versions starting with the 17.10 release. The former network configuration file /etc/network/interfaces is not used anymore.

Configure a Static IP Address with Netplan on Ubuntu

Here the steps to configure a static IP address with Netplan. The Netplan configuration files are located in the directory /etc/netplan/. The default configuration file is /etc/netplan/01-netcfg.yaml.

Open the network config file with an editor:

The configuration syntax is in Python programming language (.yaml format), so the indentation of the lines is important!

Here is an example of a static IPv4 address 192.168.1.100 on the first network interface ens33 and gateway IP 192.168.1.1. The server will use the free Google DNS servers 8.8.8.8 and 8.8.4.4 to for name resolving.

Or as Screenshot from an Ubuntu 18.04 LTS server:

Ubuntu neetwork configuration with netplan

An IPv6 address can be added in the addresses line, separated by a comma. Example:

It is important that you wrap the IPv6 address into single quotes, you will get a syntax error otherwise.

To apply the changes, run:

Or use it with the –debug switch to get some useful output if parsing of the netplan config file was successful.

Configure a DHCP address with Netplan

Here is the configuration to get the network configuration for IPv4 and IPv6 from a DHCP server.

To apply the changes, run:

More Netplan config options

Netplan is a complex new configuration system to configure network cards, virtual devices, VLANs and bridges in Ubuntu 18.04. For more examples and an in-depth explanation of the syntax, see the man page.

Network configuration on Ubuntu 12.04 – 17.04 (incl. Ubuntu 16.04 LTS)

Step 1: Configure the network interface

In this step, you will manually configure your network interface by editing the following files using your preferred text editor(nano gedit vi). For the purpose of this example, I’m using the “nano” editor. You can edit the appropriate file by entering the following command into the terminal:

You can copy and paste directly from this line.

Enter your root password, once your preferred editor opens the file you can see this on older Ubuntu versions:

Ubuntu Systems with systemd (like Ubuntu 16.04 and newer), the network interface is named ens33 instead of eth0 now and the word ‘dynamic’ has been replaced with ‘dhcp’.

A configuration where the IP address get’s assigned automatically by DHCP will look like this:

Statically configured network cards will have a section like this on older Ubuntu versions:

Here is an example for an older Ubuntu Release:

And here an example for Ubuntu 16.04 and newer:

And here the complete network configuration file from an Ubuntu 16.04 system.

Ubuntu network configuration file

If you use “nano” editor to edit the configuration file, type Ctrl+x to save changes.

Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES) ?

<—Type “y”

File Name to Write: interfaces

<—ENTER

Step 2: Configure the DNS servers

Changes in /etc/resolv.conf are required only on Systems with Ubuntu < 14.04, for newer Ubuntu versions the nameservers get configured in the /etc/network/interfaces file.

a) Ubuntu versions < 14.04

In this step, you will manually configure your dns configuration file.

Once your editor opens the file you want to enter the following information…

If you use “nano” editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES) ?

<—Type “y”

File Name to Write: resolv.conf

<—ENTER

Here is an example:

b) Ubuntu versions 14.04 and newer

Open the /etc/network/interfaces file again and add a line dns-nameservers 8.8.8.8 right after the gateway line.

The nameservers 8.8.8.8 and 8.8.4.4 are provided by Google for public use, so you can use them in your network configuration.

If you use “nano” editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING “No” WILL DESTROY CHANGES) ?

<—Type “y”

File Name to Write: interfaces

<—ENTER

Step 3: Restart networking

Manually restart your network interface with the new settings.

For Ubuntu < 14.04 use the networking init script:

This should return a result that looks like the following:

For Ubuntu versions 14.04 and newer use systemctl instead:

At this point you can check if the settings are correct:

If everything is correct you will get this result.

Configure the Hostname

The hostname of an Ubuntu Server or Desktop is being configured in the files /etc/hostname and /etc/hosts. The /etc/hostname file sets the actual system hostname while /etc/hosts is used for the local name resolution.

In this example, I will change the hostname of my system to obelix.example.com.

First, edit the /etc/hostname file

The hostname file contains the local part of the hostname only. The local part here is “obelix”. Change the content of the /etc/hostname file to:

and save the file. The hostname file as it looks in nano after editing:

Edit the hostname on Ubuntu

Then open the /etc/hosts file with an editor:

and change the line that starts with the IP address of the system like this:

Here a screenshot of the hosts file.

Configure the hosts file

The format is like this:

[IP Addesss] [full hostname incl. domain] [local part of the hostname]

Finally, restart the system to apply the hostname change.

分类: Linux

0 条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据