Skip to content
shellhacks.dev
Network

Set Host Aliases in ~/.ssh/config with HostName

Give a server a short name in ~/.ssh/config with Host and HostName. A name that matches no Host block is not an error: ssh treats it as a host name.

1 min read
Summarize in AI

You log in to the same server several times a day and the user, port and address are too long to type.

Shell
cat > cfg <<'EOF'
Host db
  HostName 10.0.0.5
  User deploy
  Port 2222
EOF
ssh -F cfg -G db | grep -E '^(hostname|user|port) '
echo ---
ssh -F cfg -G dbx | grep -E '^(hostname|user|port) '
Text
user deploy
hostname 10.0.0.5
port 2222
---
user root
hostname dbx
port 22

A Host line starts a block of settings that apply when the name you type matches one of its patterns, and HostName is the real address to connect to, as in the ssh_config manual. ssh -G resolves the configuration without connecting. The first answer shows the alias expanded. The second shows a typo: no block matches dbx, so ssh keeps it as the host name, with the local user and port 22, and the failure arrives later as a DNS error or a prompt for the wrong account.

Check an alias with ssh -G before relying on it. The file is read once per connection, in order, and the first value found for each option wins, which matters as soon as a second block matches the same name. Use a space or an equals sign between a keyword and its value, and keep the indentation consistent so the blocks stay readable.

Share

More snippets

Network

Send Form Data with curl Using -d or -F

Send form fields with curl -d for urlencoded bodies and -F for multipart. A plus sign sent with -d arrives as a space, so encode values with --data-urlencode.

2 min read
Network

Send JSON in a POST Request with curl

Send JSON with curl -H 'Content-Type: application/json' -d '{...}'. Plain -d labels the body as a form, and curl 7.82 added --json to set both headers.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.