muuuuuj
muuuuuj

Reputation: 77

mac terminal computer name is garbled

When I use terminal enter zsh, my computer name is garbled, n3-85-8 instead MacBook-Pro. Sometime so as the bash. Do anyone know why? And how to fix it.enter image description here

Upvotes: 2

Views: 758

Answers (2)

Eljay
Eljay

Reputation: 5321

Extending Grisha's excellent answer, there are several different host names. Often the same, but may vary to accommodate different naming constraints. The whatami function (below) can help you choose which one you want in your PS1 prompt.

Here's a Bash function to help assess the different names.

function whatami {
    local cn=$(scutil --get ComputerName)
    local lhn=$(scutil --get LocalHostName)
    local hn=$(scutil --get HostName)
    local nbn=$(/usr/libexec/PlistBuddy -c "Print :NetBIOSName" /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist)
    printf '\e[1mComputerName\e[0m: '"$cn"'\n'
    printf '\e[1mLocalHostName\e[0m (Bonjour): '"$lhn"'\n'
    printf '\e[1mHostName\e[0m: '"$hn"'\n'
    printf '\e[1mNetBIOSName\e[0m (SMB): '"$nbn"'\n'
    printf '\e[1mIP Address\e[0m: '
    for x in $(ifconfig -l); do ipconfig getifaddr $x; done
}

Upvotes: 0

Grisha Levit
Grisha Levit

Reputation: 8617

There are two effects happening here:

  • Bash only reads the hostname (as displayed in the prompt) once at shell startup, which means you only see the change when you start a new shell, not when your hostname changes.
  • macOS by default changes its own hostname based on the network configuration

You can configure your computer not to change its hostname (see for example this question). Or, you can configure bash to use the computer's persistent LocalHostName in the prompt. This value does not change when you connect to a different network.

You can edit your ~/.bashrc (or related file) to have a line like:

PS1=$(scutil --get LocalHostName)':\W \u\$'

Upvotes: 5

Related Questions