Sending Files from a Remote Ubuntu Machine to Your Local Machine with SSH by@okeybukks
469 reads

Sending Files from a Remote Ubuntu Machine to Your Local Machine with SSH

tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

The remote machine is the Ubuntu system installed in your local system. The local machine is your**Laptop or Desktop**, in my case, it's a Mac. To transfer files from your remote system to your local machine, an ssh connection needs to be set between the remote and local systems. To get the public key transferred to the remote system, the IP address and user currently logged into the remote is needed. To send the created public key to the. remote system run this command from the terminal of your local. machine.
featured image - Sending Files from a Remote Ubuntu Machine to Your Local Machine with SSH
Achebe Okechukwu  HackerNoon profile picture

@okeybukks

Achebe Okechukwu

An Igbo man with a conscience. Cloud engineering student...

react to story with heart

This topic was one I really feared when I started my Linux journey. My biggest struggle was understanding the difference between remote and local machines, this sounds weird but I struggled with it. So this write-up is aimed at explaining how to transfer files between your remote system and your local machine.

The remote machine is the Ubuntu system installed in your local system, in my case the remote system was installed using vagrant. The local machine is your Laptop or Desktop, in my case, it's a Mac.

To transfer files from your remote system to your local system, an ssh connection needs to be set between the remote and local systems.

To create an ssh connection between your remote and local systems, from your terminal in your local machine, run this command to create your ssh key.

‘ssh-keygen -t rsa’

If you are okay with the current location suggested by the ssh-keygen prompt, just press enter. Also for the passphrase prompt, you can simply just press enter.

The created keys can be found using this command.

ls -l .ssh/

A sample output of the command above can be seen below.

-rw-------  1 achebe  staff  2635 Aug 23 00:11 id_rsa -rw-r--r--  1 achebe  staff   589 Aug 23 00:11 id_rsa.pub’’’

The ‘id_rsa’ is the private key, while ‘id_rsa.pub’ is the public key generated by the ssh-keygen command.

For the ssh connection to be made, the generated public key needs to be sent to the remote system. To get the public key transferred to the remote system, the IP address and user currently logged into the remote system are needed. To get your IP address, from your remote system run this command.

ifconfig

The needed IP address is usually located in the enp0s8 or eth1 adapter section outputted by the ‘ifconfig’ command. A sample of the command is shown below.

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 inet 192.168.56.9  netmask 255.255.255.0  broadcast 192.168.56.255 inet6 fe80::5f96:e06a:841c:d33a  prefixlen 64  scopeid 0x20<link> ether 08:00:27:e0:2b:8d  txqueuelen 1000  (Ethernet) RX packets 939  bytes 229027 (223.6 KiB) RX errors 0  dropped 0  overruns 0  frame 0 TX packets 783  bytes 161203 (157.4 KiB) TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0’’’

From my remote system, 192.168.56.9 is my ipaddress. To get the user currently logged, run this command.

whoami

The user currently logged on to my remote system is vagrant.

To send the created public ssh to the remote system run this command from the terminal of your local machine.

ssh-copy-id -i .ssh/id_rsa [email protected]

Your output will look like this:

[email protected] ~ % ssh-copy-id -i .ssh/id_rsa [email protected] /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub" The authenticity of host '192.168.56.9 (192.168.56.9)' can't be established. ED25519 key fingerprint is SHA256:f3vzmjzoMj6cLsw0DhtrTlt/R6muK996HX0UdgD7qTg. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])?’’’

Type yes to the prompt given and that should be it.

However, if you get this error;

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).’’’

or simply this:

[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

Please don't fright; to get this solved, go to your remote machine and edit the /etc/ssh/sshd_config file. Change and uncomment PasswordAuthentication, its value should be;

PasswordAuthentication yes

Go on, save the file, and please remember to restart the sshd service using this command;

sudo systemctl restart sshd

Once this has been done you can rerun this command

ssh-copy-id -i .ssh/id_rsa [email protected] and you’re good to go.

Transfer of files can be possible now between the two systems because we have an established ssh connection.

For a case scenario, let's save the output of the ifconfig command from our remote system into a file and then transfer that file from our remote system to our local machine.

From our remote system, the format to save the output of a command to a file is:

command > file_name.txt

So in our case, to save the output of the ifconfig file, run this command;

ifconfig > ifconfig.txt

This created file is located in the home directory of the vagrant user which is /home/vagrant. This information is important when making the file transfer between the remote and local systems.

Transferring files between two ssh connected systems are carried out by the use of the scp command. The format for this transfer is given below.

scp source_directory destination_directory

For this transfer, run this command from the local system terminal to transfer the file into the local system.

scp [email protected]:/home/vagrant/ifconfig.txt Desktop

Where [email protected]:/home/vagrant/ifconfig.txt is the source directory where the file is located in our remote system. Desktop is the directory in the local system where we intend to store the file. Once the command above is run, check the Desktop folder in the local system, the ifconfig.txt file will be located there.

Thank you for reading, I hope it helped in giving a good understanding of file transfer with ssh.


Also published here.

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa