How to make Ubuntu a fixed IP address on the command line (explained as carefully as possible)

Introduction

When playing with Raspberry Pi, there are many occasions when you want to fix the IP address. This time I will show you how to fix his IP address on Ubuntu only on the command line.

environment

Ubuntu 20.10

Add configuration file

Create a new file called /etc/netplan/99_config.yaml and write the following contents in it.

/etc/netplan/99_config.yaml


network:
  version: 2
  renderer: networkd
  ethernets:
    <NETWORK_INTERFACE_NAME>:
      addresses:
        - <STATIC_IP_ADDR_WITH_NETMASK>
      gateway4: <DEFAULT_GATEWAY>
      nameservers:
          addresses: [<DNS, DNS, ...>]

Replace each part surrounded by <> with the following environment.

variable Explanation Example
<NETWORK_INTERFACE_NAME> Network interface name eth0
<STATIC_IP_ADDR_WITH_NETMASK> Fixed IP address and netmask 192.168.3.2/24
<DEFAULT_GATEWAY> default gateway 192.168.3.1
<DNS, DNS, ...> DNS server(Separated by commas if there are multiple) 8.8.8.8, 8.8.4.4

Network interface name

You can find the network interface name with the following command.

$ ip a | grep -E '192\.168\.[0-9]*\.[0-9]*|172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]*\.[0-9]*|10\.[0-9]*\.[0-9]*\.[0-9]*' -B 10

Long grep uses regular expressions to specify all possible values ​​for IPv4 private addresses.

Then, it will be displayed as follows.

... (Ignore OK)
...
...
...
...
...
...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.14/24 brd 192.168.3.255 scope global dynamic eth0

Write the interface name (eth0 part in the above example) corresponding to the part where grep is caught (192.168.3.14 and 192.168.3.255 in the above example) in<NETWORK_INTERFACE_NAME>.

Fixed IP address and netmask

This just specifies the static IP address and netmask you want to specify for your Ubuntu device. However, it is annoying to have an IP address in an existing LAN, so you need to find out in advance that it is not.

On a Mac, you can check with the arp-scan command.

Operation on Mac


$ brew install arp-scan

Operation on Mac


$ sudo arp-scan -l --interface <NETWORK_INTERFACE_NAME>

Check <NETWORK_INTERFACE_NAME> as you did on Ubuntu. However, there is no ip command on the Mac, so use ifconfig instead.

Operation on Mac


$ ifconfig | grep -E '192\.168\.[0-9]*\.[0-9]*|172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]*\.[0-9]*|10\.[0-9]*\.[0-9]*\.[0-9]*' -B 10

If more than one appears, you can use either one.

Specify the IP address for Ubuntu that did not appear in the results of the arp-scan command.

Next is the netmask. First, check the netmask in the LAN. For Mac, this is the part that says "Subnet Mask" in "System Preferences"> "Network".

スクリーンショット 2020-12-24 17.33.46のコピー3.png

In the screenshot above it says 255.255.255.0. The binary notation for this is as follows.

11111111 11111111 11111111 00000000

The upper 24 bits are 1, so specify/24. In other words, when combined with the IP address, it will be written as 192.168.3.2/24. (192.168.3.2 is an example. Specify a unique IP address.)

As another example, if it was 255.255.0.0, for example, in binary notation

11111111 11111111 00000000 00000000

Therefore, it is OK if you specify / 16.

default gateway

As the name implies, it specifies the default gateway.

On a Mac, the default gateway is the part that says "Router" in "System Preferences"> "Network". It's OK if you set this to the same value on Ubuntu.

スクリーンショット 2020-12-24 17.33.46のコピー.png

DNS server

You can specify multiple DNS server IP addresses, separated by commas.

If you are not sure, you can specify 8.8.8.8, 8.8.4.4 for the time being. This is Google Public DNS and is available to everyone.

Completion example

The completed example looks like this.

/etc/netplan/99_config.yaml


network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.168.3.2/24
      gateway4: 192.168.3.1
      nameservers:
          addresses: [8.8.8.8, 8.8.4.4]

In the above example, it is fixed at 192.168.3.2.

Application

After saving the above file, execute the following command.

$ sudo netplan apply

You should now have a static IP address. Let's check.

$ ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.2/24 brd 192.168.3.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.3.14/24 brd 192.168.3.255 scope global secondary dynamic eth0
       valid_lft 83119sec preferred_lft 83119sec

I was able to confirm that 192.168.3.2 was set properly.

Supplement

You can see that it is fixed because the line just below the static IP address (192.168.3.2 in the above example) says valid_lft forever preferred_lft forever. It is a proof that forever is fixed.

If it is not fixed, the expiration date is displayed as valid_lft 83119sec preferred_lft 83119sec as shown in the line immediately below 192.168.3.14. This is not fixed as it changes when it expires or the DHCP server is reset.

Even if you specify a fixed IP address, 192.168.3.14 (the non-fixed IP address) remains as in the above example. It seems that the old IP address is not deleted immediately.

Also, this time I operated from the command line, but if you are using Ubuntu Desktop and set the GUI to have a fixed IP address, the command line setting (setting in this article) has priority. Will be done. That said, it's confusing and we don't recommend having separate settings on both the GUI and the command line.

Reference site

-Set fixed IP address in Ubuntu via command

Recommended Posts

How to make Ubuntu a fixed IP address on the command line (explained as carefully as possible)
How to run a GIF file from the Linux command line (Ubuntu)
How to run a Kotlin Coroutine sample from the command line
How to change the timezone on Ubuntu
Using JUnit from the command line on Ubuntu
How to build a Pytorch environment on Ubuntu
How to run the SpringBoot app as a service
How to make JavaScript work on a specific page
Notes on updating Ubuntu from the command line (16.04 → 18.04, 18.04 → 20.04 both)
How to make a mod for Slay the Spire
How to install GNOME as a desktop environment on CentOS 7
How to apply C code format from the command line
How to set the IP address and host name of CentOS8
To use the "java" command line tool ... How to avoid popping up
How to install and configure the monitoring tool "Graphite" on Ubuntu
[Rails] How to put a crown mark on the ranking function
How to identify the path that is easy to make a mistake
How to make a Java container
How to make a JDBC driver
How to download and run a Jar package directly from the Maven repository with just the command line
How to use Bio-Formats on Ubuntu 20.04
How to install WildFly on Ubuntu 18.04
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
How to build vim on Ubuntu 20.04
How to make a Java array
How to place geckodriver (Selenium WebDriver) on the path using Maven command
How to get the current date as a string in yyyyMMdd format
How to limit IP address only to a specific directory with laravel + nginx
How to make a Java calendar Summary
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to install production Metabase on Ubuntu
IP address and port restrictions to Ubuntu
How to make a Discord bot (Java)
[Ruby on Rails] How to make the link destination part of the specified id
How to find the distance and angle between two points on a plane
How to make a unique combination of data in the rails intermediate table
Make it possible to handle the camera with old notebook + Ubuntu 18.04 LTS (droidcam)
A memo of what I did from a blank state to searching for characters using the "grep command" on "Ubuntu"