Ubuntu Hostname Setting Options
1. View Current Hostname
Before making changes, verify the existing hostname using one of these commands:
hostname: Returns the current hostname (temporary or permanent).hostnamectl: Displays detailed system information, including the static hostname (persistent across reboots).
Example output:
$ hostnamectl
Static hostname: ubuntu-server
Icon name: computer-vm
Chassis: vm
Machine ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Boot ID: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
Virtualization: kvm
Operating System: Ubuntu 24.04.3 LTS
Kernel: Linux 6.8.0-44-generic
Architecture: x86-64
2. Temporary Hostname Change (Restart-Sensitive)
Use the hostname command to modify the hostname for the current session only. The change will revert to the original after a reboot.
Command:
sudo hostname new-hostname
Example:
sudo hostname dev-server
Verification: Run hostname again to confirm the change.
3. Permanent Hostname Change (Requires Reboot)
For a lasting modification, update system configuration files and (optionally) use hostnamectl for convenience.
Option A: Using hostnamectl (Recommended)
The hostnamectl command synchronizes the change across all relevant system files and activates it immediately.
Command:
sudo hostnamectl set-hostname new-hostname
Example:
sudo hostnamectl set-hostname web-server
Note: No reboot is strictly required, but restarting ensures all services recognize the new hostname.
Option B: Editing Configuration Files
Manually update the following files to guarantee persistence:
-
Step 1: Update
/etc/hostname
This file stores the static hostname. Replace the old name with the new one.sudo nano /etc/hostnameExample content after change:
web-server -
Step 2: Update
/etc/hosts
Modify the local hostname resolution to avoid conflicts. Locate the line with127.0.1.1(or127.0.0.1for newer Ubuntu versions) and replace the old hostname.sudo nano /etc/hostsExample modification:
127.0.0.1 localhost 127.0.1.1 web-server
Reboot Requirement: Restart the system to apply changes system-wide.
sudo reboot
4. Graphical Interface Method (Desktop Edition Only)
For Ubuntu Desktop users, a GUI simplifies the process:
Steps:
- Open Settings from the application menu.
- Navigate to System > About (or Device Name in older versions).
- In the Device Name (or Hostname) field, enter the new hostname.
- Click Apply. The system will automatically update configuration files and may prompt for a restart.
5. Hostname Rules & Best Practices
- Format Constraints:
- Length: 2–64 characters.
- Characters: Letters (a-z, A-Z), digits (0-9), hyphens (-), and periods (.).
- Cannot start or end with a hyphen or period.
- Avoid Conflicts: Ensure the hostname is unique within your network to prevent DNS or network issues.
- Service Impact: After changing the hostname, restart dependent services (e.g., Docker, Nginx) to avoid connection errors.
sudo systemctl restart docker
By following these methods, you can safely configure and manage your Ubuntu system’s hostname—whether for temporary testing or permanent deployment.
以上就是关于“Ubuntu hostname设置选项”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm