Login

Navigation

This articles is published 277 days ago and last updated 277 days ago, some information may be out of date.

Securely Transfer Files Over The Network

sftp-scp.jpg

Secure File Transfer Protocol (SFTP)

SFTP, or Secure File Transfer Protocol, is a network protocol that provides file access, file transfer, and file management functionalities over any reliable data stream. It is typically used with the SSH protocol to provide secure file transfer, but it can be used with other protocols as well.

Use SFTP:

sftp username@remote-host

This command will initiate an SFTP session with the remote-host. It will be prompted to enter the password for the username connecting with.

Once in the SFTP session, we can use ls to list files, cd to change directories, get to download files, and put to upload files. Here are syntax for reference:

Server ActionsCommand Syntax
Change directorycd /etc/test or cd PATH
List filesls dir
Create directorymkdir directory
Delete directoryrmdir directory
Show current directorypwd
Change groupchgrp groupname PATH
Change ownerchown username PATH
Change permissionschmod 644 PATH (where 644 is an example of permissions)
Create linkln oldname newname
Delete file or directoryrm PATH
Rename file or directoryrename oldname newname
Exit serverexit, bye, or quit
Client ActionsCommand Syntax
Change local directorylcd PATH
List local fileslls
Create local directorylmkdir directory
Show current local directorylpwd
Upload file to serverput [local directory or file] [remote]
Download file from serverget [remote host directory or file] [local machine]
Wildcards supportedUse * or *.rpm in file names for uploading/downloading multiple files

Secure Copy Protocol (SCP)

Secure Copy Protocol (SCP) is a means of securely transferring computer files between a local and a remote host or between two remote hosts. It uses SSH for data transfer and provides the same authentication and security as SSH.

i. (push file) The example of using SCP to transfer files:

scp "/path/to/localFile" username@remote-host:/path/to/remoteDirectory

This command will copy the file localFile from local machine to the remoteDirectory on the remote-host.

ii. (push directory) The example of using SCP to transfer directory, the -r (recursive) option is require:

scp -r /path/to/localDirectory username@remote-host:/path/to/remoteDirectory

iii. (pull file) To copy a file from the remote host to local machine:

scp username@remote-host:/path/to/remoteFile /path/to/localDirectory
OptionDescription
-pPreserves the original file's permissions and metadata.
-rRecursively copies directories and their contents.
-l speedLimits the transfer speed to the specified rate in Kbps. For example, -l 800 limits the speed to 100 KB/s.

Transfer files via TAR on SSH

TAR over SSH provides end-to-end encryption, which means that the contents of the transferred files are protected from unauthorized access. It also allows you to transfer multiple files or directories in a single archive, which can be more efficient than transferring them individually.

tar czf - /path/to/directory | ssh user@remotehost "tar xzf - -C /remote/path"

This command will create a compressed tarball of the directory located at /path/to/directory, and then pipe the output to the ssh command, which will connect to the remote host as user and extract the contents of the tarball to the directory located at /remote/path.

ActionsCommand Syntax
Create compressed tarball of a directorytar czf - /path/to/directory
Pipe output to SSH command``
Connect to remote hostssh user@remotehost
Extract tarball to specify directorytar xzf - -C /remote/path
c: Create new archivetar c
z: Compress with gziptar z
f: Write archive to filetar f
-: Output to stdout instead of filetar -
x: Extract files from archivetar x

Conclusion

These are the fundamentals of using sftp, scp, and tar on Linux. They are robust tools that, once mastered, can greatly aid in managing and transferring files between systems.


Reference