So, in this post we will show you how you can transfer files to and from an EC2 linux instance using our old trustworthy friend SFTP.
For those who don't know about sftp let us give you a gist of what it is.
SFTP is SSH File Transfer Protocol (also Secure File Transfer Protocol, or SFTP) thus it works on same port 22 as ssh. It's secure in comparison to ftp which works on port 21 and nowadays blocked because of security reasons. sftp is generally pre-installed on most linux versions including Amazon Linux.
Also, if you compare it with SCP , which supports only file transfers, the SFTP allows you to perform a range of operations on remote files and resume file transfers.
If you want to know more about SFTP please look at this sftp wiki page.
Now let's see how we can use sftp to transfer files.
Pre-requisite for this are only two things both of which are pretty much standard requirement to access your EC2 linux instances.
1) ssh .pem key which you configured when you built the remote server where you want to connect.
2) Port 22 should be open to access the remote server. (In case you want to know if a port is open on a remote linux/unix server by CLI without using telnet check this post .)
Once you have checked that you have fulfilled the pre-requisites let's move to the next step.
Open your shell terminal, it can be GIT Bash installed on you local windows desktop or Linux shell terminal.
Inside the terminal you need to execute below command
sftp -o IdentityFile=identity_file ec2-user@10.xxx.xxx.xxx
where identity_file is you .pem key .
Your actual command will look like
sftp -o IdentityFile=cloudvedas.pem ec2-user@192.168.0.10
Let's check our directory in remote server
sftp>pwd
Remote working directory: /home/ec2-user
Let's go to /tmp
sftp> cd /tmp
Let's transfer a file from local machine to the remote server
sftp> PUT test123-file.sh
Now if you want to transfer a file from remote server to local machine
sftp> GET remote123-file.sh
Note: PUT and GET commands are case sensitive and will work in uppercase only.
If you forgot what was the home directory on your local machine you can check that from sftp prompt
sftp>lpwd
Local working directory: /home/cloudvedas
If you want to change the directory in your local machine do this
sftp>lcd /tmp
Hope this simple tutorial is helpful for you. Do let us know in comments section if you have any queries or to share what methods you use to transfer file to EC2 instances.