How to connect using SFTP

Dav9862

Carder
Messages
41
Reputation
0
Reaction score
27
Points
8

How to Connect Using SFTP?​

SFTP is a subsystem of SSH. Hence, it supports all SSH authentication methods. While it’s easier to set up and use password authentication, it’s much more convenient and safer to create SSH keys for a passwordless SFTP login.

Follow the steps below to connect with SFTP:

  1. Check your SSH access using one of these commands:ssh user@server_ipaddress
    ssh user@remotehost_domainname
  2. Once that is done, leave the session if no errors occurred.
  3. Initiate an SFTP connection with the following commands:sftp user@server_ipaddress
    sftp user@remotehost_domainname
  4. If you’re using a custom SSH port, use one of these commands to change the SFTP port:sftp -oPort=customport user@server_ipaddress
    sftp -oPort=customport user@remotehost_domainname
  5. Here’s how it should look like:sftp -oPort=49166 [email protected]
Once you’re connected, you will see an SFTP prompt.

How to Transfer Files Using SFTP?​

Transferring Remote Files From a Server to the Local System​

To start, let’s check which local and which remote working directory we are using. To do this, we’ll use these SFTP commands:

sftp> lpwd
Local directory: /LocalDirectory
sftp> pwd
Remote directory: /RemoteDirectory

Now, let’s see how to transfer a file from a remote server to your local machine using the get command. Here’s the basic syntax of the get command:

get /RemoteDirectory/filename.txt

For example, to copy the file /etc/xinetd.conf from the remote server to your local machine, you would use:

get /etc/xinetd.conf

Once the download is complete, you can now find that the file xinetd.conf is in the /user/home directory of your local machine.

To download multiple files with SFTP, use the mget command. To download all files in a directory called /etc that have the .conf extension to your current working directory, you will use the following command:

mget /etc/*.conf

After the download, you can find all *.conf files in /user/home directory of your local machine.

Transferring Files From the Local Machine to a Remote Server​

To copy a file from the local machine to the remote server, we’ll use the get command again. In this case, the syntax of get command will be:

get file.txt /RemoteDirectory

To move the file example.txt from a local machine to the remote machine, enter the following command:

put /home/user-name/example.txt /root

Now we will find the file in the remote server’s root directory. You can also try transferring multiple files using the mput command. It works nearly the same as mget:

mput /home/user-name/*.txt /root

This command would move all files with the .txt extension in the /home/user-name from the local machine to the remote /root directory.

NOTE: Keep in mind that to download and upload the files with SFTP, you will need to type the command put or get and press the TAB key.



Commands for Navigating With SFTP​

Some commands can be used to navigate through the remote and local servers more efficiently with SFTP. They’re similar to the ones you’d use in the Linux shell prompt.

For example, the pwd command is always useful to let you know in which working directory you are currently on.

sftp> pwd
Remote directory: /RemoteDirectory

or

sftp> lpwd
Local directory: /LocalDirectory

You can also display the list of files and directories you’re using for the remote directory:

ls

Similarly, for the local working directory:

lls

For instance, the output will look similar to this:

Pictures Templates Media Text.txt Documents

To switch from one remote working directory to another local working directory, enter the following commands:

cd name_of_directory
lcd name_of_directory

Finally, use the ! and exit commands to go back to the local shell and quit SFTP.
 
Top