Skip to content
shellhacks.dev
Network

Apply an SSH Option to All Hosts with Host * in Config

Set options for every host with Host * in ssh config, but put it last. The first value found for each option wins, so an early Host * overrides your specific blocks.

1 min read
Summarize in AI

A User line under Host * is meant as a default, and now it overrides the user set for one particular server.

Shell
cat > early <<'EOF'
Host *
  User generic
Host web
  User deploy
EOF
cat > late <<'EOF'
Host web
  User deploy
Host *
  User generic
EOF
ssh -F early -G web | grep '^user '
ssh -F late -G web | grep '^user '
ssh -F late -G other | grep '^user '
Text
user generic
user deploy
user generic

ssh reads the file from the top and, for every option, keeps the first value it finds. Every block whose pattern matches contributes, so web matches both Host * and Host web, and whichever comes first sets User. The first file therefore gives generic, and the second gives deploy for web and the default for any other name. This is the order rule in the ssh_config manual, and it is the opposite of how most configuration files work.

Specific blocks go first and general ones last. Options that accumulate, such as IdentityFile and SendEnv, add up across matches instead of replacing each other. A Match block follows the same rule. Settings in /etc/ssh/ssh_config are read after the user's file, so the user's file still wins.

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.