This guide demonstrates how to assign an IP address to a remote Linux system using the nmcli
command. This is useful for various reasons, including network policy compliance, troubleshooting, network migration, and enhanced security.
Several scenarios necessitate remote IP address changes:
Using nmcli
via SSH provides a convenient solution.
Assigning an IP Address with nmcli
First, identify the remote system's network interface using:
ssh user@remote_ip 'nmcli con show'
Replace user
and remote_ip
with the appropriate credentials. The output will show the network interface name (e.g., Wired connection 1
).
Next, modify the IP address using:
ssh -t user@remote_ip "sudo nmcli con modify 'Interface Name' ipv4.address new_ip/subnet_mask"
Replace Interface Name
, new_ip
, and subnet_mask
with the correct values (e.g., "Wired connection 1"
, 192.168.1.50/24
). The -t
flag ensures proper password prompting. You'll be prompted for the remote user's password, and then for sudo
on the remote machine.
Verify the change with:
ssh user@remote_ip "ip addr show interface_name"
Replace interface_name
with the interface's name (e.g., ens18
).
Alternative (Insecure) Method:
While less secure, you can use echo
and piping to avoid repeated password entry:
echo 'password' | ssh -t user@remote_ip "sudo -S nmcli con modify 'Interface Name' ipv4.address new_ip/subnet_mask"
Caution: This method exposes your password and is strongly discouraged. Using SSH keys is the recommended secure approach.
Automated Script:
A Bash script, nmcli_remote_ip_changer.sh
, simplifies this process. It prompts for remote credentials, identifies interfaces, and applies the new IP configuration, including gateway and DNS settings. The script can be found at [GitHub Link - Replace with actual GitHub link if available]. Remember to make it executable (chmod x nmcli_remote_ip_changer.sh
) before running.
Conclusion:
nmcli
offers a streamlined method for managing remote Linux system IP addresses. While direct command execution is efficient, using a script like nmcli_remote_ip_changer.sh
enhances usability and security when coupled with SSH key-based authentication. Prioritize secure practices to protect your systems.
The above is the detailed content of How To Assign IP Address To Remote Linux Systems Via SSH. For more information, please follow other related articles on the PHP Chinese website!