Copying Files Between Local and Remote Machines Using SCP

Secure Copy Protocol (SCP) is a command-line tool that allows you to securely copy files and directories between your local machine and a remote server over SSH. Here’s how you can use SCP to copy files in both directions: from your local machine to a remote server and from a remote server to your local machine.

Copying from Local to Remote (Upload)

To copy a file from your local machine to a remote server, use the following command:

1
scp /path/to/localfile user@hostname:/path/to/destination/
  • /path/to/localfile: The path to the file on your local machine that you want to copy.
  • user: Your username on the remote server.
  • hostname: The hostname or IP address of the remote server.
  • /path/to/destination/: The destination directory on the remote server where you want to copy the file.

For example, if you want to copy a file named “example.txt” from your local machine to the home directory of a remote server with the IP address “123.45.67.89” and the username “myuser,” you would use:

1
scp example.txt [email protected]:~/

You’ll be prompted to enter the password for the remote user, and then the file will be copied.

Copying from Remote to Local (Download)

To copy a file from a remote server to your local machine, use this command:

1
scp user@hostname:/path/to/remotefile /path/to/localdestination/
  • user: Your username on the remote server.
  • hostname: The hostname or IP address of the remote server.
  • /path/to/remotefile: The path to the file on the remote server that you want to copy.
  • /path/to/localdestination/: The local directory where you want to save the copied file.

For example, if you want to download a file named “example.txt” from the remote server to your local home directory, you would use:

1
scp [email protected]:~/example.txt ~/Downloads/

Again, you’ll be prompted to enter the remote user’s password, and then the file will be copied to your local machine.

Remember to replace the placeholders with your actual file paths, usernames, hostnames, and destination paths.

SCP is a secure and efficient way to transfer files between local and remote machines over SSH, making it a valuable tool for system administrators and developers.

0%