Monday, February 20, 2017

sftp in Linux and Unix with examples

In modern world information is really precious and and transfer of information ( and data containing this information ) is really very important , today i am going to discuss sftp which is basic building block of data transfer and used widely. sftp stands for "secure file transfer protocol "

lets try to understand sftp with examples . I am logged in to one of my server with IP address 192.168.246.137 with root account. I want to make an sftp connection to another server with IP address 192.168.92.129

First requirement of making an sftp connection is to make sure that target server is reachable from source server



in order to make an sftp connection , i have to use "sftp Target-Server-IP-address"
Since i was making this connection first time it asked me a question "Are you sure you want to continue connecting (yes/no)?" to which i replied yes , and as result 192.168.92.129 was permanently added to the list of know hosts

One thing must be noticed here i did not specify any user name for while making sftp connection"

But still i was able to make a successful sftp connection . Reason ? when i did not specify username for sftp it defaulted to current user on current machine ( which was root ) . now obliviously  root account also exists on remote machine and the password i entered was of root account of remote machine and it worked

 if you want to use an specific account ( on remote machine ) , you must specify it explicitly , like below




once you are able to sftp successfully, you get sftp prompt, you can change directory using cd command,
you can collect a file from remote server using "get" command on sftp prompt ( here i used "get sftp-linux-example.txt" ) , if you want to collect multiple files at one go you need to use something mightier called "mget".  similarly sftp has two more useful weapons  in its arsenal  one is "put" to send a file on remote server and its elder sibling "mput" to send multiple files at once ( hint : you can you use wild card pattern with the help of * )

 


 To list files on remote server you can use ls command on sftp prompt, pwd command shows the current working directory on sftp  prompt, you can even know the current working directory of  source host using "lpwd" command on sftp prompt, refer below examples



To See all available command on sftp prompt type "help"



 We all know that True power of  Unix and Linux systems lies in ability to get things automated  using shell scripts.In order to use sftp efficiently inside shell script we have to provide predefined set of instructions to sftp. sftp has something called "batch mode" or -b option , which is quite useful for this purpose

 but it should be used with password less authentication or better we call it non interactive ssh authentication . Now in order to setup this type of authentication to you need to follow certian simple steps ( though cryptography is huge topic in itself, i will be just explaining basis steps for ssh key generation for now )
1. login to host system which you will be using to  make sftp connection, you need to run "ssh-keygen -t rsa"
command  to generate a public-private key pair, once  you run this command it will ask you for passphrase, for a basic authentication less connection , you can provide blank passphrase ( though there is a more secure alternative with non blank passphrase - The SSH agent  , also note that i was already having existing key pair, which i have overwritten in order to show you the process , you may need to choose your options more wisely )




2. Now go to .ssh directory and you can see a private key and public key, copy the public key to your target server




 

 3.  login to target server and append the pubic key to .ssh/authorized_keys file.
you may also need to provide appropriate permissions to .ssh directory and authorized_keys, but remember "Dont use 777"



i have a file MBATCH.txt on my server which will connect to target ( remote server )  which contains some instructions ( one mput and one bye commands )



So i used it with sftp -b, as shown in following example ,



See i was able to transfer the files only by specifying batch file name having instruction

 One more important thing to remember, by default sftp uses TCP port number 22



if you want to specify some other port number you can specify it using "-oPort" option

I hope you all enjoyed this post, thanks for reading,

Visit other posts - 

 

if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com