Home > Common Problem > body text

How to change ip address in linux

zbt
Release: 2023-10-07 11:03:36
Original
9556 people have browsed it

Linux is an open source operating system widely used in various devices and servers. Changing the IP address is one of the common tasks for network configuration in Linux systems. This article will introduce several methods on how to change the IP address in Linux system.

Method 1: Use the ifconfig command

ifconfig is the command used to configure the network interface in the Linux system. To change the IP address, you can use the following steps:

1. Open a terminal window and log in as the root user.

2. Enter the following command to view the configuration information of the current network interface:

ifconfig
Copy after login
Copy after login

This will display the detailed information of the current network interface, including IP address, subnet mask, gateway, etc.

3. Determine the name of the network interface whose IP address you want to change. Typically, an Ethernet interface is named eth0 or enp0sX, where X is a number.

4. Enter the following command to change the IP address:

ifconfig netmask
Copy after login

will be replaced with the name of the network interface to change the IP address, will be replaced with the new IP address, will be replaced with the new subnet mask.

For example, to change the IP address of the eth0 interface to 192.168.0.10 and the subnet mask to 255.255.255.0, you can enter the following command:

ifconfig eth0 192.168.0.10 netmask 255.255.255.0

5. Enter the following command to verify whether the new IP address is effective:

ifconfig
Copy after login
Copy after login

This will display the updated network interface configuration information.

Method 2: Use the ip command

The ip command is a more powerful and modern alternative for configuring network interfaces in Linux systems. To change the IP address, you can use the following steps:

1. Open a terminal window and log in as the root user.

2. Enter the following command to view the configuration information of the current network interface:

ip addr show
Copy after login
Copy after login

This will display the detailed information of the current network interface, including IP address, subnet mask, gateway, etc.

3. Determine the name of the network interface whose IP address you want to change. Typically, an Ethernet interface is named eth0 or enp0sX, where X is a number.

4. Enter the following command to change the IP address:

ip addr add / dev
Copy after login

will be replaced with the new IP address, will be replaced with the new subnet mask, will be replaced with the network to change the IP address Interface name.

For example, to change the IP address of the eth0 interface to 192.168.0.10 and the subnet mask to 24, you can enter the following command:

ip addr add 192.168.0.10/24 dev eth0
Copy after login

5. Enter the following command to verify the new IP Whether the address is valid:

ip addr show
Copy after login
Copy after login

This will display the updated network interface configuration information.

Method 3: Modify the configuration file

Another way to change the IP address is to directly modify the network configuration file. In most Linux distributions, network configuration files are located in the /etc/network/interfaces or /etc/sysconfig/network-scripts directories. To change the IP address, you can follow the steps below:

1. Open a terminal window and log in as the root user.

2. Use a text editor to open the network configuration file. For example, in Ubuntu, you can open the /etc/network/interfaces file with the following command:

sudo nano /etc/network/interfaces
Copy after login

3. Locate the network interface configuration section where you want to change the IP address. Typically, it will start with "iface" followed by the interface name (such as eth0).

4. In the interface configuration section, find the "address" and "netmask" lines and change their values ​​to the new IP address and subnet mask.

For example, to change the IP address of the eth0 interface to 192.168.0.10 and the subnet mask to 255.255.255.0, you can add or modify the following line to:

address 192.168.0.10

netmask 255.255.255.0

5. Save the file and close the text editor.

6. Enter the following command to restart the network service:

sudo service networking restart
Copy after login

Alternatively, you can use the following command to restart the network interface:

sudo ifdown && sudo ifup
Copy after login

Replace with the Network interface name.

The above are several methods to change the IP address in Linux system. Choose the appropriate method according to the actual situation, and make sure to back up important network configuration files before changing the IP address.

The above is the detailed content of How to change ip address in linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!