How to Upload File via Remote Ssh
Sooner or later, you'll find yourself in a situation where you take to upload the file to the remote server over SSH or copy a file from it.
There are various ways you lot tin transfer files over SSH. I am going to discuss the following methods here:
- scp: Legacy command which is beingness deprecated
- rsync: Popular control for file synchronization
- sshfs: Mounting remote directory over SSH
- sftp clients: GUI tool for accessing file over SFTP
For a successful file transfer over SSH, you need to
- to have SSH access between the two machines
- to know the username and countersign on the remote machine
- IP address or hostname (on the same subnet) of the remote car
With that bated, let's see the methods for copying files between remote systems via SSH.
Method ane: Employ scp control to copy files over SSH
I have read that scp is going to be deprecated. Nevertheless, it is my favorite tool for transferring files between systems over SSH. Why? Because its syntax is similar to the cp command.
Let'southward meet how to utilise the scp command.
Copy files from the remote automobile to your local motorcar

Here'due south the scenario. You want to copy files from the remote Linux organisation to the currently logged in system.
Here's a generic syntax that copies the file from the home directory of the user on the remote arrangement to the current directory of your locally logged in system.
scp [email protected]_address:/home/username/filename .
Exercise you see the similarity with the cp command? It'south almost the same except that you accept to specify username and ip address with colon (:).
Now, permit me prove you a real-globe example of this command.

In the example in a higher place, I copied the file remote.txt
from the /dwelling/abhishek/my_file
directory on the remote organisation to the current directory of the local machine.
This should give you a hint that you should know the exact location of the file on the remote organisation. The tab completion does non work on remote systems.
Copy files from your local motorcar to the remote machine

The scenario is slightly inverse here. In this one, you are sending a local file to the remote system over SSH using scp.
This is a generic syntax which will copy the filename to the home directory of username on the remote system.
scp filename [email protected]_address:/habitation/username

In the above example, I copied local.txt
file from the current directory to the home directory of the user abhishek
on the remote system.
Then I logged into the remote organisation to bear witness that the file has actually been copied.
You can copy directories too
Recall I told you I like scp because of its similarity with the cp control?
Similar cp command, you lot tin too use scp to copy directory over SSH. The syntax is similar to the cp command too. You but accept to apply the -r
option.
scp -r source_dir [electronic mail protected]_address:/dwelling/username/target_dir
You lot can exercise a lot more with information technology. Read some more examples of scp command in this tutorial:
Using scp Command in Linux: x Practical Examples Explained
Hither are some practical and essential scp command case to show how to securely copy files between remote Linux systems.
Method 2: Use rsync to copy files and directories over SSH
Since scp is beingness deprecated, rsync is the next best tool for copying files betwixt remote system over SSH. Actually, information technology is better than scp in many terms.
The command syntax is the same as scp. Older versions of rsync had to apply rsync -e ssh
simply that's non the case anymore.
Copy files from the remote machine to your local motorcar
Let'south say you desire to copy a file from the home directory of the user on the remote system to the current directory of your locally logged in system.
rsync [email protected]_address:/home/username/filename .
Let'due south accept the same example you lot saw with scp. I am copying the file remote.txt
from the /home/abhishek/my_file
directory on the remote system to the current directory of the local machine.

Copy files from your local machine to the remote auto
Here is a generic syntax which will copy the file to the home directory of username on the remote system.
rsync filename [email protected]_address:/dwelling/username
Time to encounter the existent world instance. I am copying local.txt
file from the electric current directory to the home directory of the user abhishek
on the remote organization.

How well-nigh copying directories with rsync?
It's the same. Simply apply -r
option with rsync to copy entire directory over SSH betwixt remote systems.
rsync -r source_dir [email protected]_address:/dwelling/username/target_dir
Have a look at this example. I copy the entire my_file directory from the remote arrangement to the local organisation.

rsync is a versatile tool. It is essentially a tool for 'recursively syncing' the contents between 2 directories and quite pop for making automated backups.
15 Applied Examples of rsync Command in Linux
Wondering how to apply rsync command? This article lists some of the essential usages of the rsync control in Linux.
Method 3: Using SSHFS to admission files from remote organisation over SSH
There is also SSHFS (SSH Filesystem) that can be used to access remote files and directories. However, this is non very convenient just for copying files.
In this method, you lot mount the remote directory on your local system. Once mounted, y'all tin copy files betwixt the mounted directory and the local arrangement.
Yous may need to install sshfs on your local system first using your distribution'southward packet manager.
On Debian and Ubuntu, you may use the post-obit command:
sudo apt install sshfs
One time you take sshfs installed on your system, you tin can apply it to mountain the remote directory. It would be better to create a dedicated directory for the mount signal.
mkdir mount_dir
Now mount the desired directory on the remote motorcar in this fashion:
sshfs [email protected]_address:path_to_dir mount_dir
One time it is mounted, you can copy files into this directory or from this directory as if it is on your local machine itself.
cp local_file mount_dir
Remember that you take mounted this file. In one case your piece of work is done, you should besides unmount it:
umount mount_dir
Here's an example where I mounted the my_file
directory from the remote organization to the remote_dir
directory on the local system. I copied the remote.txt
file to the local system then unmounted the directory.

Method 4: Use a GUI-based SFTP client for transferring files between remote systems
Every bit the last resort, you tin use an FTP client for transferring files between remote and local systems.
FileZilla is one of the most pop cross-platform FTP client. You lot tin hands install on your local organization.
Once installed, go to File->Site Manager and add together the remote system details similar IP address, SSH port number, username and countersign.

Once you connect, you can run across a split window view that shows the local filesystem on the left and the remote filesystem on the correct.

To transfer the file, drag and driblet files from left to correct or right to left. A progress bar appears at the lesser.
Which method do yous prefer?
Alright! I showed various command line and GUI methods that can be used for copying files over SSH.
Now it is up to you to decide which method to apply here. Do comment your preferred method for transferring files over SSH.
Source: https://linuxhandbook.com/transfer-files-ssh/
0 Response to "How to Upload File via Remote Ssh"
Post a Comment