This tutorial is designed to guide you through the process of connecting Linux systems using SSH and efficiently transferring files between them. We'll begin by focusing on Part 1, where we'll pull files from `Linux Machine 2` via `Linux Machine 1`. This involves first pulling a single file and then multiple files using SSH.
Moving forward, in Part 2, we'll reverse the direction of the connection, connecting from `Linux Machine 1` to `Linux Machine 2` to copy files back using SSH.
Part 3 expands on this concept further by demonstrating how to connect sequentially from `Linux Machine 1` to `Linux Machine 2`, and then from `Linux Machine 2` to `Linux Machine 3`, facilitating direct file transfers from `Linux Machine 3` to `Linux Machine 1`.
Additionally, we will cover the use of SSH keys in Part 4 for secure file transfers and explore the setup of SSH aliases in Part 5 to streamline command execution. By the end of this tutorial, you will have a comprehensive understanding of SSH connections and efficient file management across multiple Linux machines.
Three Linux systems (Linux Machine 1, Linux Machine 2, Linux Machine 3).
SSH installed on all systems.
To pull a single file from `Linux Machine 2` via `Linux Machine 1`, use the `scp` command:
Linux (Debian/Ubuntu)
scp -P port_number user@machine2_ip:/path/to/file /local/path/on/machine1
Replace:
`port_number`: with the port number for the SSH connection of `Linux Machine 2`,
`user` with your username on `Linux Machine 2`,
`machine2_ip` with the IP address of `Linux Machine 2`,
`/path/to/file` with the file's location on `Linux Machine 2`,
`/local/path/on/machine1` with the directory where you want to save the file on `Linux Machine 1`.
This method securely retrieves a single file from `Linux Machine 2` via `Linux Machine 1` using SSH.
Example:
Linux (Debian/Ubuntu)
scp -P 22 john@192.168.1.2:/home/john/document.txt /home/sara/documents
To pull specific files from `Linux Machine 2` via `Linux Machine 1`, use the `scp` command specifying each file individually:
Linux (Debian/Ubuntu)
scp -P port_number user@machine2_ip:/path/to/file1.txt /path/to/file2.txt /local/path/on/machine1
Replace:
`port_number`: with the port number for the SSH connection of `Linux Machine 2`,
`user` with your username on `Linux Machine 2`,
`machine2_ip` with the IP address of `Linux Machine 2`,
`/path/to/file1.txt` and `/path/to/file2.txt` with the paths of the specific files on `Linux Machine 2` that you want to retrieve,
`/local/path/on/machine1` with the directory where you want to save the files on `Linux Machine 1`.
This command retrieves `file1.txt` and `file2.txt` from `Linux Machine 2` to the specified directory on `Linux Machine 1`.
Example:
Linux (Debian/Ubuntu)
scp -P 22 john@192.168.1.2:/home/john/file1.txt john@192.168.1.2:/home/john/file2.txt /home/sara/documents
To pull all files from a specific directory on `Linux Machine 2` via `Linux Machine 1`, use a wildcard `*`:
Linux (Debian/Ubuntu)
scp -P port_number user@machine2_ip:/path/to/directory/* /local/path/on/machine1
Replace:
`port_number`: with the port number for the SSH connection of `Linux Machine 2`,
`user` with your username on `Linux Machine 2`,
`machine2_ip` with the IP address of `Linux Machine 2`,
`/path/to/directory/*` with the path to the directory containing the files on `Linux Machine 2`,
`/local/path/on/machine1` with the directory where you want to save the files on `Linux Machine 1`.
This command retrieves all files from the specified directory on `Linux Machine 2` to the specified directory on `Linux Machine 1`.
Example:
Linux (Debian/Ubuntu)
scp -P 22 john@192.168.1.2:/home/john/docs/* /home/sara/documents
Ensure the files are transferred successfully by listing the destination directory on `Linux Machine 1`:
Linux (Debian/Ubuntu)
ls /local/path/on/machine1
By following these steps, you can efficiently transfer single or multiple files from `Linux Machine 2` to `Linux Machine 1`.
Open a terminal on `Linux Machine 1`.
Use the `ssh` command to connect to `Linux Machine 2`.
Linux (Debian/Ubuntu)
ssh -P port_number user@machine2_ip
Replace:
`port_number`: with the port number for the SSH connection of `Linux Machine 2`,
`user`: with your username on `Linux Machine 2`
`machine2_ip`: with the IP address of `Linux Machine 2`
Example:
Linux (Debian/Ubuntu)
ssh -P 22 john@192.168.1.2
From `Linux Machine 2`, use the `scp` (secure copy) command to copy a single file to `Linux Machine 1`.
Linux (Debian/Ubuntu)
scp -P port_number /path/to/file user@machine1_ip:/path/to/destination
Replace:
`port_number`: with the port number for the SSH connection of `Linux Machine 1`,
`/path/to/file`: with the path of the file on `Linux Machine 2`,
`user`: with your username on `Linux Machine 1`,
`machine1_ip`: with the IP address of `Linux Machine 1`,
`/path/to/destination`: with the path where you want to save the file on `Linux Machine 1`.
Example:
Linux (Debian/Ubuntu)
scp -P 22 /home/john/document.txt john@192.168.1.3:/home/sara/documents
To copy multiple files, you can specify each file path individually. This method allows you to explicitly list the files you want to copy. Here’s how to do it in a Linux environment, specifically Debian or Ubuntu.
Here’s the command syntax for copying multiple specific files:
Linux (Debian/Ubuntu)
scp -P port_number /path/to/file1 /path/to/file2 user@machine1_ip/path/to/destination
To copy all files in a directory, you can use wildcard characters. Wildcards allow you to match multiple files without listing them individually, which is particularly useful for copying all files within a directory.
Here’s the command syntax for copying all files in a directory:
Linux (Debian/Ubuntu)
scp -P port_number /path/to/files/* user@machine1_ip:/path/to/destination
After copying the files, use the `exit` command to close the SSH session on `Linux Machine 2` and return to `Linux Machine 1`.
Linux (Debian/Ubuntu)
exit
On `Linux Machine 1`, list the destination directory to verify that the files have been copied successfully.
Linux (Debian/Ubuntu)
ls /path/to/destination
In this setup, access to `Linux Machine 2` is only possible through `Linux Machine 1`, and access to `Linux Machine 3` is contingent upon connecting first to `Linux Machine 2`. This sequential access pattern ensures a controlled and secure workflow between the machines. Additionally, files can be directly copied from `Linux Machine 3` to `Linux Machine 1` using SSH, facilitating efficient data transfer across the network.
Open a terminal on `Linux Machine 1`.
Use the `ssh` command to connect to `Linux Machine 2`.
Linux (Debian/Ubuntu)
ssh -P port_number user@machine2_ip
Replace:
`port_number` with the actual port number used for SSH to `Linux Machine 2`.
`user` with your username on `Linux Machine 2`.
`machine2_ip` with the IP address of `Linux Machine 2`.
From the SSH session on `Linux Machine 2`, connect to `Linux Machine 3`.
Linux (Debian/Ubuntu)
ssh -P port_number user@machine3_ip
Replace:
`port_number` with the actual port number used for SSH to `Linux Machine 3`.
`user` with your username on `Linux Machine 3`.
`machine3_ip` with the IP address of `Linux Machine 3`.
From `Linux Machine 3`, use the `scp` command to copy a single file directly to `Linux Machine 1`.
Linux (Debian/Ubuntu)
scp -P port_number /path/to/file user1@machine1_ip:/path/to/destination
Explanation:
`-P port_number`: This specifies the port number to use for the SSH connection. Replace port_number with the actual port number you use to connect to `machine1_ip` via SSH.
`/path/to/file1`: This is the path to the first file you want to copy. Replace `/path/to/file1` with the actual path to the file on `Linux Machine 3`.
`/path/to/file2`: This is the path to the second file you want to copy (optional). Replace `/path/to/file2` with the actual path to another file on `Linux Machine 3`, if you are copying multiple files.
`user1`: This is the username you use to log in to `Linux Machine 1`. Replace `user1` with your actual username on `Linux Machine 1`.
`machine1_ip`: This is the IP address of `Linux Machine 1`. Replace `machine1_ip` with the actual IP address of `Linux Machine 1`.
`/path/to/destination`: This is the path where you want to save the copied files on `Linux Machine 1`. Replace `/path/to/destination` with the actual directory path on `Linux Machine 1` where you want to place the copied files.
To copy multiple files by specifying each file path individually, use the following command syntax:
Linux (Debian/Ubuntu)
scp -P port_number /path/to/file1 /path/to/file2 user1@machine1_ip:/path/to/destination
To copy all files in a directory using wildcard characters (`*`), use the following command syntax:
Linux (Debian/Ubuntu)
scp -P port_number /path/to/files/* user1@machine1_ip:/path/to/destination
After copying the files, use the `exit` command to close the SSH session on `Linux Machine 3` and return to `Linux Machine 2`.
Linux (Debian/Ubuntu)
exit
Use the `exit` command again to close the SSH session on `Linux Machine 2` and return to `Linux Machine 1`.
Linux (Debian/Ubuntu)
exit
On `Linux Machine 1`, list the destination directory to verify that the files have been copied successfully.
Linux (Debian/Ubuntu)
ls /path/to/destination
In this part of the tutorial, we'll cover how to use SSH keys to securely copy files between computers using the `scp` (secure copy) command. SSH keys provide a more secure and convenient method for authentication compared to traditional password-based methods.
SSH keys are a pair of cryptographic keys used for authenticating and securing network connections. Typically, you'll have a private key stored on your local machine and a corresponding public key on the remote machine. If you haven't already set up SSH keys, you can refer to my tutorial on How to Generate SSH Key Pair on Linux and Connect to a Linux Server before proceeding.
To pull files directly from `Linux Machine 2` via `Linux Machine 1`, use the `scp` (secure copy) command. This can be done using SSH keys for authentication to enhance security.
Pull a Single File using SSH Keys:
To pull a single file from `Linux Machine 2` via `Linux Machine 1`, use the following command:
Linux (Debian/Ubuntu)
scp -i "/path/to/my_ssh_key" -P port_number user@machine2_ip:/path/to/file /path/to/destination
Explanation:
`scp`: Secure copy command.
`-i "/path/to/my_ssh_key"`: Specifies the SSH private key to use for authentication.
`-P port_number`: Specifies the port number for the SSH connection.
`user@machine2_ip:/path/to/file`: The user, machine IP, and path to the file on `Linux Machine 2`.
`/path/to/destination`: The path on `Linux Machine 1` where you want to copy the file.
Example:
Linux (Debian/Ubuntu)
scp -i "/home/johndoe/Desktop/CustomFolder/my_ssh_key" -P 2222 alice@192.168.1.3:/home/alice/documents/myfile.txt /home/johndoe/backup/
This command copies `myfile.txt` from `Alice`'s home directory on `Linux Machine 2` to the backup directory on `Linux Machine 1`.
Pull Multiple Files using SSH Keys
To pull multiple files from `Linux Machine 2` via `Linux Machine 1`, you can specify multiple file paths:
Linux (Debian/Ubuntu)
scp -i "/home/johndoe/Desktop/CustomFolder/my_ssh_key" -P 2222 user@machine2_ip:/path/to/file1 user@machine2_ip:/path/to/file2 /path/to/destination
Or, using wildcards to copy all files in a directory:
Linux (Debian/Ubuntu)
scp -i "/home/johndoe/Desktop/CustomFolder/my_ssh_key" -P 2222 user@machine2_ip:/path/to/files/* /path/to/destination
Copy a Single File Using SSH Keys:
To copy a single file from a remote machine (`Linux Machine 2`) to your local machine (`Linux Machine 1`) using an SSH key, use the following `scp` command:
Linux (Debian/Ubuntu)
scp -i "/path/to/my_ssh_key" -P port_number /path/to/file user@machine1_ip:/path/to/destination
Explanation:
`scp`: Secure copy command.
`-i "/path/to/my_ssh_key"`: Specifies the SSH private key to use for authentication.
`-P port_number`: Specifies the port number for the SSH connection.
`/path/to/file`: The path to the file you want to copy.
`user@machine1_ip:/path/to/destination`: The user, machine IP, and destination path on the local machine (`Linux Machine 1`) where you want to copy the file.
Example:
Linux (Debian/Ubuntu)
scp -i "/home/johndoe/Desktop/CustomFolder/my_ssh_key" -P 2222 /home/alice/documents/myfile.txt johndoe@192.168.1.2:/home/johndoe/backup/
This command copies `myfile.txt` from `Alice's home directory` on the remote machine (`Linux Machine 2`) to the documents directory on your local machine (`Linux Machine 1`).
Copy Multiple Files Using SSH Key:
To copy multiple files from `Linux Machine 2` to `Linux Machine 1`, you can specify multiple file paths:
Linux (Debian/Ubuntu)
scp -i "/home/johndoe/Desktop/CustomFolder/my_ssh_key" -P 2222 /path/to/file1 /path/to/file2 user@machine1_ip:/path/to/destination
Or, using wildcards to copy all files in a directory:
Linux (Debian/Ubuntu)
scp -i "/home/johndoe/Desktop/CustomFolder/my_ssh_key" -P 2222 /path/to/files/* user@machine1_ip:/path/to/destination
SSH aliases streamline `scp` commands by replacing host names or IP addresses with short, memorable aliases. This section guides you through setting up SSH aliases for both `Linux Machine 1` and `Linux Machine 2` and demonstrates their use for file transfers.
Configure SSH Alias for `Linux Machine 1`
Open or create the SSH configuration file on your remote machine (`Linux Machine 2`):
Linux (Debian/Ubuntu)
nano ~/.ssh/config
Add the following configuration for `Linux Machine 1`:
Linux (Debian/Ubuntu)
Host machine1
HostName machine1_ip
User local_user
IdentityFile "/home/johndoe/Desktop/CustomFolder/my_ssh_key"
Port machine1_port_number
Replace `machine1` with your preferred alias,
Replace `machine1_ip` with the IP address or hostname of `Linux Machine 1`,
Replace `local_user` with your username on `Linux Machine 1`,
Replace `"/home/johndoe/Desktop/CustomFolder/my_ssh_key"` with the path to your SSH private key.
Specify `Port` if `Linux Machine 1` uses a non-standard SSH port.
Save and close the file (`Ctrl+O` to write out, `Ctrl+X` to exit `nano`).
Configure SSH Alias for `Linux Machine 2`
Open or create the SSH configuration file on your remote machine (`Linux Machine 1`):
Linux (Debian/Ubuntu)
nano ~/.ssh/config
Add the following configuration for `Linux Machine 2`:
Linux (Debian/Ubuntu)
Host machine2
HostName machine2_ip
User local_user
IdentityFile "/home/johndoe/Desktop/CustomFolder/my_ssh_key"
Port machine2_port_number
Replace `machine1` with your preferred alias,
Replace `machine1_ip` with the IP address or hostname of `Linux Machine 2`,
Replace `local_user` with your username on `Linux Machine 2`,
Replace `"/home/johndoe/Desktop/CustomFolder/my_ssh_key"` with the path to your SSH private key.
Specify `Port` if `Linux Machine 2` uses a non-standard SSH port.
Save and close the file (`Ctrl+O` to write out, `Ctrl+X` to exit `nano`).
Using SSH aliases in this way simplifies your `scp` commands by replacing the longer host names or IP addresses with shorter, more memorable aliases.
Now, utilize the defined aliases (`machine1` and `machine2`) in `scp` commands for efficient file transfers.
Pull Files from `Linux Machine 2` via `Linux Machine 1`
To pull a file from `Linux Machine 2` via `Linux Machine 1` using the SSH alias (`machine2`):
Linux (Debian/Ubuntu)
scp machine2:/path/to/file /path/to/destination
Explanation:
`scp`: Secure copy command.
`machine2`: Alias defined in your SSH configuration file (`~/.ssh/config`) representing `Linux Machine 2`.
`/path/to/file`: Path to the file on `Linux Machine 2` you want to copy.
`/path/to/destination`: Path where you want to store the `file` on `Linux Machine 1`.
Example:
Suppose you want to retrieve `file2.txt` from `/home/remote_user/` on `Linux Machine 2`, then transfer it to `/home/local_user/backup_folder/` on `Linux Machine 1` using `scp`. Here’s how you can do it from `Linux Machine 1`:
Linux (Debian/Ubuntu)
scp machine2:/home/remote_user/file2.txt /home/local_user/backup_folder/
Copy Files from `Linux Machine 2` to `Linux Machine 1`
To copy a file from `Linux Machine 2` (remote machine) to `Linux Machine 1` (local machine) using the SSH alias (machine1):
Linux (Debian/Ubuntu)
scp /path/to/file machine1:/path/to/destination
Explanation:
`scp`: Secure copy command.
`/path/to/file`: Path to the file on `Linux Machine 2` that you wish to copy.
`machine1`: Alias representing `Linux Machine 1` as configured in `~/.ssh/config`.
`/path/to/destination`: Destination path on `Linux Machine 1` where `file` will be copied.
Example:
Suppose you want to copy `file1.txt` from `/home/remote_user/` on `Linux Machine 2` to `/home/local_user/destination_folder/` on `Linux Machine 1`. To do this, execute the following command from `Linux Machine 2`:
Linux (Debian/Ubuntu)
scp /home/remote_user/file1.txt machine1:/home/local_user/destination_folder/
Setting up SSH aliases simplifies file transfers between Linux Machine 1 and Linux Machine 2 by replacing complex host configurations with user-defined aliases. By configuring these aliases in the SSH configuration file (~/.ssh/config), you enhance the efficiency and manageability of scp commands, improving your workflow when handling remote file transfers.
This completes the comprehensive tutorial on connecting via SSH, copying files between Linux systems, using SSH keys for secure connections, and simplifying commands with SSH aliases.
Published: June 26, 2024
Have a question or suggestion? Want to request a tutorial or simply leave me a message? I'd love to hear from you! Join our community on Discord for exclusive content, engaging discussions, and more. Thank you! 🌟