Secure File Transfer Basics

LinuxLinuxBeginner
Practice Now

Introduction

This lab will cover the basics of using the Linux command line to transfer files using SFTP, FTP, and SCP. These tools are commonly used to transfer files to and from remote servers, and can be useful for tasks such as uploading website files to a server or downloading data from a remote machine.

We have configure a SFTP and FTP server in the lab environment. When you actually use, make sure that you have the necessary credentials to access the remote server (such as a username and password) and that the server is configured to allow file transfers over SFTP, FTP, and SCP.

Achievements

  • sftp - Secure File Transfer Protocol
  • ftp - File Transfer Protocol
  • scp - Secure Copy

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") linux/RemoteAccessandNetworkingGroup -.-> linux/scp("`Secure Copying`") linux/RemoteAccessandNetworkingGroup -.-> linux/sftp("`Secure File Transferring`") linux/RemoteAccessandNetworkingGroup -.-> linux/ftp("`File Transferring`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills linux/source -.-> lab-40{{"`Secure File Transfer Basics`"}} linux/scp -.-> lab-40{{"`Secure File Transfer Basics`"}} linux/sftp -.-> lab-40{{"`Secure File Transfer Basics`"}} linux/ftp -.-> lab-40{{"`Secure File Transfer Basics`"}} shell/comments -.-> lab-40{{"`Secure File Transfer Basics`"}} shell/quoting -.-> lab-40{{"`Secure File Transfer Basics`"}} shell/cond_expr -.-> lab-40{{"`Secure File Transfer Basics`"}} shell/globbing_expansion -.-> lab-40{{"`Secure File Transfer Basics`"}} end

Connect to a Remote Server with SFTP

SFTP (Secure File Transfer Protocol) is a secure way to transfer files between a local machine and a remote server.

To connect to a remote server using SFTP, you can use the sftp command followed by the username and the IP address or hostname of the server. For example:

The password for the sftpuser is 123456.

## Type yes to accept the server's fingerprint
sftp sftpuser@127.0.0.1

You will be prompted for the password for the user. Once you have entered the correct password, you will be connected to the remote server.

Transfer files with SFTP

Once you are connected to the remote server, you can transfer files using the put and get commands.

  • To upload a file from your local machine to the remote server, use the put command followed by the path to the local file. For example:
put /home/labex/.zshrc
  • To download a file from the remote server to your local machine, use the get command followed by the path to the remote file. For example:
get /sfptuser/.zshrc

When you are finished transferring files, you can close the SFTP connection by using the exit command.

Connect to a Remote Server with FTP

FTP (File Transfer Protocol) is a standard network protocol used to transfer files between a local machine and a remote server. It is less secure than SFTP, as it transmits data in plain text.

To connect to a remote server using FTP, you can use the ftp command followed by the IP address or hostname of the server. For example:

The username for the ftp is ftpuser and the password is 123456

ftp 127.0.0.1

You will be prompted for the username and password for the server. Once you have entered the correct credentials, you will be connected to the remote server.

Transfer Files with FTP

Once you are connected to the remote server, you can transfer files using the put and get commands.

To upload a file from your local machine to the remote server, use the put command followed by the path to the local file. For example:

## Upload .zshrc file to the server and rename it to zshrc-upload
put /home/labex/.zshrc zshrc-upload

The file will be uploaded to the current directory on the server(/home/ftpuser).

To download a file from the remote server to your local machine, use the get command followed by the path to the remote file. For example:

get zshrc zshrc-download

The file will be downloaded to the current directory on your local machine.

When you are finished transferring files, you can close the FTP connection by using the bye or exit command.

Copying File to Remote Server

SCP (Secure Copy) is a command-line utility that allows you to securely transfer files between a local machine and a remote server, or between two remote servers. It uses SSH (Secure Shell) to encrypt the data being transferred, ensuring that the files are transmitted securely.

The basic syntax for the scp command is as follows:

scp [options] [source] [destination]

The source and destination arguments can be either a local file or directory, or a remote file or directory in the format username@host:path.

For example, to copy a file file.txt from your local machine to a remote server with IP address 127.0.0.1, you would use the following command:

Ths labex user password is 123456.

scp file.txt labex@127.0.0.1:/home/labex/file-scp.txt

Download Files From Remote Server

You can also use scp to download files from remote server.

For example, to download a file file.txt from a remote server with IP address 127.0.0.1 to local, you would use the following command:

scp labex@127.0.0.1:/home/labex/file-scp.txt /home/labex/file-scp-new.txt

You can also set the local directory as the destination, make sure to include the trailing slash (/) in the path.

SCP Advanced Options

scp also has several advanced options that can be used to customize the file transfer. Some of the most commonly used options include:

  • -r - This option allows you to recursively copy entire directories.
  • -C - This option enables compression during file transfer, which can be useful when transferring large files over a slow connection.
  • -P - This option allows you to specify a port number for the connection.

For example, to copy the ~/Desktop directory from local machine to a remote server with IP address 127.0.0.1 using compression and specifying a port number, you would use the following command:

scp -r -C -P 22 ~/Desktop labex@127.0.0.1:/home/labex/Code/

Summary

This lab introduced the Linux SFTP, FTP and SCP commands, which are used to transfer files between a local machine and a remote server or between two remote servers. SFTP is a secure way of transferring files whereas FTP is a standard network protocol which is less secure than SFTP. SCP is a command-line utility that allows you to securely transfer files between a local machine and a remote server or between two remote servers using SSH (Secure Shell) to encrypt the data being transferred. It also covered some advanced options available with the SCP command which can be used to customize file transfer such as -r, -C and -P options.

Other Linux Tutorials you may like