Linux set static ip address

How to Configure Static IP Address on Ubuntu 18.04

In this tutorial, we’ll explain how to set up a static IP address on Ubuntu 18.04.

Generally, IP addresses are assigned dynamically by your router DHCP server. Setting a static IP address on your Ubuntu machine may be required in different situations, such as configuring port forwarding or running a media server on your network.

Configuring Static IP address using DHCP #

The easiest and the recommended way to assign a static IP address to a device on your LAN is by setting up a Static DHCP on your router. Static DHCP or DHCP reservation is a feature found on most routers which makes the DHCP server to automatically assign the same IP address to a specific network device, every time the device requests an address from the DHCP server. This works by assigning a static IP to the device’s unique MAC address. The steps for configuring a DHCP reservation vary from router to router, and it’s advisable to consult the vendor’s documentation.

Netplan #

Starting with 17.10 release, Netplan is the default network management tool on Ubuntu, replacing the configuration file /etc/network/interfaces that had previously been used to configure the network on Ubuntu.

Netplan uses configuration files in YAML syntax. To configure a network interface with Netplan, you need to create a YAML description for that interface, and Netplan will generate the required configuration files for your chosen renderer tool.

Netplan currently supports two renderers NetworkManager and Systemd-networkd. NetworkManager is mostly used on Desktop machines while the Systemd-networkd is used on servers without a GUI.

Configuring Static IP address on Ubuntu Server #

The newer versions of Ubuntu uses ‘Predictable Network Interface Names’ that, by default, start with en[letter][number] .

The first step is to identify the name of the ethernet interface you want to configure. To do so use the ip link command, as shown below:

The command will print a list of all the available network interfaces. In this case, the name of the interface is ens3 :

1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3: ens3: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 56:00:00:60:20:0a brd ff:ff:ff:ff:ff:ff 

Netplan configuration files are stored in the /etc/netplan directory and have the extension .yaml . You’ll probably find one or two YAML files in this directory. The file may differ from setup to setup. Usually, the file is named either 01-netcfg.yaml , 50-cloud-init.yaml , or NN_interfaceName.yaml , but in your system it may be different.

Читайте также:  Kvm linux over linux

Open the YAML configuration file with your text editor :

sudo nano /etc/netplan/01-netcfg.yaml
network:  version: 2  renderer: networkd  ethernets:  ens3:  dhcp4: yes 

Before changing the configuration, let’s explain the code in a short.

Each Netplan Yaml file starts with the network key that has at least two required elements. The first required element is the version of the network configuration format, and the second one is the device type. The device type can be ethernets , bonds , bridges , or vlans .

The configuration above also includes the renderer type. Out of the box, if you installed Ubuntu in server mode, the renderer is configured to use networkd as the back end.

Under the device’s type (in this case ethernets ), you can specify one or more network interfaces. In this example, we have only one interface ens3 that is configured to obtain IP addressing from a DHCP server dhcp4: yes .

To assign a static IP address to ens3 interface, edit the file as follows:

  • Set DHCP to dhcp4: no .
  • Specify the static IP address 192.168.121.199/24 . Under addresses: you can add one or more IPv4 or IPv6 IP addresses that will be assigned to the network interface.
  • Specify the gateway gateway4: 192.168.121.1
  • Under nameservers , set the IP addresses of the nameservers addresses: [8.8.8.8, 1.1.1.1]
network:  version: 2  renderer: networkd  ethernets:  ens3:  dhcp4: no  addresses:  - 192.168.121.199/24  gateway4: 192.168.121.1  nameservers:  addresses: [8.8.8.8, 1.1.1.1]

When editing Yaml files, make sure you follow the YAML code indent standards. If there are syntax errors in the configuration, the changes will not ne applied.

Once done save and close the file and apply the changes with:

Verify the changes by typing:

3: ens3: mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 56:00:00:60:20:0a brd ff:ff:ff:ff:ff:ff inet 192.168.121.199/24 brd 192.168.121.255 scope global dynamic ens3 valid_lft 3575sec preferred_lft 3575sec inet6 fe80::5054:ff:feb0:f500/64 scope link valid_lft forever preferred_lft forever 

That’s it! You have assigned a static IP to your Ubuntu server.

Configuring Static IP address on Ubuntu Desktop #

Setting up a static IP address on Ubuntu Desktop computers requires no technical knowledge.

  1. In the Activities screen, search for “network” and click on the Network icon. This will open the GNOME Network configuration settings. Click on the cog icon.
  2. The Network interface settings dialog box will be opened:
  3. In “IPV4” Method» section, select “Manual” and enter your static IP address, Netmask and Gateway. Once done, click on the “Apply” button.

Now that you have set up a static IP Address, open your terminal either by using the Ctrl+Alt+T keyboard shortcut or by clicking on the terminal icon and verify the changes by typing:

The output will show the interface IP address:

. 2: wlp1s0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:e9:40:f2 brd ff:ff:ff:ff:ff:ff inet 192.168.121.106/24 brd 192.168.121.255 scope global dynamic noprefixroute ens3 valid_lft 3523sec preferred_lft 3523sec inet6 fe80::5054:ff:fee9:40f2/64 scope link valid_lft forever preferred_lft forever 

Conclusion #

You have learned how to assign a static IP address on your Ubuntu 18.04 machine.

If you have any questions, please leave a comment below.

Источник

Setting a Static IP in Ubuntu – Linux IP Address Tutorial

Zaira Hira

Zaira Hira

Setting a Static IP in Ubuntu – Linux IP Address Tutorial

In most network configurations, the router DHCP server assigns the IP address dynamically by default. If you want to ensure that your system IP stays the same every time, you can force it to use a static IP.

Читайте также:  Change user without password linux

That’s what we will learn in this article. We will explore two ways to set a static IP in Ubuntu.

Static IP addresses find their use in the following situations:

  • Configuring port forwarding.
  • Configuring your system as a server such as an FTP server, web server, or a media server.

Pre-requisites:

To follow this tutorial you will need the following:

  • Ubuntu installation, preferably with a GUI.
  • sudo rights as we will be modifying system configuration files.

How to Set a Static IP Using the Command Line

In this section, we will explore all the steps in detail needed to configure a static IP.

Step 1: Launch the terminal

You can launch the terminal using the shortcut Ctrl+ Shift+t .

Step 2: Note information about the current network

We will need our current network details such as the current assigned IP, subnet mask, and the network adapter name so that we can apply the necessary changes in the configurations.

Use the command below to find details of the available adapters and the respective IP information.

The output will look something like this:

image-14

For my network, the current adapter is eth0 . It could be different for your system

As my current adapter is eth0 , the below details are relevant.

6: eth0: mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:15:5d:df:c3:ad brd ff:ff:ff:ff:ff:ff inet 172.23.199.129/20 brd 172.23.207.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::215:5dff:fedf:c3ad/64 scope link valid_lft forever preferred_lft forever

It is worth noting that the current IP 172.23.199.129 is dynamically assigned. It has 20 bits reserved for the netmask. The broadcast address is 172.23.207.255 .

We can find the subnet mask details using the command below:

Select the output against your adapter and read it carefully.

image-15

Based on the class and subnet mask, the usable host IP range for my network is: 172.23.192.1 — 172.23.207.254 .

Subnetting is a vast topic. For more info on subnetting and your usable IP ranges, check out this article.

Step 3: Make configuration changes

Netplan is the default network management tool for the latest Ubuntu versions. Configuration files for Netplan are written using YAML and end with the extension .yaml .

Note: Be careful about spaces in the configuration file as they are part of the syntax. Without proper indentation, the file won’t be read properly.

ls into the /etc/netplan directory.

If you do not see any files, you can create one. The name could be anything, but by convention, it should start with a number like 01- and end with .yaml . The number sets the priority if you have more than one configuration file.

I’ll create a file named 01-network-manager-all.yaml .

Let’s add these lines to the file. We’ll build the file step by step.

image-17

How to Set a Static IP Using the GUI

It is very easy to set a static IP through the Ubuntu GUI/ Desktop. Here are the steps:

  • Search for settings .
  • Click on either Network or Wi-Fi tab, depending on the interface you would like to modify.
  • To open the interface settings, click on the gear icon next to the interface name.
  • Select “Manual” in the IPV4 tab and enter your static IP address, Netmask and Gateway.
  • Click on the Apply button.
Читайте также:  Linux with nvidia driver

image-16

  • Verify by using the command ip a

image-18

Conclusion

In this article, we covered two methods to set the static IP in Ubuntu. I hope you found the article useful.

What’s your favorite thing you learned from this tutorial? Let me know on Twitter!

You can read my other posts here.

Источник

How to Set Static IP Address and Configure Network in Linux

If you are a Linux system administrator, time will come when you will need to configure networking on your system. Unlike desktop machines where you can use dynamic IP addresses, on a server infrastructure, you will need to setup a static IP address (at least in most cases).

This article is meant to show you how to configure static IP address on most frequently used Linux distributions.

For the purpose of this tutorial, we will use the following Internet Protocol version 4 (IPv4) details:

IP address: 192.168.0.100 Netmask: 255.255.255.0 Hostname: node01.tecmint.com Domain name: tecmint.com Gateway: 192.168.0.1 DNS Server 1: 8.8.8.8 DNS Server 2: 4.4.4.4

Configure Static IP Address in RHEL/CentOS/Fedora:

To configure static IP address in RHEL / CentOS / Fedora, you will need to edit:

/etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0

Where in the above «ifcfg-eth0″ answers to your network interface eth0 . If your interface is named “ eth1» then the file that you will need to edit is «ifcfg-eth1» .

Let’s start with the first file:

NETWORKING=yes HOSTNAME=node01.tecmint.com GATEWAY=192.168.0.1 NETWORKING_IPV6=no IPV6INIT=no
# vi /etc/sysconfig/network-scripts/ifcfg-eth0

Note: Make sure to open the file corresponding to your network interface. You can find your network interface name with ifconfig -a command.

In that file make the following changes:

DEVICE="eth0" BOOTPROTO="static" DNS1="8.8.8.8" DNS2="4.4.4.4" GATEWAY="192.168.0.1" HOSTNAME="node01.tecmint.com" HWADDR="00:19:99:A4:46:AB" IPADDR="192.68.0.100" NETMASK="255.255.255.0" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" UUID="8105c095-799b-4f5a-a445-c6d7c3681f07"

You will only need to edit the settings for:

Other settings should have already been predefined.

Next edit resolve.conf file by opening it with a text editor such as nano or vi:

nameserver 8.8.8.8 # Replace with your nameserver ip nameserver 4.4.4.4 # Replace with your nameserver ip

Once you have made your changes restart the networking with:

# /etc/init.d/network restart [On SysVinit] # systemctl restart network [On SystemD]

Set Static IP Address in Debian / Ubuntu

To setup static IP address in Debian/ Ubuntu, open the following file:

# nano /etc/network/interfaces

You may see a line looking like this:

auto eth0 iface eth0 inet dhcp

Change it so it looks like this:

auto eth0 iface eth0 inet static address 192.168.0.100 netmask 255.255.255.0 gateway 192.168.0.1 dns-nameservers 4.4.4.4 dns-nameservers 8.8.8.8

Save the file and then edit /etc/resolv.conf like this:

nameserver 8.8.8.8 # Replace with your nameserver ip nameserver 4.4.4.4 # Replace with your nameserver ip

Restart the networking on your system with:

# /etc/init.d/network restart [On SysVinit] # systemctl restart network [On SystemD]

Your static IP address has been configured.

Conclusion:

You now know how to configure a static IP address on a Linux distro. If you have any questions or comments, please do not hesitate to submit them in the comment section below.

Источник

Оцените статью
Adblock
detector