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.
Ubuntu 20.10
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 |
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>
.
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".
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
.
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.
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.
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
.
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.
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.
-Set fixed IP address in Ubuntu via command
Recommended Posts