Skip to main content

Easy and Secure File Transfers A Simple Guide to Using SFTP with a Remote Server

· 11 min read
Yashwin Shankar
Front End Developer @ Tech4Biz Solutions Pvt Ltd.
Introduction
FTP, which stands for File Transfer Protocol, used to be a common way to move files between computers. However, it's not very safe because it doesn't protect your files during the transfer. In 2022, most new software won't use it much anymore because it's not secure. People now prefer more secure methods. You might still find FTP in some old programs, but it's kind of like an outdated tool, and modern programs use safer ways to transfer files.

SFTP or Secure File Transfer Protocol, is like a superhero version of regular file transfer. It's bundled with something called SSH, which makes sure your files are safe during the transfer. Imagine it as a super-secure way to share files between computers. It can do the same things as regular file transfer, but it's like the upgraded, safer version. You can use it whenever you need to share files securely, just like how you used to use regular file transfer.

SFTP is a safer way to share files compared to regular FTP. It has extra security features because it works with something called SSH. Think of SFTP as a very secure method to exchange files between computers. Regular FTP is not as safe and should only be used carefully or on networks you really trust.

Even though you can use SFTP with easy-to-use programs, we'll show you how to use its basic, command-line version. It's like learning the special abilities of SFTP through simple commands.

SFTP Connection Made Easy: A Step-by-Step Guide
By default, SFTP uses the SSH protocol to verify and create a secure connection. This means it has the same ways of checking your identity as SSH.

While you can log in with a password, it's safer and more convenient to use something called 'SSH keys.' Think of these keys like a special passcode only your computer understands. It's a more secure way, and in the long run, it can be easier for you. We recommend using SSH keys for better security and a smoother experience.

Setting up SSH keys is like giving your computer a secure passcode. If you've done that, good job!

Now, if you can use SSH to connect to your computer (think of it like sending a message to your computer), you're ready to use SFTP to handle your files. To check, just use a command to make sure your computer can communicate with the other one.
ssh sammy@your_server_ip_or_remote_hostname

If everything is good, you can finish by typing:
exit

Now, you can start using SFTP by typing:
sftp sammy@your_server_ip_or_remote_hostname

After typing this command, you'll be connected to the other system, and your screen will show an SFTP prompt.

If you're using a different port number to communicate with your system (not the usual port 22), you can open the SFTP connection like this:
sftp -oPort=custom_port sammy@your_server_ip_or_remote_hostname

This will link you to the other system using the port you chose.

Need Help with SFTP? Here's What to Do
The first command you should know is 'help.' It's like asking for directions in SFTP. To use it, just type either of these in the prompt:
help

OR

?

This will show you a list of all the things you can do.
Output
Available commands:
bye                                Quit sftp
cd path                            Change remote directory to 'path'
chgrp grp path                     Change group of file 'path' to 'grp'
chmod mode path                    Change permissions of file 'path' to 'mode'
chown own path                     Change owner of file 'path' to 'own'
df [-hi] [path]                    Display statistics for current directory or
                                 filesystem containing 'path'
exit                               Quit sftp
get [-Ppr] remote [local]          Download file
help                               Display this help text
lcd path                           Change local directory to 'path'
. . .

We'll look at some of these commands in the next sections.
SFTP Basics: Exploring Your Files
We can move around the remote system's folders using commands that work like those in a regular computer.

Let's start by figuring out where we are right now on the remote system. Just like when you check where you are on your own computer, you can type this to see the current folder:
pwd

Output
Remote working directory: /home/demouser

We can check what's inside the current folder on the remote system using a command you're already familiar with:
ls

Output
Summary.txt     info.html       temp.txt        testDirectory

Keep in mind that SFTP commands are a bit different from what you might be used to, and they don't have all the advanced features. However, you can still do important things. For instance, if you add -la to 'ls,' it shows more details about files, like when they were changed and who can access them:
ls -la

Output
drwxr-xr-x    5 demouser   demouser       4096 Aug 13 15:11 .
drwxr-xr-x    3 root     root         4096 Aug 13 15:02 ..
-rw-------    1 demouser   demouser          5 Aug 13 15:04 .bash_history
-rw-r--r--    1 demouser   demouser        220 Aug 13 15:02 .bash_logout
-rw-r--r--    1 demouser   demouser       3486 Aug 13 15:02 .bashrc
drwx------    2 demouser   demouser       4096 Aug 13 15:04 .cache
-rw-r--r--    1 demouser   demouser        675 Aug 13 15:02 .profile
. . .

To go to another folder, just type this command:
cd testDirectory

Now that we've explored the files on the other computer, what if we want to check our own files? It's easy! Just add an 'l' before the command. Every command we've talked about has a local version. For instance, to see where you are in your own computer, type:
lpwd

Output
Local working directory: /Users/demouser

We can see what's in the current folder on our own computer by typing:
lls

Output
Desktop			local.txt		test.html
Documents		analysis.rtf		zebra.html

We can also switch to a different folder on your computer like this:
lcd Desktop

SFTP Made Simple: How to Transfer Files with Ease
If you want to bring files from your other computer to yours, you can use the 'get' command:
get remoteFile

Output
Fetching /home/demouser/remoteFile to remoteFile
/home/demouser/remoteFile                       100%   37KB  36.8KB/s   00:01

When you use the 'get' command, it downloads a file from the other computer and gives it the same name on your computer.
But if you want to give it a different name, just type the name you want after the 'get' command:
get remoteFile localFile

The 'get' command can do more! For example, if you want to copy a whole folder and everything inside it, just add the word 'recursive' after the 'get' command:
get -r someDirectory

To keep things organized, use the '-P' or '-p' flag with the command. This makes sure that the files keep their proper permissions and access times:
get -Pr someDirectory

Putting Files on the Remote System: A Step-by-Step Guide
Sending files to the other computer is just as easy. Instead of 'get,' you use the 'put' command:
put localFile

Output
Uploading localFile to /home/demouser/localFile
localFile                                     100% 7607     7.4KB/s   00:00

You can use the same tricks for 'put' as you did with 'get.' For example, to copy a whole folder from your computer, just type 'put -r':
put -r localDirectory

A helpful tool for checking if you have enough space for downloads and uploads is the 'df' command. It's like a tool you might use on your computer. With this, you can make sure you have enough room for the files you want to transfer.
df -h

Output
  Size     Used    Avail   (root)    %Capacity
19.9GB   1016MB   17.9GB   18.9GB           4%

Something to remember is that the 'df' command doesn't have a local version. But, you can still do it by using the '!' command.

The '!' command takes you to your own computer's command area. Here, you can run any command you normally would on your computer. To check disk usage, just type:
!

and then
df -h

Output
Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   595Gi   52Gi  544Gi     9%    /
devfs          181Ki  181Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home

You can use any other command as usual in your own computer. To go back to working with SFTP, just type:
exit

You'll see the SFTP prompt come back now.

Quick File Changes: Getting Started with SFTP
With SFTP, you can do some basic file management tasks. For example, you can change who owns a file on the other computer by using this command:
chown userID file

Unlike the regular 'chmod' command, SFTP doesn't use usernames but something called UIDs. Unfortunately, you can't find out the UID directly from SFTP.

Instead, you can look in a file called '/etc/passwd' on the computer. This file connects usernames with UIDs in most Linux systems.
get /etc/passwd
!less passwd

Output
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
. . .

Remember how we used the '!' command before? We used it before typing a command for our computer. It lets us run any command on our computer while still using SFTP. We could have used it with the 'df' command earlier to check disk space on our computer.

The UID you need is in the third part of each line in the file, separated by colons.

Similarly, you can change which group owns a file using this command:"
chgrp groupID file

Once more, there isn't a direct way to see all the groups on the other computer. But you can use this command to get a workaround:
get /etc/group
!less group

Output
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
. . .

The third column shows the group ID linked to the name in the first column. That's what we need to know.

The 'chmod' command in SFTP works just like it does on your computer's files:
chmod 777 publicFile

Output
Changing mode on /home/demouser/publicFile

You can't directly change permissions for files on your own computer using SFTP. But, you can adjust something called 'umask' to make sure new files you copy over have the right permissions.

You can do this using the 'lumask' command:
lumask 022

Output
Local umask: 022

Now, whenever you download regular files (unless you use the -p flag), they'll automatically have permissions set to 644.

You can also make directories on your computer with 'lmkdir' and on the other computer with 'mkdir'.

The other file commands we'll talk about are only for the other computer's files.
ln
rm
rmdir

These commands do the same things as the ones you use on your computer. If you ever need to do these actions on your own files, just remember you can switch to your computer's command area by typing:
!

To run a command on your computer, just put '!' before the command. Like this:
!chmod 644 somefile

When you're done with SFTP, just type 'exit' or 'bye' to finish and close the connection.
bye

Conclusion:
SFTP might not have all the features of modern tools, but it's handy for certain tasks, especially if you're familiar with old FTP ways or need to limit what someone can do on your computer.

For example, you can let specific users transfer files without giving them full access to your computer. If you want to learn how, check our tutorial on 'How To Enable SFTP Without Shell Access.'If you're used to FTP or SCP, SFTP combines the best of both. It might not fit every situation, but it's a versatile tool to have.

Ready to explore more? Check our tutorial for additional insights!