{"id":63043,"date":"2022-06-22T09:42:52","date_gmt":"2022-06-22T08:42:52","guid":{"rendered":"https:\/\/www.bleuanus.nl\/?p=63043"},"modified":"2022-06-22T09:43:55","modified_gmt":"2022-06-22T08:43:55","slug":"how-to-configure-static-ip-address-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/www.bleuanus.nl\/index.php\/2022\/06\/22\/how-to-configure-static-ip-address-on-ubuntu-20-04\/","title":{"rendered":"How to Configure Static IP Address on Ubuntu 20.04"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Typically, in most network configurations, the IP address is assigned dynamically by the router DHCP server. Setting a static IP address may be required in different situations, such as configuring port forwarding or running a&nbsp;<a href=\"https:\/\/linuxize.com\/post\/how-to-install-plex-media-server-on-ubuntu-18-04\/\">media server<\/a>&nbsp;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"configuring-static-ip-address-using-dhcp\">Configuring Static IP address using DHCP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The easiest and recommended way to assign a static IP address to a device on your LAN is to configure 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, each time the device requests an address from the DHCP server. This works by assigning a static IP to the device\u2019s unique MAC address.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The steps for configuring a DHCP reservation vary from router to router. Consult the vendor\u2019s documentation for more information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"netplan\">Netplan<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu 17.10 and later uses&nbsp;<a href=\"https:\/\/netplan.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Netplan<\/a>&nbsp;as the default network management tool. The previous&nbsp;<a href=\"https:\/\/linuxize.com\/post\/how-to-check-your-ubuntu-version\/\">Ubuntu versions<\/a>&nbsp;were using&nbsp;<a href=\"https:\/\/linuxize.com\/post\/ifconfig-command\/\"><code>ifconfig<\/code><\/a>&nbsp;and its configuration file&nbsp;<code>\/etc\/network\/interfaces<\/code>&nbsp;to configure the network.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Netplan configuration files are written in YAML syntax with a&nbsp;<code>.yaml<\/code>&nbsp;file extension. To configure a network interface with Netplan, you need to create a&nbsp;<a href=\"https:\/\/yaml.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">YAML<\/a>&nbsp;description for the interface, and Netplan will generate the required configuration files for the chosen renderer tool.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Netplan 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"configuring-static-ip-address-on-ubuntu-server\">Configuring Static IP address on Ubuntu Server<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">On Ubuntu 20.04, the system identifies network interfaces using \u2018predictable network interface names\u2019.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first step toward setting up a static IP address is identifying the name of the ethernet interface you want to configure. To do so, use the&nbsp;<a href=\"https:\/\/linuxize.com\/post\/linux-ip-command\/\"><code>ip link<\/code><\/a>&nbsp;command, as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip link<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The command prints a list of all the available network interfaces. In this example, the name of the interface is&nbsp;<code>ens3<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000\n    link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n2: ens3: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000\n    link\/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Netplan configuration files are stored in the&nbsp;<code>\/etc\/netplan<\/code>&nbsp;directory. You\u2019ll probably find one or more YAML files in this directory. The name of the file may differ from setup to setup. Usually, the file is named either&nbsp;<code>01-netcfg.yaml<\/code>,&nbsp;<code>50-cloud-init.yaml<\/code>, or&nbsp;<code>NN_interfaceName.yaml<\/code>, but in your system it may be different.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If your Ubuntu cloud instance is provisioned with cloud-init, you\u2019ll need to disable it. To do so create the following file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/cloud\/cloud.cfg.d\/99-disable-network-config.cfg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\/etc\/cloud\/cloud.cfg.d\/99-disable-network-config.cfg<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>network<\/strong>: {<strong>config<\/strong>: disabled}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Copy<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To assign a static IP address on the network interface, open the YAML configuration file with your&nbsp;<a href=\"https:\/\/linuxize.com\/post\/how-to-install-visual-studio-code-on-ubuntu-20-04\/\">text editor<\/a>&nbsp;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/netplan\/01-netcfg.yaml<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\/etc\/netplan\/01-netcfg.yaml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>network<\/strong>:\n  <strong>version<\/strong>: 2\n  <strong>renderer<\/strong>: networkd\n  <strong>ethernets<\/strong>:\n    <strong>ens3<\/strong>:\n      <strong>dhcp4<\/strong>: <strong>yes<\/strong>\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Copy<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before changing the configuration, let\u2019s explain the code in a short.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each Netplan Yaml file starts with the&nbsp;<code>network<\/code>&nbsp;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&nbsp;<code>ethernets<\/code>,&nbsp;<code>bonds<\/code>,&nbsp;<code>bridges<\/code>, or&nbsp;<code>vlans<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The configuration above also has a line that shows the&nbsp;<code>renderer<\/code>&nbsp;type. Out of the box, if you installed Ubuntu in server mode, the renderer is configured to use&nbsp;<code>networkd<\/code>&nbsp;as the back end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Under the device\u2019s type (<code>ethernets<\/code>), you can specify one or more network interfaces. In this example, we have only one interface&nbsp;<code>ens3<\/code>&nbsp;that is configured to obtain IP addressing from a DHCP server&nbsp;<code>dhcp4: yes<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To assign a static IP address to&nbsp;<code>ens3<\/code>&nbsp;interface, edit the file as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set DHCP to&nbsp;<code>dhcp4: no<\/code>.<\/li><li>Specify the static IP address. Under&nbsp;<code>addresses:<\/code>&nbsp;you can add one or more IPv4 or IPv6 IP addresses that will be assigned to the network interface.<\/li><li>Specify the gateway.<\/li><li>Under&nbsp;<code>nameservers<\/code>, set the IP addresses of the nameservers.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\/etc\/netplan\/01-netcfg.yaml<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>network<\/strong>:\n  <strong>version<\/strong>: 2\n  <strong>renderer<\/strong>: networkd\n  <strong>ethernets<\/strong>:\n    <strong>ens3<\/strong>:\n      <strong>dhcp4<\/strong>: <strong>no<\/strong>\n      <strong>addresses<\/strong>:\n        - 192.168.121.221\/24\n      <strong>gateway4<\/strong>: 192.168.121.1\n      <strong>nameservers<\/strong>:\n          <strong>addresses<\/strong>: &#91;8.8.8.8, 1.1.1.1]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Copy<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When editing Yaml files, make sure you follow the YAML code indent standards. If the syntax is not correct, the changes will not be applied.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once done, save the file and apply the changes by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo netplan apply<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Verify the changes by typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ip addr show dev ens3<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2: ens3: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state UP group default qlen 1000\n    link\/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff\n    inet 192.168.121.221\/24 brd 192.168.121.255 scope global dynamic ens3\n       valid_lft 3575sec preferred_lft 3575sec\n    inet6 fe80::5054:ff:feb0:f500\/64 scope link \n       valid_lft forever preferred_lft forever\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it! You have assigned a static IP to your Ubuntu server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Typically, in most network configurations, the IP address is assigned dynamically by the router DHCP server. Setting a static IP address may be required in different situations, such as configuring port forwarding or running a&nbsp;media server&nbsp;. Configuring Static IP address using DHCP The easiest and recommended way to assign a static IP address to a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10],"class_list":["post-63043","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-dhcp"],"_links":{"self":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts\/63043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/comments?post=63043"}],"version-history":[{"count":1,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts\/63043\/revisions"}],"predecessor-version":[{"id":63044,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/posts\/63043\/revisions\/63044"}],"wp:attachment":[{"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/media?parent=63043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/categories?post=63043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bleuanus.nl\/index.php\/wp-json\/wp\/v2\/tags?post=63043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}