DEV Community

Michele Caci
Michele Caci

Posted on

How to run commands using SSH

On your UNIX or Linux terminal you may use the ssh command to connect to a distant server/machine and start a new session there. While connected to this session, you may run all the commands you have permission to run in that server/machine but what if you wanted to run these commands but still staying in your starting session?

If to connect to a machine with $IP_ADDR as ip address and with $USERNAME as user you run ssh $USERNAME@$IP_ADDR to run a command in that machine as the aforementioned user you can run ssh $USERNAME@$IP_ADDR 'cmd' which can be any command like ls, pwd, whoami and so on. Quotation marks '' are not mandatory but in my opinion they help to clearly identify what command you intend to run.

A practical (even if a bit cheaty) example:

$ ssh mcaci@localhost 'whoami' # "ssh mcaci@localhost whoami" would give the same result
...
mcaci@localhost's password: 
mcaci
Enter fullscreen mode Exit fullscreen mode

Reference.

Top comments (0)