Editing remote files with OpenSSH

As you know, the main feature of OpenSSH is to establish secure connections to remote machines, so you get interactive sessions against them. However, OpenSSH also allows you to execute commands on remote machines. You can execute commands and have the output returned to the screen without logging in to the remote machine.

To execute a command remotely simply type:

ssh user@remote_host ‘ls -al /etc’

However, some commands require a terminal to run properly. For example, if you want to edit a remote file using vi you probably will try something like this:

ssh user@remote_host ‘vi /etc/passwd’

And you’ll get warnings like this:

Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal

To avoid such warnings and cleanly edit your remote files type the following:

ssh -t user@remote_host ‘vi /etc/passwd’

The -t option will… (from OpenSSH man pages)

Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g., when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Leave a Reply

You must be logged in to post a comment.