[ Team LiB ] Previous Section Next Section

Recipe 6.2 Invoking Remote Programs

6.2.1 Problem

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

6.2.2 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

6.2.3 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 session is not manually setting the value of the DISPLAY environment variable. ssh sets it automatically to the correct value. Check your shell startup files (e.g., .bash_profile or .bashrc) and their systemwide equivalents (such as /etc/profile) to ensure they are not setting DISPLAY. Alternatively, X forwarding might be disabled in the SSH server: check the remote /etc/ssh/sshd_config for the setting X11Forwarding no.

6.2.4 See Also

ssh(1). We keep lots of SSH tips at http://www.snailbook.com. The official OpenSSH site is http://www.openssh.com.

    [ Team LiB ] Previous Section Next Section