hostname - macOS
Overview
The hostname command in macOS is used to display or set the system’s host name. This name is used to identify the machine on a network. The command is most effective in network administration, script automation, and system maintenance tasks.
Syntax
The basic syntax for using the hostname command is:
hostname [options]
To set the hostname:
sudo hostname <new_hostname>
Where <new_hostname> is the name you wish to assign to your machine.
Options/Flags
hostname supports a few options that control its execution:
-s,--short: Display the short host name (the portion before the first dot) only.-f,--fqdn,--long: Display the FQDN (Fully Qualified Domain Name), which is the long host name.-i,--ip-address: Display the IP addresses associated with the host name.-a,--alias: Display the alias name of the host, if used.-d,--domain: Display domain name of the host.-A,--all-fqdns: Display all FQDNs of the machine.--help: Display help information.--version: Display version information.
Examples
- 
Display the current host name:
hostname - 
Set a new host name:
sudo hostname new-host-name - 
Display the IP address of the host:
hostname -i - 
Display the short version of the host name:
hostname -s - 
Display the fully qualified domain name (FQDN):
hostname -f 
Common Issues
- Permission Denied: When setting a new hostname, you need administrative privileges. Ensure you use 
sudoto avoid this error. - Hostname Not Persisting After Reboot: Changes made using 
hostnamemight not persist through a reboot. To permanently change the hostname, use thescutilcommand:sudo scutil --set HostName <new_hostname> 
Integration
Combine hostname with other commands to perform more complex tasks. For example, logging the host name and IP in a log file:
echo "$(date) - Host: $(hostname -s) - IP: $(hostname -i)" >> system.log
This can be used in scripts to track the network status of multiple machines.
Related Commands
scutil: Manage system configuration parameters.ping: Check network connectivity.ifconfig: Configure or display network interface parameters.
For further reading and more detailed information, you can consult the macOS man pages available via man hostname in your terminal.