DEV Community

Michele Caci
Michele Caci

Posted on

3

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs