ARTICLE - Cloud Servers & Compute

Set a Static IP and DNS on CentOS 7

Before changing a remote host's address, confirm the network plan, active connection profile and out-of-band console. On NetworkManager-managed CentOS 7, use nmcli for persistent changes and verify routes and DNS before relying on the new connection.

2023-04-03 cnblogs.com (CQZHUOMI) 55 views
Advertisement

Changing an IP address, gateway or DNS remotely can disconnect SSH immediately. First arrange access through a cloud console, VM console or physical out-of-band management, record the current connection settings, and obtain the new address, prefix, gateway and DNS from the network administrator. For a CentOS 7 interface managed by NetworkManager, use nmcli to change the connection profile. Do not apply example values to a production network.

Static IP change and recovery timeline Console ready Record old values Edit profile Reactivate Verify or restore SSH may drop; restore the original settings from an out-of-band console Sequence only; the local network defines the address, prefix, gateway and DNS
Figure 1: Static IP change and recovery timeline. Scope: operational sequence; local administrators or the cloud platform assign network values.

CentOS Linux 7 reached end of life on 2024-06-30, according to the CentOS project lifecycle page. This article focuses on CentOS 7 connection profiles managed by NetworkManager. Newer distributions may also use NetworkManager, but configuration backends and service-management details vary by release.

Identify the interface, profile and address plan

A device name such as ens33 is not necessarily the NetworkManager connection name, which might be System ens33. List active connections and devices, then replace Wired connection 1 in the commands with the actual profile name. Do not guess the profile or edit an eth0 file by assumption.

nmcli connection show --active
nmcli device status
ip -4 address
ip route

The output should identify the active profile, device, current IPv4 address and default route. Record these values before changing anything. Confirm that the proposed address is unused, the prefix and gateway belong to the intended subnet, and a console session remains available.

Set a static IPv4 address and DNS with nmcli

The addresses below are documentation examples and must be replaced with values assigned by the network administrator or cloud console. Inspect the profile first, then modify the NetworkManager connection. Activating it may disconnect the current SSH session. See the RHEL 7 NetworkManager guide and nmcli reference for connection properties and activation commands.

PROFILE="Wired connection 1"
IFACE="ens33"

nmcli connection modify "$PROFILE" \
  ipv4.method manual \
  ipv4.addresses 192.0.2.40/24 \
  ipv4.gateway 192.0.2.1 \
  ipv4.dns "192.0.2.53" \
  connection.autoconnect yes

nmcli connection up "$PROFILE" ifname "$IFACE"

On success, nmcli reports that the connection was activated. This confirms that the profile was applied, not that the gateway, Internet or DNS is reachable. Before running connection up over SSH, confirm that console recovery is available.

Verify the address, route and name resolution

Check the kernel address, default route, saved connection properties and system resolver. Replace ens33 and the test domain with the actual device and a relevant name. If ICMP is blocked in the environment, a missing ping reply alone does not prove that the gateway is down.

PROFILE="Wired connection 1"
IFACE="ens33"

ip -4 address show dev "$IFACE"
ip route
nmcli -f ipv4.method,ipv4.addresses,ipv4.gateway,ipv4.dns connection show "$PROFILE"
getent ahostsv4 example.com

Check that the address and prefix match the plan, the default route uses the intended gateway, the profile remains manual, and getent returns an address. If the address is correct but there is no route, recheck the gateway and subnet. If IP connectivity works but names do not resolve, check ipv4.dns and the upstream resolver. NetworkManager may generate /etc/resolv.conf; editing that file directly is not a persistent configuration method.

Recover from a failed activation at the console

If SSH drops after reactivation, log in through the cloud console, VM console or out-of-band management arranged beforehand. Restore the address method, address, gateway and DNS recorded before the change, then reactivate the original profile.

If the original profile used DHCP and had no extra static routes or special DNS policy, restore automatic IPv4 configuration from the console:

PROFILE="Wired connection 1"
IFACE="ens33"

nmcli connection modify "$PROFILE" \
  ipv4.method auto \
  ipv4.addresses "" \
  ipv4.gateway "" \
  ipv4.dns ""
nmcli connection up "$PROFILE" ifname "$IFACE"

After recovery, check nmcli connection show --active, ip -4 address and ip route. If the original profile used a static address, restore its recorded values instead of using the DHCP example. A cloud instance's public IP is generally assigned or mapped by the provider control plane; changing the guest's private interface address does not automatically change it.

Limitations

  • This covers IPv4 profiles managed by NetworkManager on CentOS 7. If the interface is managed by the legacy network service or another network manager, do not mix configuration methods; identify the active manager first.
  • CentOS Linux 7 is end of life and is not a suitable default for new production deployments.
  • A cloud instance's private address, routes and security rules are constrained by its platform network. Public IPs and elastic IPs must be assigned or bound in the cloud control plane, not obtained solely by editing the guest interface.
  • Dual NICs, bonding, VLANs, policy routing, IPv6 and unattended remote recovery need separate route and rollback planning.

FAQ

How should I choose between a static address and a DHCP reservation?

Use a DHCP reservation when the network centrally manages address allocation; it reduces conflicts with the address pool. Choose a static host configuration only when the machine needs an address outside DHCP allocation. In either case, verify that the address is unused and that the gateway, prefix and DNS match the subnet. A cloud private address must also fit the VPC subnet plan.

What prerequisites and permissions are needed before a remote IP change?

Arrange console or out-of-band access and record the old connection settings first. Confirm the interface, active profile, target IPv4 address, prefix, gateway and DNS with the network administrator or cloud console. Running nmcli connection up can interrupt SSH. The console must remain usable to restore the previous profile if the new address or route fails.

Will changing a cloud server's static IP change its public address?

Usually not. The guest OS configures the private IPv4 address visible on its interface, while the cloud control plane assigns or maps a public or elastic IP. Check the private address, subnet and routes in the provider console. To change a public address, use the platform workflow and review service bindings and access rules.

How can I recover if SSH drops after activating nmcli?

Use the console or out-of-band access arranged beforehand; do not rely on the disconnected SSH session. Restore the original IPv4 method, address, gateway and DNS from your notes, then reactivate the profile. If the original used DHCP, restore ipv4.method auto; verify ip route shows the expected default route before reconnecting.

Related Providers

  • Alibaba Cloud ECS: for cloud hosts where the private address, VPC subnet and recovery console need to be checked together.
  • Tencent Cloud: for instances managed through a console where out-of-band access should be confirmed before a network change.

For more host fundamentals, see the cloud servers category and Linux server basics.

Community & Resources

Original source: cnblogs.com (CQZHUOMI) ↗