• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

run command in remote linux / unix system by using ssh

September 21, 2013 by Sharad Chhetri Leave a Comment

How to run command in remote linux / unix system by using ssh

If you are working as a linux system admin, it is important to know how you can execute the command in remote linux or unix based operating system by using ssh.The method is very useful and you can also use it in your script. SSH bydefault runs in port no. 22.
The advantage of using ssh is ,it make a encrypted connection to remote machine.
I will share some basic example of running the command in remote unix based system

I hope you are already familiar with command ssh username@remote-serverIP-or-FQDN

Details of Remote Machine:

IP Address: 192.168.1.34
Operating System : Ubuntu 12.04 LTS

The syntax of running command in remote system is:


ssh username@Remote-IP-Address-OR-FQDN "command"

Example 1: Simple and general method to execute the command in remote system. In this example I will create a directory called test in remote machine

Here ,
Username: linux
Remote Server IP address: 192.168.169.1.34
Command: mkdir -p ~/test

ssh linux@192.168.169.1.34 "mkdir -p ~/test"

Reference of my system:

linux@tuxworld:~$ ssh linux@192.168.1.34 “mkdir -p ~/test”
linux@192.168.1.34’s password:
linux@tuxworld:~$ ssh linux@192.168.1.34 “ls -ld ~/*”
linux@192.168.1.34’s password:
drwxrwxr-x 2 linux linux 4096 Sep 21 01:32 /home/linux/test
linux@tuxworld:~$

Example 2: Use absolute path of command. It is good practice to use absolute path of command. To get the absolute path of command I will use which command .
Here I will firstly get the absolute path of command.

In this example I will create file called testfile by using touch command

Now I will get absolute path of command in remote system then will use in my next ssh command

ssh linux@192.168.1.34 "which touch"

ssh linux@192.168.1.34 "ssh linux@192.168.1.34 "/usr/bin/touch testfile"

Reference from my system:

linux@tuxworld:~$ ssh linux@192.168.1.34 “which touch”
linux@192.168.1.34’s password:
/usr/bin/touch
linux@tuxworld:~$ ssh linux@192.168.1.34 “/usr/bin/touch testfile”
linux@192.168.1.34’s password:
linux@tuxworld:~$ ssh linux@192.168.1.34 “ls -la”
linux@192.168.1.34’s password:
total 28
drwxr-xr-x 4 linux linux 4096 Sep 21 01:40 .
drwxr-xr-x 3 root root 4096 Sep 19 23:54 ..
-rw-r–r– 1 linux linux 220 Sep 19 23:54 .bash_logout
-rw-r–r– 1 linux linux 3486 Sep 19 23:54 .bashrc
drwx—— 2 linux linux 4096 Sep 20 20:38 .cache
-rw-r–r– 1 linux linux 675 Sep 19 23:54 .profile
drwxrwxr-x 2 linux linux 4096 Sep 21 01:32 test
-rw-rw-r– 1 linux linux 0 Sep 21 01:40 testfile
linux@tuxworld:~$

Example 3: If you have different port no. for ssh other than bydefault 22. You have to pass -p argument in your ssh command.

Syntax of command with different ssh port no.

ssh -p port-no. username@Remote-IP-Address-OR-FQDN "command"

In this example we will create a file and also redirect echo output in the file called hellofile.Later we will open it with cat command.

Here,

SSH port no. 2112 (Note: it might be different port no. in your case,it is just an example)

Note: I have changed the port no. of ssh in remote server.Hence using same IP address

ssh -p 2112 sharad@192.168.1.34 "echo 'hello World from sharadchhetri.com' > hellofile"

Reference from my system:

linux@tuxworld:~$ ssh -p 2112 sharad@192.168.1.34 “echo ‘hello World from sharadchhetri.com’ > hellofile”
sharad@192.168.1.34’s password:
linux@tuxworld:~$
linux@tuxworld:~$ ssh -p 2112 sharad@192.168.1.34 “cat hellofile”
sharad@192.168.1.34’s password:
hello World from sharadchhetri.com
linux@tuxworld:~$

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. SSH WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
  2. rsync over ssh port number on Linux/Unix system
  3. How to know last command run successfully : Linux / Unix
  4. How to install linux operating system in remote machine with telnet
  5. how to deny root ssh access in linux server
  6. How To Find Absolute Full Path Of Command On Linux / Unix : which command
  7. SCP file and directory in linux and Unix like operating system
  8. Non interactive ,without typing password do ssh to Server : By sshpass
  9. Agent admitted failure to sign using the key ssh
  10. How to install ssh server on Ubuntu

Filed Under: Linux, SSH Tagged With: ssh

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

How to increase Password Expire date without resetting the password

Virtual Machine inaccessible status on VirtualBox 4.3

Keep logs of user after sudo su – : Secondary Logging

curl command to check the http status

The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form

How to forcefully send mail from mailq in sendmail MTA

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy