By using the SSH command, You can run a command on a remote server without login in to the server shell.

SSH Command Format

# ssh [email protected] remotecommand

Suppose I want to run ls command on remote server pk.testmail.com

[email protected]:~$ ssh [email protected] ls
 The authenticity of host ' pk.testmail.com (x.x.x.x)' can't be established.
 RSA key fingerprint is 7d:74:91:ed:30:1e:86:0b:69:c9:77:0b:72:0e:ad:4e.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added 'pk.testmail.com' (RSA) to the list of known hosts.
 [email protected]'s password:

anaconda-ks.cfg
downloads
install.log
install.log.syslog

Note: When we run any command on remote server, by default ssh will not allocate a pseudoterminal. For using pseudoterminal we have to use -t option with ssh command as explained below.

[email protected]:~$ ssh -t [email protected] pk.testmail.com ls
 [email protected]'s password:
 anaconda-ks.cfg  downloads  install.log  install.log.syslog
 Connection to pk.testmail.com closed.

Now you can see, Command output will be the same as we run the same command after login in to the remote server.

To run multiple commands on remote server, use semi-colon between commands as shown below:

[email protected]:~$ ssh -t [email protected] pk.testmail.com "ls ; df -h ; date ; cal"
 [email protected]'s password:
 anaconda-ks.cfg  downloads  install.log  install.log.syslog  server_invalid_mails_cleanup.sh
 Filesystem      Size  Used Avail Use% Mounted on
 /dev/vda         20G  6.9G   12G  37% /
 tmpfs           246M     0  246M   0% /dev/shm
 Mon Nov 10 00:52:12 EST 2014
 November 2014
 Su Mo Tu We Th Fr Sa
 1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
 16 17 18 19 20 21 22
 23 24 25 26 27 28 29
 30
 Connection to pk.testmail.com closed.

We can also use echo command to run a remote command on remoteserver.

[email protected]:~$ echo "ls" | ssh [email protected]
 Pseudo-terminal will not be allocated because stdin is not a terminal.
 [email protected]'s password:
 anaconda-ks.cfg
 downloads
 install.log
 install.log.syslog

Note: We can’t use the -t option with the ssh command to run the remote command using echo command because Pseudo-terminal will not be allocated because stdin is not a terminal.

Use ssh command with tar command to move a directory of files between two machines as an alternative to scp command. Example is explained below:

[email protected]:~$ tar -cvf - images/test1 | ssh [email protected] pk.testmail.com  '(cd /tmp/; tar -xf -)'
 images/test1/
 images/test1/7
 images/test1/6
 images/test1/5
 images/test1/4
 images/test1/9
 images/test1/8
 images/test1/3
 images/test1/2
 images/test1/1
 [email protected]'s password:

Now files are uploaded on Remote server. Check files on remote server as shown below.

[[email protected] tmp]# ls -l images/
 total 4
 drwxrwxr-x 2 1000 1000 4096 Nov 10 01:02 test1
 [[email protected] tmp]# ls -l images/test1/
 total 0
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 1
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 2
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 3
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 4
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 5
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 6
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 7
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 8
 -rw-rw-r-- 1 1000 1000 0 Nov 10 01:02 9