search
HomeOperation and MaintenanceCentOSHow to modify IP address in centos

How to modify IP address in centos

Apr 14, 2025 pm 08:27 PM
centosshell scriptsubnet

Modifying the CentOS IP address is divided into three steps: modify the IPADDR, NETMASK, and GATEWAY parameters in the /etc/sysconfig/network-scripts/ifcfg-eth0 file. Save the file and restart the network service (systemctl restart network). Optional: Write scripts to automate the IP address modification process.

How to modify IP address in centos

CentOS modify IP address: Don't be scared by those complicated tutorials!

Many novices get overwhelmed when they see modifying CentOS IP addresses, and they are confused by various tutorials. Actually, it's not that complicated! In this article, I will tell you how to easily get it done in a way that you can definitely understand. After reading this article, you can not only modify the IP, but also understand the principles behind it, and even write a script yourself to automate the process.

First of all, you have to understand that the network configuration of CentOS mainly relies on files in /etc/sysconfig/network-scripts/ directory, especially ifcfg-eth0 (or ifcfg-ens33 , depending on your network card name, you can view it with the ip addr command). This file is like a network-configured "ID card", which records all the information of your network card, including the IP address, subnet mask, gateway, etc.

Basic knowledge review: Don't be afraid, this part is very simple

You only need to know, in the ifcfg-eth0 file, several key parameters:

  • DEVICE : Your network card name, such as eth0 or ens33 .
  • BOOTPROTO : Start the protocol, generally set to static to represent static IP, and dhcp means dynamically obtaining IP. We modify the IP here, of course we need to set it to static .
  • IPADDR : Your IP address, such as 192.168.1.100 .
  • NETMASK : Subnet mask, such as 255.255.255.0 .
  • GATEWAY : Gateway address, such as 192.168.1.1 .

Core concept: Modify configuration files and restart network services

Modifying the IP address is actually to modify the three parameters of IPADDR , NETMASK , and GATEWAY in ifcfg-eth0 file. Don't be afraid, use a text editor (such as vi or nano ) to open this file and modify it.

For example, suppose we want to change the IP address to 192.168.1.101 , the subnet mask remains unchanged and the gateway remains unchanged, then the content of the modified ifcfg-eth0 file may look like this (note: adjust DEVICE according to your network card name):

 <code class="bash">DEVICE=eth0 BOOTPROTO=static IPADDR=192.168.1.101 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 ONBOOT=yes</code>

After modification, save the file. Then, restart the network service and let the modification take effect:

 <code class="bash">systemctl restart network</code>

or

 <code class="bash">service network restart</code>

Advanced usage: Automating with scripts

For situations where IP addresses are often modified, writing a script will be much more convenient. The following is a simple shell script that can modify the IP address according to the parameters:

 <code class="bash">#!/bin/bash IP=$1 NETMASK=$2 GATEWAY=$3 sed -i "s/IPADDR=.*/IPADDR=$IP/g" /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i "s/NETMASK=.*/NETMASK=$NETMASK/g" /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i "s/GATEWAY=.*/GATEWAY=$GATEWAY/g" /etc/sysconfig/network-scripts/ifcfg-eth0 systemctl restart network echo "IP address changed successfully!"</code>

Using this script, you can run it like this:

 <code class="bash">./my_ip_script.sh 192.168.1.102 255.255.255.0 192.168.1.1</code>

Common Errors and Debugging: Don't panic, take your time

The most common mistake is to forget to restart the network service after modifying the file. Also, the network card name is written incorrectly, resulting in invalid modification. If you still cannot connect to the network after modification, check your firewall settings to make sure that the network connection is not blocked. Use the ip addr command to check the status of the network card to see if the IP address is modified successfully.

Performance Optimization and Best Practices: Keeping Code Clean

The sed command is used in the script to replace it, which is relatively efficient. Remember to keep your configuration files neat and annotate your scripts clearly for future maintenance. Developing good programming habits will help you avoid many detours in the days ahead.

Remember, practice brings true knowledge! If you do more hands-on, you can become a CentOS network configuration expert! Don’t be afraid of making mistakes, making mistakes is part of learning. I wish you success!

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

Statement
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
CentOS's Purpose: Building Robust and Reliable ServersCentOS's Purpose: Building Robust and Reliable ServersMay 11, 2025 am 12:18 AM

CentOS is suitable for building powerful and reliable servers. Its advantages include: 1. Stability and reliability, support cycle up to 10 years; 2. Security, built-in SELinux and regular security patches; 3. Compatibility and ecosystem, highly compatible with RHEL, with a rich software warehouse; 4. Performance optimization, suitable for various hardware platforms and providing kernel tuning.

The Future of CentOS: Transitioning to New DistributionsThe Future of CentOS: Transitioning to New DistributionsMay 10, 2025 am 12:19 AM

CentOS will continue to evolve in the future, and users should choose alternative distributions. 1) Evaluate the requirements, choose such as RockyLinux or AlmaLinux, and focus on stability and support. 2) Develop a migration plan, use tools such as CentOS2Rocky, and pay attention to testing and verification. 3) Plan early, maintain contact with the open source community, and ensure a smooth transition.

CentOS: The Choice for Server EnvironmentsCentOS: The Choice for Server EnvironmentsMay 09, 2025 am 12:21 AM

CentOS is widely selected as a server operating system because it is stable, secure and free. 1.CentOS is based on RHEL, providing enterprise-level stability and a life cycle of up to 10 years. 2. It has rich software packages and strong community support. 3. Simple installation, use yum management software package, and intuitive configuration. 4. Improve server management efficiency through command line tools, regular backups and log management. 5. Optimize server performance by adjusting kernel and network parameters.

The Future of CentOS: What's Next?The Future of CentOS: What's Next?May 08, 2025 am 12:01 AM

CentOS will continue to develop through CentOSStream in the future. CentOSStream is no longer a direct clone of RHEL, but is part of RHEL development. Users can experience the new RHEL functions in advance and participate in development.

CentOS: From Development to Production EnvironmentsCentOS: From Development to Production EnvironmentsMay 07, 2025 am 12:08 AM

The transition from development to production in CentOS can be achieved through the following steps: 1. Ensure the consistent development and production environment, use the YUM package management system; 2. Use Git for version control; 3. Use Ansible and other tools to automatically deploy; 4. Use Docker for environmental isolation. Through these methods, CentOS provides powerful support from development to production, ensuring the stable operation of applications in different environments.

CentOS Stream: The Successor and its ImplicationsCentOS Stream: The Successor and its ImplicationsMay 06, 2025 am 12:02 AM

CentOSStream is a cutting-edge version of RHEL, providing an open platform for users to experience the new RHEL functions in advance. 1.CentOSStream is the upstream development and testing environment of RHEL, connecting RHEL and Fedora. 2. Through rolling releases, users can continuously receive updates, but they need to pay attention to stability. 3. The basic usage is similar to traditional CentOS and needs to be updated frequently; advanced usage can be used to develop new functions. 4. Frequently asked questions include package compatibility and configuration file changes, and requires debugging using dnf and diff. 5. Performance optimization suggestions include regular cleaning of the system, optimizing update policies and monitoring system performance.

CentOS: Examining the Reasons Behind the End of LifeCentOS: Examining the Reasons Behind the End of LifeMay 04, 2025 am 12:12 AM

The reason for the end of CentOS is RedHat's business strategy adjustment, community-business balance and market competition. Specifically manifested as: 1. RedHat accelerates the RHEL development cycle through CentOSStream and attracts more users to participate in the RHEL ecosystem. 2. RedHat needs to find a balance between supporting open source communities and promoting commercial products, and CentOSStream can better convert community contributions into RHEL improvements. 3. Faced with fierce competition in the Linux market, RedHat needs new strategies to maintain its leading position in the enterprise-level market.

The Reasons for CentOS's Shutdown: A Detailed AnalysisThe Reasons for CentOS's Shutdown: A Detailed AnalysisMay 03, 2025 am 12:05 AM

RedHat shut down CentOS8.x and launches CentOSStream because it hopes to provide a platform closer to the RHEL development cycle through the latter. 1. CentOSStream, as the upstream development platform of RHEL, adopts a rolling release mode. 2. This transformation aims to enable the community to get exposure to new RHEL features earlier and provide feedback to accelerate the RHEL development cycle. 3. Users need to adapt to changing systems and reevaluate system requirements and migration strategies.

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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor