In the past we used insecure File Transfer Protocol (FTP) to transfer files fromone computer to another that acted as an FTP server. FTP used to use a seperate port number then other protocols, port 21, which would also require administrators to port forward it on their routers.
Now we transfer files over SSH protocol, since it is more secure and we use it anyway when we run commands on our remote servers. This allows us to do less port forwarding, since we use port 22 that is designated for SSH, to both log in to our remote shell and to transfer files. We also get more security and it allows us to use same credentials that we use to log in to ssh and we can also use public keys. This makes file transfer to servers a lot easier and more secure today.
SCP is the simplest file transfer program that works over SSH and it is best suitable for use in scripts. It acts exactly like cp
command except it also supports copying files to remote machines, not just on our own computer. SCP also supports ssh arguements and uses same ssh configuration that our ssh client does.
To copy files on another machine we need to write that machines IP address (or domain name) before the file path and seperated it with :
from the rest of the file path. For example:
scp localfile user@192.168.1.13:/home/user/filetext.txt
In the previous example we copied the file from our local computer, in our current working directory, called localfile
to home/user/filetext.txt
location on the machine that has 192.168.1.13
IP address. We can write the username in front of the IP address, just like with ssh.
We can also download files from remote machines and use all the ssh arguments tha we need in the process. For example:
scp -p 22 -i ~/.ssh/id_rsa user@example.com:remotefile file.txt
In this example we specifed explicitly to use port 22
to connect to the remote machine at example.com
We also explicitly instructed scp to use our private key (identity file) named id_rsa
in the .ssh
directory in our home folder (~
is short for the path of your home folder).
This time we used relative path on the remote machine as well to locate our file, instead giving it the full absolute path. By default current working directory on the remote machine will be the home directory of the user we are connecting as (user
in our example).
Tab autocompletion for remote file paths, might work if you have setup your ssh configuration to not require you to type any passwords when logging in to the remote server.
SCP is great because we can use a single command to copy files, which makes it a great option for writing scripts or using it in the crontab. However, ftp clients used to have interactive shells when connected to the remote machine. In these interactive shells we could run ftp specific commands to download and upload files and navigate and list available files on the remote machine.
This made is easier when manually transfering files, as you could upload or download multiple files when logged in and make sure all the files are there.
SFTP program uses SSH protocol but has that same feature of interactive shell. We can also navigate and list files on our local machine by prepending the command with lowercase letter l, which stands for local.
In sftp we can also use some regular file manipulation and navigation Linux (UNIX) commands, like: rm, mkdir, ls, cd and cp. We can use their local versions as well to run them on our local machine while still in our sftp shell: lrm, lmkdir, lls, lcd and lcp.
Not all Linux commands can be used in the sftp shell (at least not on the remote server ), but some of those basic ones can.
Main sftp specific commands you will need are put
and get
The have some optional arguments (like -r
for recurisve directory) that you can read more about by running help put
and help get
in the sftp shell.
Only requried argument for both of these commands is the file path of the local or remote files, depending which command you are using. For example:
put localfile.pdf
With previous command we can upload file localfile.pdf
to the remote server.
get -r /home/bob/Music
With this command we get download the Music folder and all the files in it from the remote machine to our current working directory on our local machine.
Autocompletion for file paths should work in your sftp shell, both for local and remote paths.