MJUN Tech Note

SSH Options and Config Settings

The Basics

ssh username@{hostname or ip}

By default, it connects to port 22. The username can be omitted. If omitted, it connects with the current PC’s username.

~/.ssh/config

On Unix and Linux systems, private keys and config are stored under .ssh/ in the user’s home directory. The .ssh directory should have 700 permissions, and files under .ssh should have 600 permissions.

~/.ssh/
├── authorized_keys
├── config
├── id_rsa
├── id_rsa.pub
├── known_hosts
├── known_hosts-e

To make SSH connections easier, write connection settings in advance in the config file.

Host hoge
  HostName 192.168.0.0
  User hoge
  Port 22
  IdentifyFile ~/.ssh/id_rsa

With the above settings written in ~/.ssh/config, you can connect with just ssh hoge.

Multi-hop SSH

For security reasons, sometimes you can’t connect directly to the target machine. For example, you might only be able to connect to the target machine via local network, requiring connection through a proxy server (which is in the same network as the target machine and accessible from outside). In this case, you need to layer SSH connections like Client -> Proxy -> Target. Here’s how to do this with a single SSH command or SSH config.

First, the SSH command:

ssh -o ProxyCommand='ssh -W %h:%p Proxy' Target

-o is an option to specify SSH settings, which takes priority over SSH config.

Next, the SSH config setting:

Host proxy
    User hoge

Host target
    User fuga
    ProxyCommand  ssh -W %h:%p proxy

With the above settings, running ssh target will connect to the server behind the proxy.

SSH Command Options and SSH Config Correspondence

commandconfigDescriptionExample
-pPortSpecify the port of the connection targetssh -p 22
-iIdentifyFilePath to the private key used for connectionssh -i ~/.ssh/hoge_rsa
-XForwardX11Whether to forward X windowForwardX11 yes
-YForwardX11TrustedTrusted X window forwarding. Use this for Xquartz on macOSForwardX11Trusted yes
-LLocalForwardForward client port to PC in remote networkssh -L 8080:remote_pc:80
-o StrictHostKeyChecking=StrictHostKeyCheckingDetermines how to handle known_hostsyes(don’t connect), no(connect), ask(default confirm)

Settings Available for macOS

  • AddKeysToAgent
  • UseKeychain

Register keys with SSH agent. Effective even after restart.