In this blog we are going to take a look at SFTP or Secure File Transfer Protocol. SFTP is basically “SSH File Transfer Protocol!” And it transfers the data securely by using encryption. SFTP works as a subsystem of SSH and shares the same port. What SSH is configured for which is port number 22 by default.
It is important to note that SFTP is not similar to the “FTP Protocol” rather it’s a different protocol. It simply uses an FTP style of interaction between a client and an sshd server, OK!
So to connect to a remote system via SFTP we use following command:
To list the contents use the ‘ls’ command.
To change to the directory use ‘cd’ or ‘chdir’ followed by the directory name.
To list current working directory use ’pwd’ and to make a directory use ‘mkdir <dir name>’
To copy a file from remote machine to cthe current achine where you are connected with SFTP use the following command:
Sftp> put remote directory/file.txt
To copy files from the current machine to remote machine use sftp command:
Sftp> get file.txt/remotedirectory
For Example: file1.txt is on the system where I am connected with SFTP and I am sending it to my other server:
Please understand that what files you can upload and download just type the command ‘put’ or ‘get’ and press “TAB key”.
To upload you use ‘put’ and to download you use ‘get’ command.
To delete a file use ‘rm file.txt’
To copy recursively use –r option as follows:
Sftp> get –r dir1/ /home/xyz/
Let’s see a demo:
It will require 2 machines. With IP address command you will come to know the IP address of the first machine, similarly you can know the IP address of another machine. Consider that the IP address of the first machine is 1.11 and IP address of 2nd machine is 1.15.
Now to connect these 2 machines what I am going to use is a SFTP command followed by the user – root and connecting this with a root. You can connect with any user and whenever you are going to go into his or land in his home directory, so to prove that lets list the content using ‘ls’ command. It will show you the contents on the second machine also if you run the ‘ls’ command it will show you same contents like first machine.
This means right now we are in roots’ home directory. So let’s say if you connect with a user test then you are going to land in his home directory.
Let me tell you what we can do with this SFTP Protocol. So let’s say I want to put a file or let’s talk about make directory i.e. mkdir command used to make the directory test2. You can see the contents using ‘ls’ command. On the second machine also you will not see anything on the second machine. In this case it is required to upload a file. So use the ‘put’ command and press the tab key.
You will see the rest of files from which you can upload a file.
So upload the test2.txt file from machine1 as follows:
put ‘tab key’ <file name>
When you will run ‘ls’ command on the second machine you will see the contents. If you want to remove a file, use ‘rm’ command.
If you check it on a second machine, you will see that the file is removed.
Let’s say if you want to download some files assume that I have created a text file in 1.11 machine and if you check it in 1.15 it will not be present there. So download this file using ‘get’ command as follows:
get press tab key
Let’s check it whether that file has been downloaded or not on 1.15 machine using ‘ls’ command, you will find that the file is present.
Let’s discuss how a directory can be uploaded using its contents.
Consider there is a directory last2.txt.
SFTP works in such a way that when you are going to upload the file you should have the same directory name in the remote system also. What does it mean by?
For example: assume that if you want to upload a directory with its contents but remember that it should have the same directory name in the remote screen / machine also, otherwise it will give you an error.
So let’s try to upload that directory using the following command:
put –r test2.txt
It will upload the files on the second machine. Type ‘ls’ command and you will see that the content is visible using ‘ls’ command.
Thus we have proved that while uploading a directory it should have the same name as in the local and remote machine. But for downloading you don’t need anything like that.
This is it! I hope after reading this blog you must have learned something from it. You are always welcome and if you want to leave any comment please leave it in the comment section below.
Please fill in your email address to get most latest updates.