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:
achebe@okechukwus-MacBook-Pro ~ % 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.