Uploading and downloading files in AWS instance can be done using Filezilla client or Linux scp
command. If you are a windows user, you can use WinSCP for transferring files to your EC2 instance.
In this tutorial, I will explain how you can transfer files to AWS ec2 instances using the following methods.
- Copy Files Using Filezilla Client (GUI Based).
- Copy files using SCP (Works only on Linux/MAC systems)
Upload Files To EC2 using FileZilla
To transfer files to ec2 Linux instance, we will use FileZilla
You can download the FileZilla client from here Download FileZilla.
Step1: Install and open FileZilla
Step2: Go To Edit
–>Settings
–>SFTP
Note: By default, AWS ec2 instances come with a private key for login. We will use that private key with Filezilla. If you want to setup a password-based access, check out the ec2 password setup blog.
Click add key file and add your .ppk
key of your AWS instance and then click ok. You can convert the AWS pem file to ppk
using puttygen. Refer to this tutorial –> How To Create .ppk File Using PuttyGen
Step3: In the FileZilla homepage enter the host details (public IP, elastic IP or the public DNS)
and enter the username in the relevant field. ( username varies for different images.
For ubuntu Instance, the username will be ubuntu
, for Linux machines, the username will be ec2-user
) .
Leave the password field empty, since we are using the private key for authentication.
Port number is 22
for SFTP
. Once you enter all the necessary details click connect.
Filezilla will be connected to your server instance and you can view your server files and folders.
Step4: File Upload And Download:- Once FileZilla is connected to your server instance, you can upload files to your instance, and also you can download files from your server instance.
The local site is your local system files. The remote site is your server instance.
To upload files choose the directory on your server where you want your files to be uploaded.
Now select the file to be uploaded and right-click on it. Click the upload file option. Your file starts uploading to the directory you selected. Similarly, you can download files from your server instance by right-clicking the file.
Few important things to understand,
- Since you are logging in as a user, you cannot upload the files to the root directory due to permissions issues. Alternatively, upload the files to the user home directory and copy it to the desired location using any ssh client like putty.
- You will be uploading and downloading files as a normal user and not a root user. So the files that are owned by root cannot be uploaded and downloaded. You will get a permission denied error when you try to download a file owned by root.
So, if you want to download those files, ssh into the machine and change the owner of that file to the normal user using the following command.
sudo chown user:user /folder/file
In the above command, the user is your instance’s default username. For example, for Ubuntu instances, it’s ubuntu , and for RHEL instances its ec2-user
. Give the username accordingly. If you want to change the owner of a recursive folder, add a -R
switch to the command, as shown below.
sudo chown -R user:user /folder/file
Upload File Using SCP Command Line Utility
If you are using a Linux or Mac system, you can use the scp
utility to upload or transfer files to the ec2 instance.
Note: Ensure that your pem file has only read permission to it. Or else the connection might fail.
Let’s have a look at the syntax.
scp -i /path/to/key.pem file-name user@server-ip:/path/to/user/home
For example,
scp -i /keys/mykey.pem file.txt [email protected]:/home/ubuntu
If you want to copy a whole folder, then you should use a recursive flag -r with the command as shown below.
scp -i /keys/mykey.pem -r folder-name [email protected]:/home/ubuntu
I hope this article helps. Let me know in the comments section if you face any errors.
If you want to copy files from Windows to Linux, check out my Windows to Linux file copy guide.
Conclusion
Transferring files to ec2 servers is essential when you host applications like WordPress in ec2.
Always make sure you follow good security practices and provide only the required permission to the files in the server.
Also, always enable correct security group rules to enable an extra layer of the firewall to reduce the attack surface of your websites.
1 comment
not worked for me. permission denie , i can not upload.