Samba is free software for Linux servers that allows you to share directories and files with Windows workstations, other Linux machines, and smart phones. While the configuration is relatively straightforward, there are quite a few things that can go wrong. This post has the steps you need to get it installed and configured, along with some troubleshooting ideas if it’s not quite working right.
Contents
Install Samba
Samba can be installed on your server with a couple commands:
sudo apt-get install samba samba-common sudo apt-get install python-glade2 system-config-samba
Create Samba User and Group
Next, you will need to create a Samba username and group.
The Samba user will not require shell access to the Linux system, so include –shell /bin/false in the command:
sudo adduser --shell /bin/false sambauser
Add the username to the internal Samba database as well:
sudo smbpasswd -a sambauser
Create a new group, which all new Samba users will be added to:
sudo addgroup sambagroup
Add the newly created Samba user to the Samba group:
sudo adduser sambauser sambagroup
You will need to create a directory that will contain the files to be shared. Two possible locations for it are your home directory, or under the /media/ directory.
mkdir sambashare
Use the chown command to make sambagroup the group owner of the new directory.
sudo chown :sambagroup sambashare
Configure smb.conf
Edit the smb.conf file, which is located at /etc/samba/smb.conf and add a new share at the bottom with the following options:
[ShareName] path = /home/username/sambashare read only = no valid users = sambauser
The share name can be anything you like. Change the path to match the directory you created earlier. The last line (“valid users”) restricts access to the sambauser user. You can change that to another user, or delete the entire line so that all users have access to the share.
Restart the Samba service so that the changes you made the configuration file are applied:
sudo service smbd restart
Test Access
You have successfully setup a Samba share! Now check if the client can access the shared directory. For example, from a Linux workstation:
smbclient //192.168.0.1/sharename -U sambauser
If a connection is established, type ls to see if you get a directory listing. If it’s not working, read the troubleshooting section further down.
Mount Automatically
This section has been adapted from Ubuntu’s documentation.
To mount the share automatically when the computer starts, add the following line to your /etc/fstab file on the client machine:
//servername/sharename /media/windowsshare cifs username=msusername,password=mspassword,iocharset=utf8,sec=ntlm 0 0
For example:
//192.168.1.150/Pictures /home/santa/pics cifs credentials=/home/santa/.smbcredentials,iocharset=utf8,sec=ntlm 0 0
Then add your Samba login credentials to the file .smbcredentials in your home directory. The contents of the file should look like this:
username=msusername password=mspassword
For security, remove all permissions except those of the owner
chmod 600 /home/santa/.smbcredentials
Use the mount command to test. The following command will mount all entries in the fstab file:
sudo mount -a
Troubleshooting
Access Denied
If you see an error message about access denied, the first thing you should do is check the directory permissions.
Make sure that each directory from the root to the shared folder includes execute permissions for everyone. For example, if the shared directory is located at /media/username/shares/documents you could give all users access to all the directories throughout the path with this command:
chmod -R o+x /media
To be more restrictive, you can make the samba group an owner of all directories leading to the shared folder and give the group execute permission throughout the entire path.
sudo chown -R :sambagroup /media chmod -R g+x /media
Samba Log
Check the Samba log for clues as to what the problem may be. The log should be located at: /var/log/samba/log.smbd. You can access it using the tail command. The following command will list the last 50 lines of the Samba log:
tail -n50 /path/to/log
Configuration File
To see which options in smb.conf are actually having an effect on the Samba configuration, you can run testparm. You will probably notice that a lot of the settings you typed in the config file are not actually having an effect on the configuration. You can use the output from testparm to cleanup unnecessary statements. It will also tell you if you have entered a setting in the wrong section.
testparm