Run commands on remote server via ssh

Written by
Date: 2010-11-10 10:36:30 00:00


If you need to run a command on a remote server via ssh, you can create a pseudo TTY with ssh -t

The -t option in the ssh -t command force pseudo-tty allocation.

This is very handy when you need to run remote screen based applications on the remote server, establishing something like a “temporary” ssh connection, that will be established while the command is running, and it is closed as soon as the command is finished.

Let’s suppose you want to edit a file in the remote server.

ssh -t [user]@[remote-server] vim [file]

Once you close the remote file, the ssh session will be terminated.

Another good use, is to run htop on a remote computer.

ssh -t user@10.1.1.2 htop

And it can also be used to run commands as root with the sudo command, on servers with the root account disabled like Ubuntu.

ssh -t user@server.tld "sudo command"

You will be asked for the password of user on remote server in order to run command as root.