FTP is the standard method for transferring files or other data between computers, but this method is becoming increasingly obsolete in security-demanding environments. This is exactly why SFTP is needed, as it is especially more secure for VPS hosting users.
In this guide, we'll demonstrate how to use SFTP to transfer files to secure hosting, as well as talk about some other useful commands and explain more about how it works.
How to connect using SFTP
SFTP is a subsystem of SSH. Therefore, it supports all SSH authentication methods. Although the method of using username and password is quite familiar to everyone, but if you set up an SSH key for passwordless SFTP login, it will be much more convenient and secure. a subsystem of SSH.
You can also check out this tutorial for how to set up SSH Keys. Once done, follow the steps below to connect to SFTP. To connect via SFTP on a Linux machine, you will need to go through the following steps:
- Test the SSH connection with one of the following commands:
ssh user@server_ipaddress
ssh user@remotehost_domainname - Then exit the session if no error is found.
- Establish an SFTP connection with the following command:
sftp user@server_ipaddress
sftp user@remotehost_domainname - If you are using a private SSH port, use the following command to change the SFTP port:
sftp -oPort=customport user@server_ipaddress
sftp -oPort=customport user@remotehost_domainname - The command will look like this:
sftp -oPort=49166 user@31.220.57.32
After the connection is successful, you will see the SFTP message.
How to transfer files using SFTP
In this section, we will show you how to transfer files from remote to local using SFTP and vice versa.
Note: You can transfer files using SFTP clients, like WinSCP or FileZilla. If you like FileZilla, check out the instructions here.
Transfer files from remote server to local machine
First, check what local and remote directories we are using with the following SFTP command:
sftp> lpwd
Local directory: /LocalDirectory
sftp> pwd
Remote directory: /RemoteDirectory
Here we will show you how to transfer remote files to local system and vice versa
Now, let's see how to transfer files from remote server to local machine with the command get. This is the basic syntax of the command get:
get /RemoteDirectory/filename.txt
For example, to copy files /etc/xinetd.conf from remote server to local machine you would use:
get /etc/xinetd.conf
Once the download is complete, you should now be able to see the file xinetd.conf located in folder /user/home of the internal machine.
To download multiple files using SFTP, use the command mget. To download all files in a folder named /etc with extension .conf to the current directory, you would use the following command:
mget /etc/*.conf
After downloading you can find all the files *.conf in folder /user/home of the internal machine.
Transfer files from local machine to remote server/virtual server hosting
To copy files from the local machine to the remote server or host the virtual server, we will use the command again get. In this case, the command's syntax get will:
get file.txt /RemoteDirectory
To move files example.txt from the local machine to the remote machine, enter the following command:
put /home/user-name/example.txt /root
Now we will see the files in the root directory of the remote server. You can also try transferring multiple files with the command mput. It works almost like mget:
mput /home/user-name/*.txt /root
This command will move all files with extension .txt in folder /home/user-name from local machine to directory /root from far away.
NOTE: Remember that to download and upload files using SFTP you need to enter the command put or get and press the key TAB.
Several commands can be used to navigate between remote and local machines when using SFTP. They are similar to when you use the Linux shell command.
For example, the command pwd will be used to indicate what the current directory is.
sftp> pwd
Remote directory: /RemoteDirectory
Or
sftp> lpwd
Local directory: /LocalDirectory
You can display the list of files and folders on the remote directory with the command:
ls
Similarly, with the local directory, use the command to list the files:
lls
The result will be as follows:
Pictures Templates Media Text.txt Documents
To switch between the remote directory and the local directory, enter the following command:
cd name_of_directory
lcd name_of_directory
Finally, use the command ! and exit to return to the local shell and exit SFTP.
Basic file operations in SFTP
SFTP also helps you manage folders and files with commands.
To check the capacity of the remote server in gigabytes, use the command df as follows:
df -h
The results you will see:
Filesystem Size Used Avail Use% Mounted on
/dev/ploop29212p1 59G 2.5G 56G 5% /
none 1.5G 0 1.5G 0% /sys/fs/cgroup
none 1.5G 0 1.5G 0% /dev
tmpfs 1.5G 0 1.5G 0% /dev/shm
tmpfs 1.5G 568K 1.5G 1% /run
tmpfs 308M 0 308M 0% /run/user/0
Use command mkdir to create a new directory on both local and remote servers:
mkdir name_of_directory
lmkdir name_of_directory
You can delete a directory from a remote server with the command rmdir:
rmdir name_of_directory
To rename the file from the remote host, use the following command:
rename filename new_filename
Here is an example:
rename Old_FileExample New_FileExample
If you want to delete the remote file, use the command rm:
rm filename
And the order chown used to change the file owner:
chown userid filename
userid maybe username or numeric user ID. Eg:
chown UserOne FileExample
chown 1234 FileExample
chgrp used to change the group owner of the file:
chgrp groupid filename
Eg:
chgrp NewGroup FileExample
Finally, you may need to use chmod To set file permissions:
chmod 764 FileExample
In the example above, the three digits represent user, group, and other of files.
For rights read (r), write (w), and execute (x), its corresponding value is 4, 2, 1. 0 can also be used to set the file without any permissions.
To assign file permissions, simply add up the values of the above permissions. Here is a way to make it easier for you to understand:
chmod ugo FileExample
# u đại diện cho User người có quyền đọc, viết và thực thi.
# g là Group, tại đây chúng tôi gán quyền write và excute file (ghi và thục thi).
# o là Others, chỉ có quyền đọc.
List of useful SFTP commands
If you need a quick cheat sheet, here is a list of all the available SFTP commands. You can find this list yourself by entering the command help or ? – both will prompt the same result.
bye Thoát sftp
cd path Thay đổi thư mục từ xa thành 'path'
chgrp [-h] grp path Thay đổi nhóm file 'path' thành 'grp'
chmod [-h] mode path Thay đổi quyền của file 'path' thành 'mode'
chown [-h] own path Thay đổi sở hữu file 'path' thành 'own'
df [-hi] [path] Hiển thị số liệu thống kê cho thư mục hiện tại hoặc hệ thống file có
chứa 'path'
exit Thoát sftp
get [-afpR] remote [local] Download file
help Hiển thị văn bản trợ giúp này
lcd path Thay đổi thư mục nội bộ thành 'path'
lls [ls-options [path]] Hiển thị danh sách thư mục nội bộ
lmkdir path Tạo thư mục nội bộ
ln [-s] oldpath newpath Liên kết file từ xa (-s cho symlink)
lpwd In thư mục làm việc nội bộ
ls [-1afhlnrSt] [path] Hiển thị danh sách thư mục từ xa
lumask umask Đặt local umask thành 'umaks'
mkdir path Tạo thư mục từ xa
progress Chuyển đổi hiển thị của đồng hồ đo tiến độ
put [-afpR] local [remote] Upload file
pwd Hiển thị thư mục làm việc từ xa
quit Thoát sftp
reget [-fpR] remote [local] Tiếp tục tải file
rename oldpath newpath Đặt lại tên file
reput [-fpR] local [remote] Tiếp tục tải file lên
rm path Xóa file từ xa
rmdir path Xóa thư mục từ xa
symlink oldpath newpath File từ xa Symlink
version Hiển thị phiên bản SFTP
!command Thực thi 'command' trong local shell
! Thoát khỏi local shell
What is SFTP?
SFTP, short for SSH File Transfer Protocol, is a much safer way to transfer files to the server or vice versa. Use SSH protocol, it supports encryption and other security methods to better protect file transfers. It is the only secure file transfer protocol that protects against attacks at any point in the data transmission. This makes it the preferred protocol among professionals.
During file transfer, all data is divided into packets and sent over the same secure protocol.
Sensitive information will be encrypted and no one can read it during the transfer from the client to the server/or hosting. In other words, the original content in plaintext (readable text) will be replaced by ciphertext (ciphertext).
Only the recipient with the decryption key will be able to see the original content. This prevents any unauthorized access to the file.
FTP has two channels for exchanging different data – command channel and data channel. In contrast, SFTP has only one encrypted channel where data is exchanged in encrypted, formatted packets.
Epilogue
There are quite a few basics on how to use SFTP for secure file transfers. We hope this guide has demonstrated how useful it is and its basic usage. However, if you need more information about FTP, you can find more tutorials here.
If you have any more questions about SFTP, don't hesitate to leave a comment below.
Post a Comment
Post a Comment