How Do I Use Ssh to Upload Files to a Server
Mar 09, 2022
Edward S.
6min Read
How to Employ SFTP (SSH File Transfer Protocol)
FTP is the standard method of transferring files or other data between computers, but it's becoming more and more outdated in today'south security-conscious environment. Fortunately, that's where SFTP comes in, which is particularly useful for VPS hosting users.
In this guide, we're going to show how to utilise SFTP for secure file transfer, talk near another useful commands and elaborate more on how it works.
Download Ultimate SSH Commands Cheat Sheet
How to Connect Using SFTP?
SFTP is a subsystem of SSH. Hence, information technology supports all SSH authentication methods. While it's easier to prepare and use password authentication, it's much more convenient and safer to create SSH keys for a passwordless SFTP login.
You lot can bank check this tutorial on how to set up your SSH keys. Once you're set, follow the steps beneath to connect with SFTP:
- Cheque your SSH access using one of these commands:
ssh user@server_ipaddress ssh user@remotehost_domainname
- Once that is washed, leave the session if no errors occurred.
- Initiate an SFTP connection with the following commands:
sftp user@server_ipaddress sftp user@remotehost_domainname
- If yous're using a custom SSH port, utilize one of these commands to change the SFTP port:
sftp -oPort=customport user@server_ipaddress sftp -oPort=customport user@remotehost_domainname
- Here's how information technology should look like:
sftp -oPort=49166 user@31.220.57.32
In one case you're connected, y'all will see an SFTP prompt.
How to Transfer Files Using SFTP?
Here we're going to bear witness you how to transfer remote files to the local system using SFTP and vice versa.
NOTE: Y'all can also transfer your files using SFTP clients, such as WinSCP or FileZilla. If you're interested in the latter, check our tutorial here.
Transferring Remote Files From a Server to the Local Organization
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'due south see how to transfer a file from a remote server to your local motorcar using the get command. Here'southward 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 auto, y'all would utilise:
get /etc/xinetd.conf
In one case the download is complete, you lot can now observe that the file xinetd.conf is in the /user/home directory of your local motorcar.
To download multiple files with SFTP, use the mget command. To download all files in a directory chosen /etc that have the .conf extension to your current working directory, you will use the following command:
mget /etc/*.conf
After the download, yous can detect 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 employ the get command again. In this case, the syntax of get command will be:
get file.txt /RemoteDirectory
To motion the file example.txt from a local machine to the remote car, enter the following command:
put /home/user-name/instance.txt /root
At present we will find the file in the remote server'due south root directory. You tin can as well attempt transferring multiple files using themput command. Information technology works near the same as mget:
mput /dwelling/user-proper name/*.txt /root
This control would move all files with the .txt extension in the /domicile/user-name from the local auto to the remote /root directory.
Note: Keep in mind that to download and upload the files with SFTP, yous will need to type the command put or get and printing the TAB key.
Commands for Navigating With SFTP
Some commands tin can exist used to navigate through the remote and local servers more efficiently with SFTP. They're like to the ones yous'd use in the Linux shell prompt.
For instance, the pwd command is always useful to permit you know in which working directory yous 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 example, the output will look like 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 go out commands to get back to the local vanquish and quit SFTP.
Basics of File Maintenance Using SFTP
With SFTP, you tin can likewise manage directories and files using specific commands.
To bank check the remote server's disk space in gigabytes, use the df function like so:
df -h
Here's an output example:
Filesystem Size Used Avail Use% Mounted on /dev/ploop29212p1 59G 2.5G 56G 5% / none 1.5G 0 1.5G 0% /sys/fs/cgroup none 1.5G 0 one.5G 0% /dev tmpfs 1.5G 0 ane.5G 0% /dev/shm tmpfs one.5G 568K 1.5G 1% /run tmpfs 308M 0 308M 0% /run/user/0
Use the mkdir command to create a new directory on either the remote and local server :
mkdir name_of_directory lmkdir name_of_directory
You lot tin delete one from the remote server using the rmdir command:
rmdir name_of_directory
Meanwhile, renaming a remote file is also rather straightforward:
rename filename new_filename
Here's an example:
rename Old_FileExample New_FileExample
If you want to remove a remote file, use the rm command:
rm filename
While the chown command is used to replace a file'south possessor:
chown userid filename
userid can either be a username or a numeric user ID. For instance:
chown UserOne FileExample chown 1234 FileExample
chgrp is used for changing a file'south grouping owner:
chgrp groupid filename
For case:
chgrp NewGroup FileExample
Finally, you volition need to use the chmod interactive command to alter a file's permission:
chmod 764 FileExample
In this instance, the three-digit value stands for the file'southward user, group, and other users.
As for the permissions to read (r), write (w), and execute (x), their values are four, 2, one, respectively. 0 tin can also exist used to provide no permissions.
To assign permissions, only calculate the total values for each user form. Hither'southward a breakdown of the example:
chmod ugo FileExample # u represents the User who'll be able to read, write and execute the file. # g is for Groups, here we've given the permission to write and execute the file. # o or Others will merely be able to read the file.
List of Useful SFTP Commands
If yous need a quick crook canvass, here's a list of all the available SFTP commands. You can find this list yourself by just entering the help or ? control — both will prompt the same result.
bye Quit sftp cd path Change remote directory to 'path' chgrp [-h] grp path Change group of file 'path' to 'grp' chmod [-h] manner path Change permissions of file 'path' to 'mode' chown [-h] own path Alter owner of file 'path' to 'own' df [-hullo] [path] Display statistics for current directory or filesystem containing 'path' exit Quit sftp go [-afpR] remote [local] Download file help Display this help text lcd path Change local directory to 'path' lls [ls-options [path]] Brandish local directory listing lmkdir path Create local directory ln [-s] oldpath newpath Link remote file (-s for symlink) lpwd Print local working directory ls [-1afhlnrSt] [path] Display remote directory listing lumask umask Set local umask to 'umask' mkdir path Create remote directory progress Toggle display of progress meter put [-afpR] local [remote] Upload file pwd Display remote working directory quit Quit sftp reget [-fpR] remote [local] Resume download file rename oldpath newpath Rename remote file reput [-fpR] local [remote] Resume upload file rm path Delete remote file rmdir path Remove remote directory symlink oldpath newpath Symlink remote file version Show SFTP version !command Execute 'command' in local shell ! Escape to local shell
What is SFTP?
SFTP, or SSH File Transfer Protocol for short, is a much more than secure way to motion files. Using the SSH protocol, information technology supports encryption and other security methods used to ameliorate protect file transfers. Information technology's the only secure file transfer protocol that protects against attacks at any point in the data transfer process, making information technology the preferred protocol.
During file transfer, all of the data is divided into packets and sent through a single secure connection.
Sensitive information will exist encrypted and made unreadable when being transferred between the client and the server. In other words, the original content (plaintext) volition be replaced past an incoherent string of characters (ciphertext).
Only the recipient with the required decryption fundamental volition be able to run across the original content. This prevents any unauthorized access during file transfer.
Regular file transfer protocol (FTP) has ii different channels to exchange data — the command channel and the data channel. In contrast, SFTP has only one encrypted channel where the information is exchanged in encrypted, formatted packets.
Conclusion
That pretty much covers the basics of how to use SFTP for secure file transfer. We promise this tutorial has proved to exist useful. All the same, if y'all need more than information on FTP lonely, you tin discover more tutorials here.
If yous have any more questions, don't hesitate to exit a comment down below.
Source: https://www.hostinger.com/tutorials/how-to-use-sftp-to-safely-transfer-files/
0 Response to "How Do I Use Ssh to Upload Files to a Server"
Postar um comentário