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:~$