Copy files using scp from one system to remote system

Linux administrator should be familiar with CLI environment. Since GUI mode in Linux servers is not a common to be installed. SSH may the most popular protocol to enable Linux administrator to manage the servers via remote in secure way. Built-in with SSH command there is SCP command. SCP is used to copy file(s) between servers in secure way.

scp stands for secure cp (copy), which means you can copy files across ssh connection. That connection will be securely encrypted, it is a very secure way to copy files between computers. scp uses by default the port 22, and connect via an encrypted connection or secure shell connection. (ssh for short).


scp [-12346BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2


Copy one single local file to a remote destination

scp /path/to/source-file user@host:/path/to/destination-folder/
scp user@host:/path/to/source-file /path/to/destination-folder

Command copy's local fource file to remote system /path/to/destination-folder/ folder and also vice versa.

Copy one single file from a remote server to another remote server

scp user1@server1:/path/to/file user2@server2:/path/to/folder/

above command copy a file from one server to another server. This command is executed from thrid server.

Copy multiple files with one command

scp file1.txt file2.txt file3.txt

Copies multiple files with file name mentioned, to another server named

Copy all files with wildcard

scp /path/to/folder/*.ext user@server:/path/to/folder/

Command copies list of file with extension .txt with in a folder.

Copy all files in a folder recursively to a remote server

scp -r /path/to/source-folder user@server:/path/to/destination-folder/

Command copies all files and folders with-in source-folder recursively to remote server with same folder structure.

For more scp command options use man page using below man command.

man scp


Tags: SCP, Remote Copy, SSH

Run command on remote server via ssh

If you need to run a command or shell script on a remote server via ssh


ssh <username>@<hostname> '<commands>'
             ssh command uses three mandatory input, 
             username = username to login to remote system
             hostname = Remote system name or ip address
             commands = single command or list of commands seperated by semicolon

Command will login to remote system with the provided username and password and executes the listed command and retrives the return data and shows in terminal or you can redirected to a file using redirect sysmbol.

Instead of execute a command, you can also execute a shell script or any executable remotely.


ssh user@system1 'ls -l'

above command will execute the ls command in home folder of the user and returns a long list of file lists available.

ssh user@system 'pwd; ls; whoami'

above command will execute the commands one by one. 

pwd - current directory - home directory of the logged user, ls - list files in home directory, whoami - shows the current logged in user name.

Executing a remote shell script or a executable

ssh -t user@remotesystem 'sudo service mysql restart'

To start or to stop a service or executable with sudo user access we need to use -t option to Force pseudo-terminal allocation.

-t      Force pseudo-terminal allocation.  This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful,

e.g. when implementing menu services.  Multiple -t options force tty allocation, even if ssh has no local tty.


Tags: SSH, Remote Command