Welcome to reprint, but please quote the original source at the beginning or end.
preface
Those of you who have read my article may have noticed from my terminal screenshots that I am usingoh-my-zsh
, and theRPROMPT
Customization is performed
Date + time + hostname
Because the author has many computers + virtual machines + cloud hosts, it is particularly important for the author to display hostname in the command prompt of the terminal, which can facilitate the author to know which machine the current terminal is connected to at a glance.
Of course, the premise is that each machine must be pre-set hostname
Setting method
general
* Nix system (Linux & macOS & FreeBSD &…) The general method of setting hostname is
# hostname <NEW HOSTNAME>
Copy the code
Linux
Hostnamectl can be used if Systemd is installed
Here’s how it helps:
$ hostnamectl -h hostnamectl [OPTIONS...] COMMAND ... Query or change system hostname. -h --help Show this help --version Show package version --no-ask-password Do not prompt for password -H --host=[USER@]HOST Operate on remote host -M --machine=CONTAINER Operate on local container --transient Only set transient hostname --static Only set static hostname --pretty Only set pretty hostname Commands: status Show current hostname settings set-hostname NAME Set system hostname set-icon-name NAME Set icon name for host set-chassis NAME Set chassis type for host set-deployment NAME Set deployment environment for host set-location NAME Set location for hostCopy the code
So we set hostname to use:
# hostnamectl set-hostname <NEW HOSTNAME>
Copy the code
macOS
MacOS, you can also use Scutil
Take a look at the help:
$ scutil --help
usage: scutil
interactive access to the dynamic store.
or: scutil --prefs [preference-file]
interactive access to the [raw] stored preferences.
or: scutil [-W] -r nodename
or: scutil [-W] -r address
or: scutil [-W] -r local-address remote-address
check reachability of node, address, or address pair (-W to "watch").
or: scutil -w dynamic-store-key [ -t timeout ]
-w wait for presense of dynamic store key
-t time to wait for key
or: scutil --get pref
or: scutil --set pref [newval]
or: scutil --get filename path key
pref display (or set) the specified preference. Valid preferences
include:
ComputerName, LocalHostName, HostName
newval New preference value to be set. If not specified,
the new value will be read from standard input.
or: scutil --dns
show DNS configuration.
or: scutil --proxy
show "proxy" configuration.
or: scutil --nwi
show network information
or: scutil --nc
show VPN network configuration information. Use --nc help for full command list
or: scutil --allow-new-interfaces [off|on]
manage new interface creation with screen locked.
or: scutil --error err#
display a descriptive message for the given error code
Copy the code
We can see that scutil can set ComputerName/LocalHostName/HostName. What’s the difference between the three?
ComputerName
: Computer name, if usedTime Machine
If so, you can see it in its backup folderComputerName.backupbundle
Named file, which is stored in the corresponding computer’s historical backupLocalHostName
: LAN (Bonjour
) the host name declared internally and externally, for examplemachine007.local
, other machines in the LAN can passmachine007.local
Directly resolve the LAN IP, and then directly access the local machineHostName
: usuallyLocalHostName
tail-removed.local
That’s what we’re going to sethostname
So we set hostname to use:
# scutil --set HostName <NEW HOSTNAME>
Copy the code