Tag: Samba

How to Set Up a shared folder using Samba on Ubuntu with auto mount

How to Set Up a shared folder using Samba on Ubuntu with auto mount

Introduction

One of the most popular questions long-time Linux users have been asked is, “How do I create a network share that Windows can see?

The best solution for sharing Linux folders across a network is a piece of software that’s about as old as Linux itself: Samba.

In this guide, we’ll cover how to configure Samba mounts on an Ubuntu 14.04 server.

Prerequisites

For the purposes of this guide, we are going to refer to the server that is going to be sharing its directories the host and the server that will mount these directories as the client.

In order to keep these straight throughout the guide, I will be using the following IP addresses as stand-ins for the host and server values:

  • Host: 1.2.3.4
  • Client: 111.111.111.111

You should substitute the values above with your own host and client values.

Download and Install the Components

Samba is a popular Linux tool, and chances are you already have it installed. To see if that’s the case, open up a terminal and test to see if its configuration folder exists:

host

$ ls -l /etc/samba

If Samba hasn’t been installed, let’s install it first.

host

$ sudo apt-get update
$ sudo apt-get install samba

Configuring network share on host

Set a password for your user in Samba

host

$ sudo smbpasswd -a <user_name>

Note: Samba uses a separate set of passwords than the standard Linux system accounts (stored in /etc/samba/smbpasswd), so you’ll need to create a Samba password for yourself. This tutorial implies that you will use your own user and it does not cover situations involving other users passwords, groups, etc…

Tip1: Use the password for your own user to facilitate.

Tip2: Remember that your user must have permission to write and edit the folder you want to share. Eg.:

$ sudo chown <user_name> /var/opt/blah/blahblah
$ sudo chown :<user_name> /var/opt/blah/blahblah

Tip3: If you’re using another user than your own, it needs to exist in your system beforehand, you can create it without a shell access using the following command:

$ sudo useradd USERNAME --shell /bin/false

You can also hide the user on the login screen by adjusting lightdm’s configuration, in /etc/lightdm/users.conf add the newly created user to the line :

hidden-users=

Create a directory to be shared

host

$ mkdir /home/<user_name>/<folder_name>

After Samba is installed, a default configuration file called smb.conf.default can be found in /etc/samba. This file needs to be copied to the same folder with the name of smb.conf, but before doing this, it’d be worth running the same ls -l /etc/samba command as above to see if your distro has that file there already. If it doesn’t exist, it’s as simple as entering sudo (or sudo -s to retain escalated privileges for the time-being, or su for systems without sudo) and making use of the default file:

host

$ cp /etc/samba/smb.conf.default /etc/samba/smb.conf

Make a safe backup copy of the original smb.conf file to your home folder, in case you make an error

host

$ sudo cp /etc/samba/smb.conf ~

Edit file /etc/samba/smb.conf

host

$ sudo nano /etc/samba/smb.conf

Once smb.conf has loaded, add this to the very end of the file:

host

[<folder_name>]
path = /home/<user_name>/<folder_name>
valid users = <user_name>
read only = no

Tip: There Should be in the spaces between the lines, and note que also there should be a single space both before and after each of the equal signs.

The [Share Name] is the name of the folder that will be viewable after entering the network hostname (eg: \\LINUXPC\Share Name). The path will be the Linux folder that will be accessible after entering this share. As for the options, there are many. As I mentioned above, the smb.conf file itself contains a number of examples; for all others, there’s a huge page over at the official website to take care of the rest. Let’s cover a couple of the more common ones, though.

guest ok = yes
— Guest accounts are OK to use the share; aka: no passwords.
guest only = yes
Only guests may use the share.
writable = yes
— The share will allow files to be written to it.
read only = yes
— Files cannot be written to the share, just read.
force user = username
— Act as this user when accessing the share, even if a different user/pass is provided.
force group = groupname
— Act as this usergroup when accessing the share. username = username, username2, @groupname
— If the password matches one of these users, the share can be accessed.
valid users = username, username2, @groupname
— Like above, but requires users to enter their username.

Here are a couple of example shares I use:

The force user and force group options are not go-to options, so I’d recommend trying to create a share without them first. In some cases, permission issues will prevent you from writing to certain folders, a situation I found myself in where my NAS mounts and desktop folder were concerned. If worse comes to worse, simply add these force options and retest.

Each time the smb.conf file is edited, Samba should be restarted to reflect the changes. On most distros, running this command as sudo (or su) should take care of it:

host

$ /etc/init.d/samba restart

For Ubuntu-based distros, the service command might need to be used. As sudo:

host

$ sudo service smbd restart

If neither of these commands work, refer to your distro’s documentation.

Once Samba has restarted, use this command to check your smb.conf for any syntax errors

host

$ testparm

With Samba all configured, let’s connect to our shares!

Connect to Samba Network Share from clients

Once a Samba share is created, it can be accessed through applications that support the SMB protocol, as one would expect. While Samba’s big focus is Windows, it’s not the only piece of software that can take advantage of it. Many file managers for mobile platforms are designed to support the protocol, and other Linux PCs can mount them as well.

To access your network share from your desktop client, use your username (<user_name>) and password through the path smb://<HOST_IP_OR_NAME>/<folder_name>/ (Linux users) or \\<HOST_IP_OR_NAME>\<folder_name>\ (Windows users). Note that <folder_name> value is passed in [<folder_name>], in other words, the share name you entered in /etc/samba/smb.conf.

We’ll take a more detailed look at a couple of different solutions for accessing our Samba shares.

Connect to Samba Network Share from Windows client

First things first: The Microsoft side. In Windows, browsing to “Network” should reveal your Linux PC’s hostname; in my particular case, it’s TGGENTOO. After double-clicking on it, the entire list of shares will be revealed.

Network shares are all fine and good, but it’s a hassle that to get to one, it requires a bunch of clicking and waiting. Depending on Windows’ mood, the Linux hostname might not even appear on the network. To ease the pain of having to go through all that again, mounting a share from within Windows is a great option. For simple needs, a desktop shortcut might provide quick enough access, but shortcuts by design are quite limited. When mounting a network share as a Windows network drive, however, it acts just as a normal hard drive does.

Mounting a Samba share as a network drive is simple:

client

$ net use X: \\HOSTNAME\Share
$ net use X: “\\HOSTNAME\Share Name”

Quotation marks surrounding the entire share is required if there’s a space in its name.

A real-world example:

Connect to Samba Network Share from Linux client

Install smbclient

client

$ sudo apt-get install smbclient

List all shares:

client

$ smbclient -L //<HOST_IP_OR_NAME>/<folder_name> -U <user>

Connect:

client

$ smbclient //<HOST_IP_OR_NAME>/<folder_name> -U <user>

Mounting a Samba Network Share from Linux client

You’ll need the cifs-utils package in order to mount SMB shares:

client

$ sudo apt-get install cifs-utils

After that, just make a directory…

client

$ mkdir ~/Desktop/Windows-Share

… and mount the share to it.

client

$ mount -t cifs -o username=username //HOSTNAME/Share /mnt/location

For example

client

$ sudo mount.cifs //WindowsPC/Share /home/geek/Desktop/Windows-Share -o user=geek

Using this command will provide a prompt to enter a password; this can be avoided by modifying the command to username=username,password=password (not great for security reasons).

In case you need help understanding the mount command, here’s a breakdown:

$ sudo mount.cifs – This is just the mount command, set to mount a CIFS (SMB) share.

WindowsPC – This is the name of the Windows computer.  Type “This PC” into the Start menu on Windows, right click it, and go to Properties to see your computer name.

//Windows-PC/Share – This is the full path to the shared folder.

/home/geek/Desktop/Windows-Share – This is where we’d like the share to be mounted.

-o user=geek – This is the Windows username that we are using to access the shared folder.

I recommend mounting the network share this way before inputting it into /etc/fstab, just to make sure all is well. Afterwards, your system’s fstab file can be edited as $ sudo (or su) to automatically mount a share at boot.

Adding values for a network share to fstab isn’t complicated, but making sure they’re the correct values can be. Depending on the network, the required hostname might have to be the text value (TGGENTOO), or an IP address (192.168.1.100). Some trial and error can be expected, and at worst, a trip to your distro’s website might have to be taken.

Here are two examples of what could go into an fstab:

//192.168.1.100/Storage /mnt/nas-storage cifs username=guest,uid=1000,iocharset=utf8 0 0
//TGGENTOO/Storage /mnt/nasstore cifs username=guest,uid=1000,iocharset=utf8 0 0

If a given configuration proves problematic, make sure that the Samba share can be mounted outside of fstab, with the mount command (as seen above). Once a share is properly mounted that way, it should be a breeze translating it into fstab speak.

Conclusion

Samba provides a quick and easy way to share files and folders between a Windows and Linux machines.

If you only want a network share between Linux machines only, you can also Set Up a Network Shared using NFS.

If you want a distributed network share with high availability and automatic replication, you can Set Up a Network Shared using GlusterFS.

Samba is said to provide the best performance for smaller files, but I haven’t done a benchmark myself so I can’t guarantee anything.

Hope you find this post helpful and get your network share running. Until next time!