SSH without Interactive Session

By default ssh drops the user into an interactive session once the connection is established. In certain use cases one might prefer changing this behaviour.

Reading ssh‘s man page, there are a few relevant options:

  1. -f: Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote site is with something like ssh -f host xterm.
  2. -n: Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh is run in the background. A common trick is to use this to run X11 programs on a remote machine. For example, ssh -n emacs & will start an emacs on, and the X11 connection will be automatically forwarded over an encrypted channel. The ssh program will be put in the background. (This does not work if ssh needs to ask for a password or passphrase; see also the -f option.)
  3. -N: Do not execute a remote command. This is useful for just forwarding ports (protocol version 2 only).

Since `-f` implies `-n`, one can simply use
$ ssh -D 1081 -fN host
to create a socks proxy tunnel, or
$ ssh -L 8888:localhost:8888 -fN host
to forward a port.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.