How to share files/folders between two different Ubuntu computers and are on different network?

Solution 1:

If computers are connected to different networks, this can be achieved using Hamachi. It enables computers to see each other as if they were in the same network. It can be used with very nice front-end called Haguichi.

enter image description here

If your computers are connected to the same local network start from here.

Now you need to share files somehow. Fortunately, Ubuntu makes it really easy. Just right-click any directory and pick Local Network Share. Window like this one should pop up:

enter image description here

Then configure your share and click Create Share.

On the other PC

Open Nautilus, click Connect to Server on the left menu. Another dialog should pop up:

enter image description here

Type in smb://[ip address of the other computer here] and click Connect.

If you use Hamachi, you will need to provide host's IP as seen by Hamachi. Either use hamachi list command or right-click other computer in Haguichi and select Copy IPv4 Address.

And that's it!

Solution 2:

If you are capable to establish SSH connection between these two computers, you can use sshfs to mount a remote folder (even entire filesystem, if you have right permissions).

sudo apt update && sudo apt install -y sshfs

For example, if you are using key authentication, the mounting command looks like:

sshfs username@hostname_or_ip:/path/to/remote-folder/ /path/to/local-folder/ -p 2222 -o IdentityFile=/path/to/ssh-key/id_rsa
  • -p 2222 you can omit this option, if you are using the default ssh port 22;
  • -o IdentityFile=/path/to/ssh-key/id_rsa you can omit this option, if yours key is placed in the default location (~/.ssh), or if you using ~/.ssh/config file;
  • username@hostname_or_ip you can replace this entry with its equivalent short name, if you using ~/.ssh/config file;
  • use sudo umount /path/to/local-folder/ to unmount;
  • for more details check: sshfs -h or its man page.

So, if the ~/.ssh/config file looks like:

Host rh1
    HostName 79.11.134.121
    IdentityFile ~/.ssh/remote-host-1/id_rsa
    User spas
    Port 22

Host rh2
    HostName 193.164.5.50
    IdentityFile ~/.ssh/remote-host-2/id_rsa
    User spas
    Port 2222

The mounting command will looks more simple:

$ sshfs rh1:/path/to/remote-folder/ /path/to/local-folder/

Once sshfs mounting works, you can:

  • create custom by script file, alias or funcion for fast mounting;
  • create fstab entry for permanent mounting (also here and here);
  • or create pure ~/.bashrc entry, which could be enough for particular cases.