search
HomeSystem TutorialLINUXShare several methods to change IP address in Linux system

Share several methods to change IP address in Linux system

Feb 09, 2024 pm 08:42 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

Currently, IP addresses in most systems are automatically assigned, but in some cases, we may need to change them manually. Today we share several methods to modify the system IP address.

As a system administrator, changing the IP address of a machine is a very common task. IP addresses in most systems are currently assigned automatically, but in some cases we may need to change them manually.

Today we share several methods to modify the system IP address.

Before taking action, we can use the following command to view the current IP address:

ip a

The above command will also display the network port (interface) name while displaying the IP address. The network port name will be used when modifying the IP address.

Use the ip command to set the IP address

We introduced the ip command in a previous article, which can view the IP address of the local machine. This command is available on most Linux distributions. Use the ip command to set the IP address. You can use the following command:

ip addr add [ip_address] dev [interface]

For example, to add an IP address to the network port eth1, you can use the following command:

sudo ip addr add 192.168.56.21/24 dev eth1

Now, there are two IP addresses in the network port eth1, one is originally configured, and the other is newly added using the command:

Linux 系统中更改 IP 地址的几种方法分享

As shown in the picture above, if you delete the old IP address, there will be only one IP address left.

Set static IP address

The IP address set through the above method will be modified after the system restarts, and the IP address is dynamic.

If you are using an older version of Ubuntu (version number less than 17.10), you can change the IP to static and permanent by editing the file /etc/network/interfaces.

sudo nano /etc/network/interfaces

If the file content is as follows, it means that the IP address of the system is set by the DHCP client:

auto eth0iface eth0 inet dhcp

We can set a static IP address by modifying the above file. For example, if you want to set the IP address to 192.168.56.20, you can modify the content of the above file to:

auto enp0s3iface enp0s3 inet staticaddress 192.168.56.20netmask 255.255.255.0gateway
 192.168.40.31

The above content is easy to understand for users who often use Linux systems. For the modification to take effect, you need to run the following command:

$ sudo systemctl restart networking.service

Tip: On RedHat-based systems, the file used to configure the network interface is /etc/sysconfig/networking-scripts/ifcfg-*

Use Netplan for network configuration (for Ubuntu)

Ubuntu has a tool for network configuration called Netplan.

We configure the IP address on Ubuntu 20.04 as an example, using NetworkManager as the renderer for the network configuration.

You can view the current IP address in the Netplan configuration file. The file is in YAML format. If it does not exist, you can create it:

sudo nano /etc/netplan/config.yaml

If the IP address is dynamic, you will see the dhcp4 parameter set to true.

If you have currently configured a static IP, you will see the following content:

---network: version: 2 renderer: networkd ethernets: eth1: addresses: 
- 192.168.56.66/24 nameservers: addresses: - 8.8.8.8 routes: - to: default via: 10.0.2.2

Set a new address by modifying the IP address above. In addition, you can also keep the old IP address and add another one to the above network port (eth1).

After configuring, test it before applying:

sudo netplan try

After confirming the changes, make the configuration take effect:

sudo netplan apply

Then check whether the configuration has taken effect:

Linux 系统中更改 IP 地址的几种方法分享

One of the advantages of using Netplan is that the configuration modification is permanent. After the machine is restarted, the configuration will still take effect, that is, the configured IP is static .

Change the system IP address using the graphical interface (for desktop users)

Using a graphical interface to manage IP addresses is the easiest way. On Ubuntu systems, the network settings contain all the necessary configurations.

Open Settings in the GNOME Dashboard, find the Network option, and then select the settings icon in the available network of the system, as shown in the following figure:

Linux 系统中更改 IP 地址的几种方法分享

In the newly opened window, select the IPv4 tab, then under IPv4 method, select the Manual (manual) option and enter the new IP address:

Linux 系统中更改 IP 地址的几种方法分享

Then restart the network connection and check the IP address:

Linux 系统中更改 IP 地址的几种方法分享

As you can see from the screenshot above, the IP address has been changed.

The ip command is available on all Linux systems; Netplan is a new way to manage your network and is a very simple method for Ubuntu systems.

The above is the detailed content of Share several methods to change IP address in Linux system. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?Apr 25, 2025 am 09:10 AM

For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

What are the differences in how Linux and Windows handle device drivers?What are the differences in how Linux and Windows handle device drivers?Apr 25, 2025 am 12:13 AM

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

Compare and contrast the security models of Linux and Windows.Compare and contrast the security models of Linux and Windows.Apr 24, 2025 am 12:03 AM

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.