Introduction
Transferring files between a remote server and your local machine is a common task for developers, system administrators, and database managers. One of the most secure and efficient ways to do this is by using scp
(Secure Copy Protocol), which leverages SSH (Secure Shell) to transfer files securely.
This guide will walk you through the step-by-step process of transferring a file from a remote Ubuntu server to your local machine using password authentication. If your server uses SSH keys for authentication, we will cover that in a separate blog post.
Prerequisites
Before we start, ensure that:
- You have access to the remote server via SSH.
- The
scp
command is available on both the remote and local machines. - You know the remote server’s IP address or hostname.
- You have a user account with appropriate permissions on the remote server.
- You have the password for the user account on the remote server (you will be prompted for it when running
scp
).
Step 1: Create a Directory for Storing Backups
Before transferring the file, let’s create a dedicated directory on your local machine to store the backup.
Open your terminal on your local machine.
Run the following command to create a directory named backups
on your Desktop:
cd ~/Desktop sudo mkdir backups
The sudo
command ensures that you have the necessary permissions to create the directory.
Give yourself write permissions to the directory:
sudo chmod u+w ~/Desktop/backups
Navigate into the directory:
cd ~/Desktop/backups
Now, we are ready to transfer the file from the remote server.
Step 2: Locate the File on the Remote Server
On the remote Ubuntu server, the database backup file is located at:
/home/karani/databases/2025-erp-database.zip
Make sure the file exists before proceeding.
Step 3: Transfer the File Using SCP
Now, use the scp
command to securely copy the file from the remote server to the current directory (.
) on your local machine.
Run the following command from inside the backups directory:
scp karani@remote_server:/home/karani/databases/2025-erp-database.zip .
Explanation of the Command:
scp
– The command to securely copy files over SSH.karani@remote_server
– The SSH username (karani
) and the remote server’s address (remote_server
). Replaceremote_server
with the actual IP address or hostname of the server./home/karani/databases/2025-erp-database.zip
– The full path of the file on the remote server..
– This specifies that the file should be copied to the current directory (which is~/Desktop/backups
since we navigated there earlier).
Entering Your Password
Once you run the command, you will be prompted to enter the password for the user karani
on the remote server. Type in your password and press Enter
. The file transfer will begin.
Step 4: Verify the Transfer
Once the transfer is complete, check if the file was successfully copied by running:
ls -lh
This command lists all files in the directory along with their sizes. If you see 2025-erp-database.zip
, the transfer was successful!
Additional Tips
Copy an Entire Directory
If you need to copy a full directory instead of a single file, use the -r
(recursive) flag:
scp -r karani@remote_server:/home/karani/databases/ .
Specify a Custom SSH Port
If your SSH server runs on a port other than the default 22
, use the -P
flag to specify the port number:
scp -P 2222 karani@remote_server:/home/karani/databases/2025-erp-database.zip .
Conclusion
Using scp
with password authentication, you can securely transfer files from a remote Ubuntu server to your local machine. By following these steps, you have:
- Created a dedicated folder for storing backups.
- Given yourself write permissions and navigated into the directory.
- Used
scp
to download a database backup from a remote server. - Entered the required password to authenticate the transfer.
- Verified the successful transfer of the file.
This method is great for occasional file transfers. However, if you frequently transfer files, consider using SSH key authentication for a more seamless experience (will be covered in a future blog post).