Set up a static IP address for Ubuntu 20.04

Introduction: When we do embedded or other projects, sometimes we can't help but find that the ip address of Ubuntu often changes. At this time, we need to manually configure the static IP.

Setting up a static IP address for Ubuntu has the following advantages:

  1. Persistence: The static IP address is fixed and bound to the MAC address of the device. This means that every time the system is restarted, the device will be assigned the same IP address, ensuring the stability and persistence of network devices.

  2. Network identification and management: Static IP addresses make devices easier to identify and manage in the network. Administrators can know exactly the IP address of each device and perform network configuration and management accordingly.

  3. Stability of services and applications: For servers running services or applications, static IP addresses ensure that services or applications are always accessible through fixed IP addresses. This is especially important for servers that need to communicate with other devices or services.

  4. Network Security: Static IP addresses allow you to more precisely configure network security policies and firewall rules. You can filter, monitor and control network traffic based on static IP addresses, thereby improving network security.

  5. Network Performance Optimization: In some cases, a static IP address can provide faster network connections and lower latency. Compared with dynamic IP addresses, static IP addresses reduce the interaction process of the DHCP protocol, which may improve network performance.

Despite these benefits of a static IP address, make sure you understand your network's configuration requirements and related network device setup before setting up a static IP address for Ubuntu. Also, make sure to choose a unique address for the static IP address that is not in use by other devices to avoid IP address conflicts.

When you need to set a static IP address on Ubuntu, you can follow the steps below:

Step 1: Open Terminal On the Ubuntu desktop, press the Ctrl + Alt + T key combination, or click the application icon in the upper left corner, then search for and open "Terminal".

07f79f3d097243c295b3d2e980c00a14.png

 

Step 2: View the network interface Enter the following command in the terminal to view the network interface and its configuration in the current system:

ifconfig

Identify the network interface name (usually eth0 or ensX) on which to set the static IP.

1ea0bf457d3c49abbd9326d90a67814a.png

 

Step 3: Edit the network configuration file Open the network configuration file with a text editor such as nano or vim. Enter the following command in Terminal to edit the file:

sudo vim /etc/netplan/01-network-manager-all.yaml 

Note: Depending on your Ubuntu version and network management tools, the path and name of the network configuration file may vary.

f1821da2075b44ef97de759f3422585c.png

 

Step 4: Configure a Static IP Address In the opened file, find the section related to the network interface you want to configure. Add the following under "addresses" (replace them with the appropriate IP address, gateway, and subnet mask), depending on your network needs:

For example, if you want to set the static IP to 192.168.232.131, the subnet mask to 255.255.255.0, and the gateway to 192.168.232.1, the configuration would look like this: (114.114.114.114 is for Telecom)

 

# Let NetworkManager manage all devices on this system
#network:
#  version: 2
#  renderer: NetworkManager

network:
  ethernets:
    ens33: # Configured network card name
      addresses: [192.168.232.131/24] # Configured static ip address and mask
      optional: true
      gateway4: 192.168.232.1 # Gateway address
      nameservers:
        addresses: [192.168.232.1, 114.114.114.114]
  version: 2
  renderer: NetworkManager

Step 5: Save Changes After completing the configuration, press the Ctrl + O key combination to save the changes, then press the Ctrl + X key combination to exit the editor.

Step 6: Apply the configuration Enter the following command in the terminal to apply the new network configuration:

sudo netplan apply

Step 7: Verify the setup Use the following command to verify that the setup of the static IP was successful:

ifconfig

You should be able to see the new IP address and related information for the network interface.

By following the steps above, you have successfully set up a static IP address on Ubuntu. Keep in mind that if your network environment has other specific requirements (such as DNS server settings), you may need to edit your network configuration files accordingly. If anything goes wrong, you can reopen the network configuration file to make adjustments or revert to a dynamic IP configuration.

 

Guess you like

Origin blog.csdn.net/weixin_53000184/article/details/130780912