cicid/blog/2019-05-29-blog-24.md
2024-11-05 16:27:23 +05:30

331 lines
15 KiB
Markdown

---
slug: Easy and Secure File Transfers A Simple Guide to Using SFTP with a Remote Server
title: Easy and Secure File Transfers A Simple Guide to Using SFTP with a Remote Server
authors: [slorber]
tags: [hola, docusaurus]
---
<!-- truncate -->
import CodeBlock from '@site/src/components/CodeBloack';
<div className="head">Introduction</div>
<div className="text">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.</div><br/>
<div className="text">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.</div><br/>
<div className="text">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.</div><br/>
<div className="text">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.</div><br/>
<div className="head">SFTP Connection Made Easy: A Step-by-Step Guide</div>
<div className="text">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.</div><br/>
<div className="text">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.</div><br/>
<div className="text">Setting up SSH keys is like giving your computer a secure passcode. If you've done that, good job!</div><br/>
<div className="text">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.</div>
<CodeBlock code={`ssh sammy@your_server_ip_or_remote_hostname`} /><br/>
<div className="text">If everything is good, you can finish by typing:</div>
<CodeBlock code={`exit`} /><br/>
<div className="text">Now, you can start using SFTP by typing:</div>
<CodeBlock code={`sftp sammy@your_server_ip_or_remote_hostname`} /><br/>
<div className="text">After typing this command, you'll be connected to the other system, and your screen will show an SFTP prompt.</div><br/>
<div className="text">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:</div>
<CodeBlock code={`sftp -oPort=custom_port sammy@your_server_ip_or_remote_hostname`} /><br/>
<div className="text">This will link you to the other system using the port you chose.</div><br/>
<div className="head">Need Help with SFTP? Here's What to Do</div>
<div className="text">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:</div>
<CodeBlock code={`help`} /><br/>
OR
<CodeBlock code={`?`} /><br/>
<div className="text">This will show you a list of all the things you can do.</div>
<CodeBlock code={`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'
. . .
`} /><br/>
<div className="text">We'll look at some of these commands in the next sections.</div>
<div ClassName="head">SFTP Basics: Exploring Your Files</div>
<div className="text">We can move around the remote system's folders using commands that work like those in a regular computer.</div><br/>
<div className="text">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:</div>
<CodeBlock code={`pwd`} /><br/>
<CodeBlock code={`Output
Remote working directory: /home/demouser
`} /><br/>
<div className="text">We can check what's inside the current folder on the remote system using a command you're already familiar with:</div>
<CodeBlock code={`ls`} /><br/>
<CodeBlock code={`Output
Summary.txt info.html temp.txt testDirectory
`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`ls -la`} /><br/>
<CodeBlock code={`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
. . .
`} /><br/>
<div className="text">To go to another folder, just type this command:</div>
<CodeBlock code={`cd testDirectory`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`lpwd`} /><br/>
<CodeBlock code={`Output
Local working directory: /Users/demouser
`} /><br/>
<div className="text">We can see what's in the current folder on our own computer by typing:</div>
<CodeBlock code={`lls`} /><br/>
<CodeBlock code={`Output
Desktop local.txt test.html
Documents analysis.rtf zebra.html`} /><br/>
<div className="text">We can also switch to a different folder on your computer like this:</div>
<CodeBlock code={`lcd Desktop`} /><br/>
<div className="head">SFTP Made Simple: How to Transfer Files with Ease</div>
<div className="text">If you want to bring files from your other computer to yours, you can use the 'get' command:</div>
<CodeBlock code={`get remoteFile`} /><br/>
<CodeBlock code={`Output
Fetching /home/demouser/remoteFile to remoteFile
/home/demouser/remoteFile 100% 37KB 36.8KB/s 00:01
`} /><br/>
<div className="text">When you use the 'get' command, it downloads a file from the other computer and gives it the same name on your computer.</div>
<div className="text">But if you want to give it a different name, just type the name you want after the 'get' command:</div>
<CodeBlock code={`get remoteFile localFile`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`get -r someDirectory`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`get -Pr someDirectory`} /><br/>
<div className="text" style={{ fontSize:'28px' }}>Putting Files on the Remote System: A Step-by-Step Guide</div>
<div className="text">Sending files to the other computer is just as easy. Instead of 'get,' you use the 'put' command:</div>
<CodeBlock code={`put localFile`} /><br/>
<CodeBlock code={`Output
Uploading localFile to /home/demouser/localFile
localFile 100% 7607 7.4KB/s 00:00
`} /><br/>
<div className="text">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':</div>
<CodeBlock code={`put -r localDirectory`} /><br/>
<div className="text">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.</div>
<CodeBlock code={`df -h`} /><br/>
<CodeBlock code={`Output
Size Used Avail (root) %Capacity
19.9GB 1016MB 17.9GB 18.9GB 4%
`} /><br/>
<div className="text">Something to remember is that the 'df' command doesn't have a local version. But, you can still do it by using the '!' command.</div><br/>
<div className="text">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:</div>
<CodeBlock code={`!`} /><br/>
<div className="text">and then</div>
<CodeBlock code={`df -h`} /><br/>
<CodeBlock code={`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
`} /><br/>
<div className="text">You can use any other command as usual in your own computer. To go back to working with SFTP, just type:</div>
<CodeBlock code={`exit`} /><br/>
<div className="text">You'll see the SFTP prompt come back now.</div><br/>
<div className="head">Quick File Changes: Getting Started with SFTP</div>
<div className="text">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:</div>
<CodeBlock code={`chown userID file`} /><br/>
<div className="text">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.</div><br/>
<div className="text">Instead, you can look in a file called '/etc/passwd' on the computer. This file connects usernames with UIDs in most Linux systems.</div>
<CodeBlock code={`get /etc/passwd
!less passwd
`} /><br/>
<CodeBlock code={`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
. . .
`} /><br/>
<div className="text">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.</div><br/>
<div className="text">The UID you need is in the third part of each line in the file, separated by colons.</div><br/>
<div className="text">Similarly, you can change which group owns a file using this command:"</div>
<CodeBlock code={`chgrp groupID file`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`get /etc/group
!less group
`} /><br/>
<CodeBlock code={`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:
. . .
`} /><br/>
<div className="text">The third column shows the group ID linked to the name in the first column. That's what we need to know.</div><br/>
<div className="text">The 'chmod' command in SFTP works just like it does on your computer's files:</div>
<CodeBlock code={`chmod 777 publicFile`} /><br/>
<CodeBlock code={`Output
Changing mode on /home/demouser/publicFile
`} /><br/>
<div className="text">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.</div><br/>
<div className="text">You can do this using the 'lumask' command:</div>
<CodeBlock code={`lumask 022`} /><br/>
<CodeBlock code={`Output
Local umask: 022
`} /><br/>
<div className="text">Now, whenever you download regular files (unless you use the -p flag), they'll automatically have permissions set to 644. </div><br/>
<div className="text">You can also make directories on your computer with 'lmkdir' and on the other computer with 'mkdir'.</div><br/>
<div className="text">The other file commands we'll talk about are only for the other computer's files.</div>
<CodeBlock code={`ln
rm
rmdir
`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`!`} /><br/>
<div className="text">To run a command on your computer, just put '!' before the command. Like this:</div>
<CodeBlock code={`!chmod 644 somefile`} /><br/>
<div className="text">When you're done with SFTP, just type 'exit' or 'bye' to finish and close the connection.</div>
<CodeBlock code={`bye`} /><br/>
<div className="con">Conclusion:</div>
<div className="text">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.</div><br/>
<div className="text">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.</div><br/>
<div className="text"><strong>Ready to explore more? Check our tutorial for additional insights!</strong></div>