Securely Connect and Transfer Files: A Guide to SSH and SCP


In the world of servers and remote access, SSH (Secure Shell) is the backbone of secure communication, allowing users to log in and execute commands on remote machines. But what if you need to transfer files between your local system and a remote server? That’s where SCP (Secure Copy Protocol) comes in.
In this blog, we'll break down how SSH works, how to SSH to our server, how to securely transfer files using SCP from our laptop to our server for some practical use cases. By the end, we’ll have a solid grasp of these powerful tools and how to use them efficiently.
SSH (Secure Shell)
What is SSH?
SSH (Secure Shell) is a protocol that lets you securely access and control remote computers over a network. SSH is widely used by developers, system administrators, and DevOps engineers to manage servers, transfer files, and automate tasks remotely—all while keeping the communication encrypted and secure.
How Does SSH Work?
SSH works using a client-server model:
You (the client) send a request to connect to a remote computer (the server).
The server verifies who you are using a password or an SSH key.
Once verified, an encrypted connection is established, allowing you to securely run commands on the remote machine.
Encryption is the key part here! SSH ensures that all data (commands, passwords, file transfers) remains private and protected from hackers.
How to SSH into an EC2 Instance?
To connect to an AWS EC2 instance using SSH, follow these steps:
Get Your EC2 Instance Details:
Before connecting, you need:
✅ Public IP Address – Found in the AWS EC2 Dashboard under “Instances.”
✅ Key Pair (.pem file) – The private key file you downloaded when creating the EC2 instance.In order to connect to your server, move in the directory where your .pem key is downloaded.
Set Permissions for the PEM File:
For security reasons, SSH will refuse to use the key if it's too accessible. Run the following command to restrict its permissions:
chmod 400 your-key-name.pem
This makes the file readable only by you, preventing unauthorized access.
Connect to the EC2 Instance via SSH
Now, use the following SSH command:
ssh -i your-key-name.pem ec2-user@<EC2-PUBLIC-IP> ssh -i SHL-Key.pem
-i my-key.pem
→ Specifies the private key.ec2-user
→ Default username for Amazon Linux instances.For Ubuntu instances, use
ubuntu@<EC2-PUBLIC-IP>
.For RHEL/CentOS, use
ec2-user@<EC2-PUBLIC-IP>
.
<EC2-PUBLIC-IP>
→ Replace with your instance’s public IP or public DNS (found in AWS Console).
Accept the SSH Connection
On your first connection, you might see a message like:
The authenticity of host '54.xx.xx.xx' can't be established. Are you sure you want to continue connecting (yes/no)?
Type yes and press Enter.
You're In!
If successful, your terminal prompt should change, indicating you're inside the EC2 instance:
ubuntu@ip-172-31-2-225:~$
Now you can run commands on the remote EC2 server! 🚀
SCP (Secure Copy)
What is SCP?
SCP (Secure Copy Protocol) is a command-line tool used to securely transfer files between your local system and a remote server over SSH. It ensures that file transfers are encrypted, making it a secure alternative to FTP or other transfer methods.
With SCP, you can:
✅ Copy files from local to remote
✅ Copy files from remote to local
✅ Copy entire directoriesHow to Use SCP with an EC2 Instance?
Before using SCP, make sure:
You have your PEM file (e.g.,
my-key.pem
).You have the public IP address of your EC2 instance.
Your EC2 security group allows inbound SSH (port 22).
Copy a File from Local to EC2
To upload a file (
file.txt
) from your local machine to an EC2 instance:scp -i your-key-name.pem file-name ec2-user@<EC2-PUBLIC-IP>:/home/ec2-user/ scp -i ~/Downloads/Keys/SHL-Key.pem Go-Lend-Manager ubuntu@ec2-13-126-217-200.ap-south-1.compute.amazonaws.com:/home/ubuntu/
-i my-key.pem
→ Specifies the SSH key.file.name
→ The file you want to transfer.ec2-user@<EC2-PUBLIC-IP>
→ Your EC2 username and IP address./home/ec2-user/
→ The destination folder on EC2.
✔ After running this, file.txt
will be in the home directory of the EC2 instance.
Copy a File from EC2 to Local
To download
file.txt
from EC2 to your local machine:scp -i my-key.pem ec2-user@<EC2-PUBLIC-IP>:/home/ec2-user/file.txt . scp -i ~/Downloads/Keys/SHL-Key.pem ubuntu@ec2-13-126-217-200.ap-south-1.compute.amazonaws.com:/home/ubuntu/Project .
✔ This saves the file in your current local directory (
.
means "here").Copy an Entire Folder (Recursively)
To upload a local folder (
myfolder/
) to EC2:scp -i my-key.pem -r myfolder ec2-user@<EC2-PUBLIC-IP>:/home/ec2-user/
-r
→ Copies everything inside the folder recursively.
To download a folder from EC2 to your local system:
scp -i my-key.pem -r ec2-user@<EC2-PUBLIC-IP>:/home/ec2-user/myfolder .
✔ This brings the entire folder to your local system.
Troubleshooting SCP Issues
❌ Permission denied (publickey)?
🔹 Ensure your PEM file has correct permissions (chmod 400 your-key-name.pem
).
❌ Connection timeout?
🔹 Check that your EC2 security group allows SSH (port 22).
❌ No such file or directory?
🔹 Make sure you're providing the correct path on both local and remote systems.
Subscribe to my newsletter
Read articles from Sahil Naik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sahil Naik
Sahil Naik
💻 Sahil learns, codes, and automates, documenting his journey every step of the way. 🚀