Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

A First Entry

Create or open the file on your computer:

nano ~/.ssh/config
Host myserver
    HostName your_server_ip
    User user
    Port 2222
    IdentityFile ~/.ssh/id_ed25519

Now connect with:

ssh myserver

This file is the client configuration. It is separate from /etc/ssh/sshd_config, which configures the server.

Permissions

ssh refuses a config file that other users can write to:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/config

The Keywords You Will Use Most

  • Host: the short name you type. It can list several names or use wildcards such as web-*.
  • HostName: the real IP address or host name.
  • User: the account on the server.
  • Port: the SSH port, if not 22. See how to change the SSH port.
  • IdentityFile: the private key to use for this host.
  • IdentitiesOnly yes: offer only that key, not every key in the agent.
  • ProxyJump: reach the server through another one.
  • ServerAliveInterval: send a keep-alive message after that many seconds without traffic.
  • LocalForward: open a tunnel on every connection.

One Key per Server

Host myserver
    HostName your_server_ip
    User user
    IdentityFile ~/.ssh/work_ed25519
    IdentitiesOnly yes

With many keys in the agent, a server may stop the connection before the right key is tried. IdentitiesOnly yes prevents that, one of the causes covered in Permission denied (publickey).

Through a Jump Host

When a server is reachable only through a gateway machine, describe both and let ssh chain them:

Host bastion
    HostName 203.0.113.10
    User deploy

Host myserver
    HostName your_server_ip
    User user
    ProxyJump bastion

ssh myserver first connects to the bastion, then on to the server. Your private key stays on your computer; the bastion only passes the traffic through.

Keep Idle Sessions Open

Some routers and firewalls close connections that are quiet for a few minutes. A keep-alive prevents that:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

After 60 quiet seconds the client sends a message through the encrypted channel, and it disconnects after three unanswered ones.

Forward a Port on Every Connection

If you always open the same tunnel, for example to a database that listens only on the server, put it in the host block:

Host myserver-db
    HostName your_server_ip
    User user
    LocalForward 3307 127.0.0.1:3306

ssh -N myserver-db then opens the tunnel without a shell. Port 3307 on your computer leads to the database on the server.

Split a Long File

With many servers, keep groups in separate files and include them at the top of ~/.ssh/config:

Include ~/.ssh/config.d/*

Create the folder with mkdir -p ~/.ssh/config.d and put one file per project in it, with mode 600 like the main file. Place the Include line before the first Host line: an Include inside a Host block applies only to that block.

Order Matters

ssh reads the file from top to bottom and, for each setting, keeps the first value it finds for the host. Put specific hosts first and the general Host * block at the end; otherwise its values win. Options on the command line come before the file.

To see the final settings for a host without connecting, run:

ssh -G myserver

Other Tools Use It Too

scp file.txt myserver:/tmp/ and sftp myserver pick up the same user, port, and key; see how to use scp. A tunnel defined with LocalForward is described in SSH tunneling and port forwarding.

Was this answer helpful?
Loading...