簡體   English   中英

在 Ubuntu 中永久更改主機名

[英]Changing a hostname permanently in Ubuntu

我想創建一個可以永久更改我的 Ubuntu 主機名的 shell 腳本。 每當我使用hostname New_hostname命令時,它會在我重新啟動機器后返回到原始主機名。

我發現我可以永久更改它的唯一方法是修改/etc/hostname的文件並保存它。 有什么方法可以只使用 shell 腳本來做到這一點嗎? 我也有密碼。

hostnamectl結合了通過hostname命令設置主機hostname和編輯/etc/hostname 不幸的是,編輯/etc/hosts仍然需要單獨完成。

hostnamectl set-hostname <new-hostname>

類型

echo "myNewHostName" > /etc/hostname

在您附近具有 root 訪問權限的任何 shell 中..

您可能還想查看文件/etc/hosts ,參見。 http://pricklytech.wordpress.com/2013/04/24/ubuntu-change-hostname-permanently-using-the-command-line/

在 Ubuntu 18.04 LTS 中

在 Ubuntu 18.04 中重新啟動后,通過 SSH 更改的主機名將恢復。 按照以下方式進行永久更改。

1.編輯/etc/cloud/cloud.cfg

sudo nano /etc/cloud/cloud.cfg

preserve_hostname設置為true

保留主機名:真

2.運行hostnamectl

hostnamectl set-hostname new-host-name

3. 重啟

sudo reboot

如何在不重啟系統的情況下更改Ubuntu服務器主機名?

輸入以下命令:

接下來編輯/ etc / hostname文件並更新主機名:

$ sudo nano /etc/hostname

最后,編輯/ etc / hosts文件並更新讀取舊主機名的行:

$ sudo nano /etc/hosts

從:

127.0.1.1 newhost.example.com

至:

127.0.1.1 myhost.example.com

保存並關閉文件。

Ubuntu Linux使用hostnamectl更改主機名

$ hostnamectl

樣本輸出:

   Static hostname: ip-10-x-xx-xxx.us-west-2.compute.internal
Transient hostname: newhost.example.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ec23eca3540747b9c3648dc0fNXXXXXX
           Boot ID: 603fff5ef0b040bbaXXXXXXXXe87bf5e
    Virtualization: kvm
  Operating System: Amazon Linux 2
       CPE OS Name: cpe:2.3:o:amazon:amazon_linux:2
            Kernel: Linux 4.14.133-113.112.amzn2.x86_64
      Architecture: x86-64

設置或更改您的計算機名稱

$ hostnamectl set-hostname 'myhost.example.com'

樣本輸出:

   Static hostname: ip-10-x-xx-xxx.us-west-2.compute.internal
Transient hostname: myhost.example.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ec23eca3540747b9c3648dc0fNXXXXXX
           Boot ID: 603fff5ef0b040bbaXXXXXXXXe87bf5e
    Virtualization: kvm
  Operating System: Amazon Linux 2
       CPE OS Name: cpe:2.3:o:amazon:amazon_linux:2
            Kernel: Linux 4.14.133-113.112.amzn2.x86_64
      Architecture: x86-64
ec2-user@host.example.com:~$ hostname
host.example.com
ec2-user@host.example.com:~$ sudo hostname myhost.example.com
ec2-user@host.example.com:~$ hostname
myhost.example.com

無需重啟即可永久更改主機名

/etc/hosts

127.0.0.1    persistent_host_name

/etc/主機名

persistent_host_name

立即應用更改

$ sudo hostname persistent_host_name

檢查更改

$ hostname
persistent_host_name

通常,您需要在這些文件中更改它:

/etc/hostname
/etc/hosts

如果您使用的是一些高級打印機,也可以在這里:

/etc/printcap

這就是為什么我建議手動執行此操作 - 但首先搜索舊主機名。 要在 /etc 中查找所有出現的內容:

sudo grep -iRI "_OLDHOSTNAME_" /etc 2>/dev/null

然后在每次出現時更改 _OLDHOSTNAME_。 完畢。

在 ubuntu 機器中更改主機名 permanet

去 :

 #vim /etc/hostname

在要為機器設置的文件中鍵入主機名

然后保存和文件

保存文檔后運行此命令

 # hostname -F /etc/hostname

然后編輯/etc/hosts文件

 #vim /etc/hosts

在文件中輸入ip主機名

然后注銷機器並重新登錄機器

如果您只想更改主機名,因為它在終端中顯示為命令提示符。 然后你可以用~/.bashrc “desired_host_name”替換PS1 \\h

就像在~/.bashrc把這一行放在文件末尾:

export PS2="continue-> ";
export PS1="\u@3050:~$ ";

在 Ubuntu 18.04 上更改主機名

定義

主機名是標識網絡上機器的標簽。 您不應在同一網絡上的兩台不同機器上使用相同的主機名。

先決條件

  • 用戶應該有 sudo 權限

更改主機名

使用 hostnamectl 命令更改主機名。 如果要將主機名更改為 new_hostname

sudo hostnamectl set-hostname new_hostname

它不會直接更改主機名。 您想永久保留更改,則必須編輯cloud.cfg文件

sudo nano /etc/cloud/cloud.cfg

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

保存文件並關閉編輯器。

驗證您的更改

您可以使用命令hostnamectl驗證您的更改,它將在靜態主機名下顯示new_hostname

PS:源碼鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM