DEV Community

Cover image for 更改IP地址指令
coolboywcy
coolboywcy

Posted on

更改IP地址指令

要更改CentOS 7的IP地址,可以按照以下步骤进行操作:

  1. 打开终端或通过SSH连接到CentOS 7服务器。

  2. 使用root权限或具有sudo权限的用户执行以下命令来编辑网络配置文件:

   sudo vi /etc/sysconfig/network-scripts/ifcfg-<interface_name>
Enter fullscreen mode Exit fullscreen mode

其中 <interface_name> 是你要更改IP地址的网络接口的名称,比如 eth0

  1. 在打开的文件中,找到并修改以下行来更改IP地址:
   BOOTPROTO=static
   IPADDR=<new_ip_address>
   NETMASK=<new_netmask>
   GATEWAY=<new_gateway>
Enter fullscreen mode Exit fullscreen mode

<new_ip_address> 替换为新的IP地址, <new_netmask> 替换为新的子网掩码, <new_gateway> 替换为新的网关地址。

  1. 保存文件并关闭编辑器。

  2. 重启网络服务以使更改生效:

   sudo systemctl restart network
Enter fullscreen mode Exit fullscreen mode
  1. 验证新的IP地址是否生效,可以使用以下命令之一:
   ip addr show <interface_name>
Enter fullscreen mode Exit fullscreen mode

或者

   ifconfig <interface_name>
Enter fullscreen mode Exit fullscreen mode

其中 <interface_name> 是你更改的网络接口的名称。

请注意,更改IP地址可能会导致网络连接中断,因此请确保你在进行更改之前有备份并可以通过其他方式访问服务器,以防万一出现问题。

Top comments (0)