Cách đổi hostname trên Linux
Có hai cách đó là dùng lệnh hostnamectl hoặc hostname
Trước hết, kiểm tra hostname bằng lệnh hostname hoặc hostnamectl
nguye@ubuntuserver ~> hostname
ubuntuserver
nguye@ubuntuserver ~> hostnamectl
Static hostname: ubuntuserver
Icon name: computer-vm
Chassis: vm
Machine ID: 357a9dfc1b6d4093ab6332a64287c078
Boot ID: d94544ee41f742a8b62928f535a0fad0
Virtualization: vmware
Operating System: Ubuntu 22.04.2 LTS
Kernel: Linux 5.15.0-72-generic
Architecture: x86-64
Hardware Vendor: VMware, Inc.
Hardware Model: VMware Virtual Platform
Dùng lệnh hostnamectl
- Dùng lệnh
hostnamectlđể thay đồi hostname, ở ví dụ này, mình sẽ đổi thànhubuntu-shuffle. Nhớ phải chạy bằngsudo
sudo hostnamectl set-hostname ubuntu-shuffle
- Đổi hostname trong file
/etc/hosts. Ví dụ, dùngvimđể thay đổi:
Trước khi thay đổi
127.0.0.1 localhost
127.0.1.1 ubuntu_server
Sau khi thay đổi
127.0.0.1 localhost
127.0.1.1 ubuntu-shuffle
Dùng lệnh hostname
- Mở file
/etc/hostnamevà thay đổi thành hostname bạn muốn, ví dụubuntu-shuffle. Dùngvimđể thay đổi file. Nhớ là cósudo.
Thay đổi nội dung của file thành tên mà bạn muốn đổi.
- Đổi hostname trong file
/etc/hosts. Ví dụ, dùngvimđể thay đổi:
Trước khi thay đổi
127.0.0.1 localhost
127.0.1.1 ubuntu_server
Sau khi thay đổi
127.0.0.1 localhost
127.0.1.1 ubuntu-shuffle
- Cho chắc thì chạy command này.
hostname linuxconfig