Auto mounting a Samba share in Linux
So you have that Samba server up and running and you can connect to it from Windows and Mac with ease. But when you turn to another Linux box that doesn't have Konqueror, Nautilus, or Dolphin you can't figure out the riddle of connecting. Or maybe you want to have this share mounted at boot time? How do you manage it?
From the command line of course. Yes there are plenty of GUI tools that will allow you to connect to a Samba share easily, but they don't help you set up anything to connect automatically. For that you will need to employ a few command line tools. But once it is finished, your system will be seamless.
What you will need
First I am going to assume you have your Samba server set up and you are able to connect to it from other machines. Outside of that you will need only one piece of software installed on your Linux machine: smbclient. This will be in your distributions' repositories so just open up your Add/Remove Software utility, search for smbclient, select it, and click Apply.
Once smbclient is installed you are ready to go.
A test
Let's first test to make sure your Linux box can see the Samba share. You will need either sudo or root access to do this. Issue the command:
smbclient //IP_TO_SAMBA_SERVER/SHARE_NAME -U USERNAME
Where:
- IP_TO_SAMBA_SERVER is the IP address of your Samba server.
- SHARE_NAME is the share you want to connect to.
- USERNAME is the user name you connect to the share with.
If all is well you should see something like this:
Enter wallenmusic's password:
Domain=[MONKEYPANTZ] OS=[Unix] Server=[Samba 3.2.5]
smb: \>
If you see that you can type quit and then hit the Enter key to escape this prompt.
Setup
The first thing you need to do is create a directory to mount the Samba share to. I created the directory /data with the command:
sudo mkdir /data
Once that directory is created you can then mount it with the command:
mount -t smbfs -o username=USERNAME //IP_TO_SAMBA_SERVER/SAMBA_SHARE /data
Where:
- IP_TO_SAMBA_SERVER is the IP address of your Samba server.
- SHARE_NAME is the share you want to connect to.
- USERNAME is the user name you connect to the share with.
Now if you check the /data directory you should see a listing of the contents of the Samba share.
Automount
Let's make that share automount at boot. This will require editing your /etc/fstab file, adding an entry for this Samba share. In this file (again you will have to have either root or sudo access) you will add a line like this:
//IP_TO_SAMBA_SERVER/SAMBA_SHARE� /data smbfs username=USERNAME,password=PASSWORD, 0 0
Where:
- IP_TO_SAMBA_SERVER is the IP address of your Samba server.
- SHARE_NAME is the share you want to connect to.
- USERNAME is the user name you connect to the share with.
- PASSWORD is the password for the Samba user
Once that entry is saved unmount the /data directory with the command:
umount /data
so you can test your automount entry.
Now, enter the command:
mount -a
If there are no errors you should see the contents of the Samba share in the /data directory.
That's it. Congratulations, you now have an automounted Samba share on your Linux machine.
Advertisement
Automount is when you use “autofs”, this article is about “fstab mount”
Hi,
but what if i want to mount a samba share with individual user rights?
Imagine i have a samba client with two users “Bob” and “Sally”.
I configured a samba share “/mydata” on a server that way that Bob has read/write access
but Sally has read access only.
How can i make fstab use individual usernames&password ???
can the value in
password=value
by encrypted instead of plain text? Thanks.
You might want to mention that you can put the username and password information in a file, and put that file in /root or somewhere similar. That way your username and password isn’t out in the open for all to see.
Just make it //IP_TO_SAMBA_SERVER/SAMBA_SHARE /data cifs credentials=/root/.smbpasswords 0 0
From the man page
credentials=filename
specifies a file that contains a username and/or password. The
format of the file is:
username=value
password=value
This is preferred over having passwords in plaintext in a shared
file, such as /etc/fstab. Be sure to protect any credentials
file properly.
And I’d also suggest not putting the username/password details in the fstab file. On many systems this file is world readable and could expose unnecessary information about your network.
I usually work with credential files. These are plain text files containing username and password for that particular share. I usually store them in the /etc/samba directory named like SAMBA_SHARE_credentials.
So the steps would be (as root or with sudo):
touch /etc/samba/SAMBA_SHARE_credentials
chown root.root /etc/samba/SAMBA_SHARE_credentials
chmod 600 /etc/samba/SAMBA_SHARE_credentials
Edited this file with your favourite editor and add two lines with your the username/password combination for that share:
username=SHARE_USERNAME
password=SHARE_PASSWORD
If you use password with spaces make sure you put your password between quotes like
password=”MY PASSWORD”
Save the file and add a fstab entry like this:
//IP_TO_SAMBA_SERVER/SAMBA_SHARE /data cifs credentials=/etc/samba/SAMBA_SHARE_credentials, 0 0
Olli: You are correct. Out of many years of habit I seem to still default to old tricks. In the /etc/fstab you can edit the entry to look like this:
//IP_TO_SAMBA_SERVER/SAMBA_SHARE /data cifs username=USERNAME,password=PASSWORD, 0 0
Hi Jack, I think smbfs is deprecated for a while now, although still in the kernel, the preferred way is to use cifs, which is maintained unlike smbfs, much more stable and probably even faster. smbfs has been tagged for removal since 2.6.17 (I think, could be wrong though).
It doesn’t change much about the way you mount it, but replace the parts it mentions smbfs with cifs and it’ll work.