Research and Development
$ sudo useradd -d /home/smbuser -s /sbin/nologin smbuser
Now, we need to allocate a Samba password to this new user. First, enter your sudo password, followed by the new password for your Samba account, and then verify the password:
$ sudo smbpasswd -a smbuser
Create a shared directory for this user and change its ownership:
$ sudo chown smbuser:smbuser /var/samba/share/smbuser
Next, edit the Samba configuration to add the preceding share:
[Private]
path = /var/samba/shares/smbuser
browsable = yes
writable = yes
valid users = smbuser
Save the changes to the configuration file and reload the Samba server:
$ sudo service smbd reload
Now, check in Windows Explorer. You should see the new shared directory. On trying to open that directory, you will be asked for a Samba username and password.
Samba allows various different types of configuration for shared resources. In the previous recipe, we learned how to set up a public share, and in this recipe we have created a private share for a single user. We have created a new user with the nologin permission. This will allow smbuser to access only the Samba shared directory and nothing else. You can also use existing user accounts on the Ubuntu server.
After adding a user, we set a password to be used with the Samba server. Samba maintains a database of passwords separately from Ubuntu passwords. You can enable or disable Samba users with the following commands.
Enable a Samba user:
$ sudo smbpasswd -e username
Disable a Samba user:
$ sudo smbpasswd -d username
Remove a Samba user:
$ sudo smbpasswd -x username
To enable multiple users to access a shared resource, you can specify the list of users under the valid users line, as follows:
valid users = userone, usertwo, userthree
Similarly, you can limit write permissions to a set of users, as follows:
write list = userone, usertwo
Samba also supports the sharing of users, home directories. This will enable users to create shares for all existing Ubuntu users with a single block of configuration. Add the following lines to the Samba configuration to enable the sharing of home directories:
[homes]
browseable = No
valid users = %S
After this configuration, user's home directories will be available at //server-name/user-name
. You will be required to provide a username and password to access these shares. Home directories are by default shared as read only. To enable write permissions, add the following line to the preceding block:
writable = yes
Note that on Windows, you will not be able to access multiple home directories from a single Windows system. Windows does not allow multiple user authentications to a single host.
Alternatively, to share a directory with a group of users, you can use group sharing. Use the following line to share a directory with a group of users:
path=/var/samba/shares/group-share
valid users = @groupname
Then, set group ownership on the directory, group-share:
$ sudo chgrp groupname /var/samba/shares/group-share
There are some other directives such as create mask, directory mask, force user, and force group. These directives can be used to determine the permissions and ownership of the newly created files under Samba share.
After any changes to the Samba configuration file, use testparm to check the configuration for any syntax errors:
$ testparm
It should show the Loaded services file OK message.