Chapter 7. SSH Remote Connection

SCP with SSH Config File

SCP with SSH Config File
Tag:

When you use the SSH config file, the scp command becomes much simpler. You can skip typing the private key file path, remote server user name and IP address.

Transferring from local to remote

Only three sets of information are needed for the command.

Source file
  • Source file or directory on your local computer (if you want to transfer a directory, use the -r option.)
Destination
  • SSH host name in your SSH config file
  • Destination directory path on the remote server (If you want to transfer the file to the user's home directory of the remote server, you can skip this argument.)

For a better understanding, please go through the following practice section.

Practice 1

Objective:
Transfer a file from the local computer to the remote server using the SSH config file

The following are the paths of the file to be transferred and the destination directory.

  • File to be transferred: ~/dir_ch7_local/sample2.txt (on the local computer)
  • Destination directory: ~/dir_ch7_remote (on the remote server)

Note: For this practice, open two terminals (command lines). One for a local operation and another one for a remote operation.

1. Create a sample file on the local computer

Create a sample file for transferring to the remote server.

For this operation, use the local terminal that is not connected to the remote server.

Command Line - INPUT (Local)
cd ~/dir_ch7_local
touch sample2.txt

2. Transfer the file

Run the scp command with the source file path and destination directory path.

Command Line - INPUT (Local)
scp sample2.txt ssh_test:dir_ch7_remote
Command Line - RESPONSE
sample2.txt                                    100%    0     0.0KB/s   00:00 

If you set a passphrase for the private key, you need to enter the passphrase to move forward.

3. Check if the file is transferred properly.

Using the terminal connected to the remote server, check if the file is successfully transferred.

Command Line - INPUT (Remote)
ls dir_ch7_remote

You can see that the file was successfully transferred.

Command Line - RESPONSE
sample.txt sample2.txt

Transferring from remote to local

Only three sets of information are needed for the command.

Source
  • SSH host name in your SSH config file
  • Source file or directory path on the remote server (If you want to transfer a directory, use the -r option.)
Destination
  • Destination directory path on your local computer (If you want to transfer the file to your home directory on your computer, you can skip this argument.)

For a better understanding, please go through the following practice section.

Practice 2

Objective:
Transfer a directory from the remote server to the local computer using the SSH config file

The following are the paths of the directory to be transferred and the destination directory.

  • Directory to be transferred: ~/dir_ch7_remote (on the remote server)
  • Destination directory: ~/dir_ch7_local_2 (on the local computer)

Note: This practice can be done only with the local terminal.

1. Create a new directory

Run the command below to create a new directory for receiving a directory from the remote server.

Command Line - INPUT (Local)
cd ~
mkdir dir_ch7_local_2

2. Transfer the directory

Run the scp command with the -r option

Command Line - INPUT (Local)
scp -r ssh_test:dir_ch7_remote dir_ch7_local_2
Command Line - RESPONSE
sample.txt                                    100%    0     0.0KB/s   00:00    
sample2.txt                                   100%    0     0.0KB/s   00:00

If you set a passphrase for the private key, you need to enter the passphrase to move forward.

3. Check if the directory is transferred properly.

Check if the directory is successfully transferred from the remote server to the local computer.

Command Line - INPUT (Local)
ls dir_ch7_local_2

You can see that the file was successfully transferred.

Command Line - RESPONSE
dir_ch7_remote

When you use the SSH config file, the scp command becomes much simpler. You can skip typing the private key file path, remote server user name and IP address.

Transferring from local to remote

Only three sets of information are needed for the command.

Source file
  • Source file or directory on your local computer (if you want to transfer a directory, use the -r option.)
Destination
  • SSH host name in your SSH config file
  • Destination directory path on the remote server (If you want to transfer the file to the user's home directory of the remote server, you can skip this argument.)

For a better understanding, please go through the following practice section.

Practice 1

Objective:
Transfer a file from the local computer to the remote server using the SSH config file

The following are the paths of the file to be transferred and the destination directory.

  • File to be transferred: ~/dir_ch7_local/sample2.txt (on the local computer)
  • Destination directory: ~/dir_ch7_remote (on the remote server)

Note: For this practice, open two terminals (command lines). One for a local operation and another one for a remote operation.

1. Create a sample file on the local computer

Create a sample file for transferring to the remote server.

For this operation, use the local terminal that is not connected to the remote server.

Command Line - INPUT (Local)
cd ~/dir_ch7_local
touch sample2.txt

2. Transfer the file

Run the scp command with the source file path and destination directory path.

Command Line - INPUT (Local)
scp sample2.txt ssh_test:dir_ch7_remote
Command Line - RESPONSE
sample2.txt                                    100%    0     0.0KB/s   00:00 

If you set a passphrase for the private key, you need to enter the passphrase to move forward.

3. Check if the file is transferred properly.

Using the terminal connected to the remote server, check if the file is successfully transferred.

Command Line - INPUT (Remote)
ls dir_ch7_remote

You can see that the file was successfully transferred.

Command Line - RESPONSE
sample.txt sample2.txt

Transferring from remote to local

Only three sets of information are needed for the command.

Source
  • SSH host name in your SSH config file
  • Source file or directory path on the remote server (If you want to transfer a directory, use the -r option.)
Destination
  • Destination directory path on your local computer (If you want to transfer the file to your home directory on your computer, you can skip this argument.)

For a better understanding, please go through the following practice section.

Practice 2

Objective:
Transfer a directory from the remote server to the local computer using the SSH config file

The following are the paths of the directory to be transferred and the destination directory.

  • Directory to be transferred: ~/dir_ch7_remote (on the remote server)
  • Destination directory: ~/dir_ch7_local_2 (on the local computer)

Note: This practice can be done only with the local terminal.

1. Create a new directory

Run the command below to create a new directory for receiving a directory from the remote server.

Command Line - INPUT (Local)
cd ~
mkdir dir_ch7_local_2

2. Transfer the directory

Run the scp command with the -r option

Command Line - INPUT (Local)
scp -r ssh_test:dir_ch7_remote dir_ch7_local_2
Command Line - RESPONSE
sample.txt                                    100%    0     0.0KB/s   00:00    
sample2.txt                                   100%    0     0.0KB/s   00:00

If you set a passphrase for the private key, you need to enter the passphrase to move forward.

3. Check if the directory is transferred properly.

Check if the directory is successfully transferred from the remote server to the local computer.

Command Line - INPUT (Local)
ls dir_ch7_local_2

You can see that the file was successfully transferred.

Command Line - RESPONSE
dir_ch7_remote

Tag: