6.2. Invoking Remote Programs

Problem

You want to invoke a program on a remote machine over a secure network connection.

Solution

For noninteractive commands:

$ ssh -l remoteuser remotehost uptime

For interactive programs, add the -t option:

$ ssh -t -l remoteuser remotehost vi

For X Window applications, add the -X option to enable X forwarding. Also add the -f option to background the program after authentication, and to redirect standard input from /dev/null to avoid dangling connections.

$ ssh -X -f -l remoteuser remotehost xterm

Discussion

For noninteractive commands, simply append the remote program invocation to the end of the ssh command line. After authentication, ssh will run the program remotely and exit. It will not establish a login session.

For interactive commands that run in your existing terminal window, such as a terminal-based text editor or game, supply the -t option to force ssh to allocate a pseudo-tty. Otherwise the remote program can get confused or refuse to run:

$ ssh server.example.com emacs -nw
emacs: standard input is not a tty
$ ssh server.example.com /usr/games/nethack
NetHack (gettty): Invalid argument
NetHack (settty): Invalid argument Terminal must backspace.

If your program is an X application, use the -X option to enable X forwarding. This forces the connection between the X client and X server—normally insecure—to pass through the SSH connection, protecting the data.

$ ssh -X -f server.example.com xterm

If X forwarding fails, make sure that your remote ...

Get Linux Security Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.