- Published on
Transferring Files Using SCP and SFTP on Linux
Continuing from my previous tutorial on SSH Server Configuration, OpenSSH also provides tools that function similarly to rcp and ftp, namely scp and sftp. These tools have the advantage of encrypting all connections between systems, making the connection and data transmission more secure and protected.
scp is used for encrypted remote file copying, while sftp is used for encrypted FTP connections.
Example of using scp:
$ scp ./filename.txt [email protected]:/home/coba/filename.txt
The command above will copy the file filename.txt from the local directory on the local system to the home directory of the user coba on the remote server example.web.id, using the account coba. To copy the file in the opposite direction, meaning copying the file /home/coba/filename.txt from the remote server to the local system, the command used would be:
$ scp [email protected]:/home/coba/filename.txt ./filename.txt
The scp command has several options that can be used. Here are those options:
-p
: Preserves the modification times, access times, and modes from the original file when copying.
-r
: Copies directories recursively. Useful for copying an entire directory and its contents.
-C
: Enables SSH compression mode.
You cannot use the -p option to preserve file ownership when using scp. So when the file is copied to another system, it will be owned by the user who logged into that system.
Another tool provided by OpenSSH is sftp. sftp functions identically to ftp. To initiate an sftp connection to a remote server, the client needs to run an sftp connection.
Running sftp:
$ sftp [email protected]
When you successfully log in as an sftp user, you can perform ftp commands such as put, ls, and cd. By default, sftp will connect to SSH using port 22. To use sftp with a different port, the sftp command would look like this:
$ sftp -P 1234 [email protected]
That's it, see ya~