SoFunction
Updated on 2025-03-03

Steps to set static IP address in Ubuntu (effective for personal testing)

If Ubuntu is installed at a minimum, without a graphical interface, and requires static IP, how should I operate it?

Netplan is the default network management tool for the latest version of Ubuntu. Netplan's configuration files are written in YAML with the extension .yaml.

Note: Spaces in the configuration file are part of the syntax, be careful. Without proper indentation, the file will not be read normally.

1. Find the netplan directory located in /etc/netplan

Enter/etc/netplanTable of contents.

cd /etc/netplan

If you don't see any files, you can create one. The file name can be any name, but as convention, it should be00-Such a number begins with .yaml. If there are multiple configuration files, the number will set priority.

2. Modify or create configuration files

I will create a name calledfile. Let's add these lines to the file and follow me to add configuration information step by step.

network:
 version: 2

The top-level node of the Netplan configuration file is a network: , which contains version:2 (indicates that using network definition version 2).

Next, we will add a renderer to control the entire network. By default, the renderer is systemd-networkd, but we set it to NetworkManager.

Now, our file looks like this

network:
 version: 2
 renderer: NetworkManager

3. Configure a network adapter that matches the unit

Next, we will addethernetsand useip aThe network adapter name is found, and the network adapter name set here iseth0, please set according to the actual situation, it may beens999What's the matter. Other supported device types includemodems:wifis:andbridges:

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:

4. Disable DHCP

Since we set up a static IP, we do not want to dynamically assign IP to this network adapter, so we willdhcp4Set to “no”。

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no

5. Configure static IP, subnet mask, and gateway

We will now specify the specific static IP mentioned in step 2 based on the subnet and the available IP range. It is 192.168.1.112, note the subnet mask 255.255.255.0, corresponding to 24.

Next, we want to specify the gateway, that is, the router or network device that assigns the IP address. My gateway is 192.168.1.1.

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no
     addresses: [192.168.1.112/24]
     gateway4: 192.168.1.1

6. Configure the DNS server address

Next, we will configure the DNS server. The first value here is119.29.29.29, I use Tencent's DNS server as my main DNS resolution service, the second value is223.5.5.5, it is Alibaba public DNS server. These values ​​can vary according to your requirements.

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no
     addresses: [192.168.1.112/24]
     gateway4: 192.168.1.1
     nameservers:
         addresses: [119.29.29.29,223.5.5.5]

7. Apply and test changes

Before we apply the changes permanently, we can use this command to test the changes:

sudo netplan try

If there are no errors, it will ask you if you want to apply these settings. Confirm that there is no problem. Press Enter on the confirmation interface to confirm the modification. Otherwise, the changes will be skipped after the timeout to maintain the status quo.

8. Check whether the static IP is effective

Finally, useip aThe command checks the results of the change and you will find that the static IP has been applied.

The above is the detailed content of the method and steps for setting static IP addresses in Ubuntu (tested effectively). For more information about setting static IP addresses in Ubuntu, please pay attention to my other related articles!