camzabriskie.com

Primers › Networking

Giving a machine a name you control

07 / 10 updated Oct 14, 2026
On this page
  1. A few terms
  2. Two questions, two failures
  3. How a name becomes an address
  4. Option 1: a hosts-file line
  5. Option 2: an SSH config alias
  6. Which one to use
  7. When an EC2 instance’s address changes
  8. Resolving a name doesn’t open the door
  9. Troubleshooting in order
  10. References

You’ve just built a virtual machine at 10.0.1.23, and there’s no DNS record for it. Typing the address every time gets old, and ssh app-server fails with Could not resolve hostname app-server, because nothing on your machine knows where app-server is. One line in the right file fixes that, without asking anyone to change DNS.

Two files can do it, and they answer slightly different needs. A hosts file line makes the name work for every program on your machine. An SSH config alias only works for SSH, but it can also fill in the user and key. Common reasons to reach for one:

This primer follows one example the whole way: a server at 10.0.1.23 that you log into as the user deploy with the key ~/.ssh/app-server.pem, and want to reach as app-server.

A few terms

Two questions, two failures

Connecting to a machine by name answers two separate questions, in order:

QuestionAnswered byFailure looks like
What IP address does this name mean?An SSH config HostName, the hosts file, or DNSCould not resolve hostname
Can I reach that address and log in?Routes, firewalls, security groups, the user, the keyA timeout, Connection refused, or Permission denied

Here’s where each part happens when you type ssh app-server, and where the two options plug in:

Both options plug into the top half, which only finds an address. Nothing in the top half can fix a problem in the bottom half.

Fixing the first question never fixes the second. A name that resolves to an address a firewall blocks still times out, just with a different error than before.

known_hosts belongs to neither question in the way people often expect. It records each server’s host key so SSH can tell it’s talking to the same machine as last time. It says nothing about where the machine is [1].

How a name becomes an address

Programs ask the operating system’s resolver, and the resolver reads the hosts file as well as asking DNS. Each line of the hosts file holds an address, a name, and optional extra names, separated by any number of spaces or tabs, with # starting a comment [2]:

10.0.1.23   app-server

Each system decides the order in its own way:

SystemHosts file
macOS, Linux/etc/hosts [2]
WindowsC:\Windows\System32\drivers\etc\hosts [6]

Editing it needs administrator rights on all three. Changes normally take effect immediately, except in programs that keep their own copy of the file [2].

Option 1: a hosts-file line

Add the line, as an administrator:

macOS / Linux

echo "10.0.1.23   app-server" | sudo tee -a /etc/hosts

Windows (PowerShell)

# Run PowerShell as Administrator
Add-Content -Path "$env:SystemRoot\System32\drivers\etc\hosts" -Value "10.0.1.23   app-server"

Then check what the resolver now returns for the name. These go through the same lookup programs use, hosts file included [7], [8], [5]:

macOS / Linux

getent hosts app-server                  # Linux
dscacheutil -q host -a name app-server   # macOS

Windows (PowerShell)

Resolve-DnsName app-server

On Linux, a working entry prints the address and the name:

10.0.1.23       app-server

If an old answer seems stuck, clear the resolver’s cache [8], [9], [10]:

macOS / Linux

sudo dscacheutil -flushcache   # macOS
resolvectl flush-caches        # Linux with systemd-resolved

Windows (PowerShell)

Clear-DnsClientCache

The hosts file only affects the one machine it’s on, and you keep it up to date by hand. When the server’s address changes, the line goes stale and the name quietly points at the wrong place.

Option 2: an SSH config alias

The SSH client reads ~/.ssh/config, or %userprofile%\.ssh\config on Windows [11], [12]. A Host block applies its settings when the name you typed matches the name after Host [11]:

Host app-server
    HostName 10.0.1.23
    User deploy
    IdentityFile ~/.ssh/app-server.pem
    IdentitiesOnly yes

app-server is now only a label. HostName is the real address or DNS name SSH connects to, so the hosts file isn’t involved. IdentitiesOnly yes makes SSH offer only the key named here, even when an SSH agent holds others [11]. With the user and key set too, ssh app-server now does what ssh -i ~/.ssh/app-server.pem deploy@10.0.1.23 did.

ssh -G prints the settings SSH would use for a name, after reading every Host block, without connecting [1]. For app-server it shows the block took effect:

ssh -G app-server
user deploy
hostname 10.0.1.23
port 22
identitiesonly yes
identityfile ~/.ssh/app-server.pem

If it shows hostname as the name you typed and user as your own username, no block matched. A typo like ssh -G app-sever looks like that.

An Include line pulls settings in from other files, so a separate file such as ~/.ssh/config_lab only takes effect if the main config names it [11]:

Include ~/.ssh/config_lab

Which one to use

Hosts fileSSH config
Works forEvery program on the machine: browsers, curl, SSHSSH, and the tools that run over it, like scp and sftp
Needs administrator rightsYesNo, it’s in your home folder
Can set the user and keyNoYes
Good forA name that has to work everywhere, like trying a website on a new serverLogging into servers

When SSH is the only thing that needs the name, the SSH config is usually the better choice. Both live on one machine only. A name everyone on the network should share belongs in DNS.

When an EC2 instance’s address changes

AWS only

Both options point at a fixed address, so on a cloud server the address has to stay put, or the name goes stale. On EC2:

AddressChanges when
Public IPv4The instance is stopped and started again. It gets a new one each time, unless it has an Elastic IP [13]
Private IPv4The instance is terminated and replaced. It survives a stop and start [13]

A private address inside the VPC is usually stable enough for a hosts line or a HostName. If it moves often, use a DNS record in a private zone that every machine on the network shares, or skip addresses and reach the instance by its ID through Session Manager.

Resolving a name doesn’t open the door

After the name resolves, the connection still has to get through the network. A firewall rule that allows port 22 from 10.0.2.0/25 covers only 10.0.2.0 through 10.0.2.127. A laptop at 10.0.3.20 falls outside it, so its connection times out even though the name resolves fine. IP addresses and CIDR explains how to read a range like that.

Troubleshooting in order

Work down the diagram: find the address, reach it, then log in.

  1. Does the alias say what you expect? ssh -G app-server [1].

  2. Does the name resolve? Use the resolver checks under Option 1. No answer means the first question is still unsolved.

  3. Can you reach the address? Try the port directly. A hang points at routing or a firewall, and “refused” means the machine answered but nothing is listening on that port [14], [15]:

    macOS / Linux

    nc -vz 10.0.1.23 22

    Windows (PowerShell)

    Test-NetConnection 10.0.1.23 -Port 22
  4. Why did login fail? ssh -v app-server prints each step, including which keys SSH offered [1].

  5. A warning that the host key changed means the server’s key no longer matches the one known_hosts remembers for that name. SSH warns, and turns off password logins for it, because that’s also what an impostor would look like [1]. If you know the server was rebuilt, ssh-keygen -R app-server removes the old key so SSH can learn the new one [16].

References

Show all 16 sourcesHide sources
  1. [1] ↩ a b c d e f OpenBSD, “ssh(1),” OpenBSD manual pages. Accessed: Oct. 5, 2026. [Online]. Available: https://man.openbsd.org/ssh
  2. [2] ↩ a b c The Linux man-pages project, “hosts(5),” Linux manual pages. Accessed: Oct. 14, 2026. [Online]. Available: https://man7.org/linux/man-pages/man5/hosts.5.html
  3. [3] ↩ The Linux man-pages project, “nsswitch.conf(5),” Linux manual pages. Accessed: Oct. 14, 2026. [Online]. Available: https://man7.org/linux/man-pages/man5/nsswitch.conf.5.html
  4. [4] ↩ Apple, “hosts(5),” macOS manual pages, online copy of the Xcode man pages. Accessed: Oct. 14, 2026. [Online]. Available: https://keith.github.io/xcode-man-pages/hosts.5.html
  5. [5] ↩ a b Microsoft, “Resolve-DnsName,” Windows PowerShell DnsClient Module Reference. Accessed: Oct. 6, 2026. [Online]. Available: https://learn.microsoft.com/en-us/powershell/module/dnsclient/resolve-dnsname
  6. [6] ↩ Microsoft, “How to reset the Hosts file back to the default,” Microsoft Support. Accessed: Oct. 14, 2026. [Online]. Available: https://support.microsoft.com/en-us/windows/experience/how-to-reset-the-hosts-file-back-to-the-default
  7. [7] ↩ The Linux man-pages project, “getent(1),” Linux manual pages. Accessed: Oct. 14, 2026. [Online]. Available: https://man7.org/linux/man-pages/man1/getent.1.html
  8. [8] ↩ a b Apple, “dscacheutil(1),” macOS manual pages, online copy of the Xcode man pages. Accessed: Oct. 14, 2026. [Online]. Available: https://keith.github.io/xcode-man-pages/dscacheutil.1.html
  9. [9] ↩ The systemd project, “resolvectl(1),” Linux manual pages. Accessed: Oct. 14, 2026. [Online]. Available: https://man7.org/linux/man-pages/man1/resolvectl.1.html
  10. [10] ↩ Microsoft, “Clear-DnsClientCache,” Windows PowerShell DnsClient Module Reference. Accessed: Oct. 6, 2026. [Online]. Available: https://learn.microsoft.com/en-us/powershell/module/dnsclient/clear-dnsclientcache
  11. [11] ↩ a b c d OpenBSD, “ssh_config(5),” OpenBSD manual pages. Accessed: Oct. 14, 2026. [Online]. Available: https://man.openbsd.org/ssh_config
  12. [12] ↩ Microsoft, “OpenSSH Server configuration for Windows,” Windows Server documentation. Accessed: Oct. 14, 2026. [Online]. Available: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh-server-configuration
  13. [13] ↩ a b Amazon Web Services, “Amazon EC2 instance IP addressing,” Amazon EC2 User Guide. Accessed: Oct. 5, 2026. [Online]. Available: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html
  14. [14] ↩ OpenBSD, “nc(1): arbitrary TCP and UDP connections and listens,” OpenBSD manual pages. Accessed: Oct. 12, 2026. [Online]. Available: https://man.openbsd.org/nc.1
  15. [15] ↩ Microsoft, “Test-NetConnection,” PowerShell NetTCPIP module reference. Accessed: Oct. 12, 2026. [Online]. Available: https://learn.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection
  16. [16] ↩ OpenBSD, “ssh-keygen(1),” OpenBSD manual pages. Accessed: Oct. 14, 2026. [Online]. Available: https://man.openbsd.org/ssh-keygen

Every source cited across the primers is collected on one page.