Thursday, June 13, 2024

Best Linux File Sharing Tips

Datamation content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Today’s article is going to provide you with some useful Linux file sharing tips using common file sharing software. This article assumes two things. First, you’re running Ubuntu. Second, you’re comfortable typing recommended commands into a terminal window.

Samba Linux File Sharing

For the most part, Samba is easy to setup and runs dependably for years. Another side benefit is that you can use Samba with Windows, Linux and OS X.

For the sake of this article, we’ll assume you’re sharing files on the computer you’re reading this article from. That said, the next step is to setup a Samba password for your user. This is going to be a separate password from the one you would use to administer your Linux PC.

sudo smbpasswd -a YourUserName

Now that we have a working Samba password, the next step is to make sure that the directories you intend on sharing are “owned” by you (the user setup to be the Samba user).

sudo chown YourUserName /DirectoryToShare

At this stage, the next step is to setup your Samba configuration. Now some file managers provide a GUI for this. I’ve found that they aren’t all that reliable and therefore, I recommend doing this by hand to ensure a successful network share. We’ll be editing the configuration file using nano.

sudo nano /etc/samba/smb.conf

With the file open, add the following at the end of the file – then save the file.

[folder_name] path = /home/YourUserName/folder_name valid users = YourUserName read only = no

See, that wasn’t so bad! All that’s left is to restart Samba for everything to take affect. But rather than bother with that, I highly recommend rebooting the Samba sharing computer along with others that might be connecting to your Samba shares.

Advantages to using Samba: Using Samba is dependable and provides some limited password protection. Best of all, it works really well with media sharing as well. This is a huge plus for Kodi and Plex users.

NFS Linux File Sharing

Depending on the version of NFS you’re setting up, you’ll find that it can provide better performance than Samba. This is useful with larger file transfers. Setup is relatively painless so long as you’re willing to forgo security features.

To install and setup NFS on your box to act as the server, simply do the following:

sudo apt-get update && sudo apt-get install nfs-kernel-server

Now head over to each Linux PC (presumably Ubuntu) that will act as the client computers and install the needed NFS common package.

sudo apt-get update && sudo apt-get install nfs-common

Next, we need to return to the server and make sure the directory to be shared has the right ownership permissions. Much as we found with Samba.

sudo chown YourUserName /DirectoryToShare

If you’re concerned about messing with /Home directory permissions, create a directory in /var instead.

sudo chown nobody:nogroup /var/DirectoryToShare

Still on the host server, it’s time to configure the NFS exports.

sudo nano /etc/exports

This next step might feel similar to working with your fstab file. Basically, this is where we let our server know what the clients are and what options needed to be considered when sharing files.

/ServerDirectoryToShare 192.168.1.RestOfClientIP (rw,sync,no_root_squash,no_subtree_check)

or if you want multiple PCs to act as clients…

/ServerDirectoryToShare (rw,sync,no_root_squash,no_subtree_check)

Remember, your IP address pool may differ – so double check that you’re using the right one. And if you prefer read only access, remove rw and use ro instead.

The above configuration will provide you with read/write access, forced synchronization, along with other nice features. Now save the file, then execute the following command to export your NFS table.

sudo exportfs -a

And finally, we need to enable and start the NFS service.

sudo service nfs-kernel-server start

Still with me? Yeah, NFS has more steps than Samba, but if you follow along it’s really not so bad. Ready for the final steps? Let’s get to it. On the client PC, edit the following.

sudo nano /etc/fstab

Then add the following to the fstab file.

192.1.168.RestOfServerIP/DirectoryToShare /LocalDirectoryAsMountPoint nfs auto,_netdev,rw,hard,intr 0 0

Save the fstab file. Note that the addition of auto,_netdev should help make sure the network is live before mounting the file system. This will help prevent errors with NFS upon rebooting.

SSH Linux File Sharing

There are a number of ways to do this, however I prefer sticking with something easy like SSHFS. Basically SSHFS is just like it seems – a file system for SSH. First step is to install the SSHFS package.

sudo apt-get install sshfs

Now I’m going to go on record in stating this is a super-cool way to move files back and forth securely. Especially when you’re connecting to remote computers over the Internet. The following approach could be combined with a Dynamic DNS service if the remote WAN IP address isn’t a static one.

At this point, I’m going to assume you have created a mount point already. Perhaps in /mnt or /var. The key thing to remember here is that we’re using SSHFS as a means of mounting our access to the remote SSH server instances as a network share. To be blunt, it’s pretty neat.

Assuming the SSH server is secured well with a key and not a password (I should certainly hope so), then you would connect to the SSH server as follows.

sudo sshfs -o allow_other,defer_permissions,IdentityFile=~/.ssh/id_rsa /local_mount_point

Remember, if you’re connecting to a domain or a Dynamic DNS service domain, just replace the xxx’s with that domain.

Which is the best method for Linux file sharing?

So which method is best overall? In terms of LAN simplicity, Samba wins all day long. However if you need greater performance in terms of speed, NFS has its advantages. And of course, mounting a remote file system should always be done in a secure way. This leaves us with SSHFS.

What say you? Which of these methods is your goto method for file sharing? Perhaps you don’t use any of these methods and instead, rely on something else? Hit the Comments and tell me about it.

Subscribe to Data Insider

Learn the latest news and best practices about data science, big data analytics, artificial intelligence, data security, and more.

Similar articles

Get the Free Newsletter!

Subscribe to Data Insider for top news, trends & analysis

Latest Articles