Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Ubuntu - Samba Client Setup and Persistent Shares

Jun 08, 2023 • 0 Minute Read

Please set an alt value for this image...
Today's topic is going to cover the Samba client setup and your ability to mount your Windows shares (Windows 8 included) on your Ubuntu desktop. Although you will get the basic information you need to install and configure your Ubuntu system for accessing Windows shares, if you want to see it in action (including those pieces on the Windows side) in more detail, please visit our sister site Linux Academy. There you can see not only how to configure your Ubuntu system for accessing Windows shares, but how to set up you Ubuntu server as a file server. Linux Academy offers a large number of certification level courses across a wide range of Linux topics. In addition to the demonstration videos, you have access to your very own dedicated Amazon Web Services Linux Server to follow along with each class!Standing in One PlaceThe default setup for most home networks uses DHCP. The problem with DHCP is that it can make managing individual systems more difficult, particularly when you are not running and automatically updated internal DNS service (which most home users would not be doing). Make your life easier and set up your Ubuntu Desktop with a static IP address. This is done easily enough from your 'Settings' application, specifically the 'Network' settings which should look something like this:You will notice in this particular case, that I have both a local DNS server for my network and a Windows domain. Neither of these are necessary for the purposes of sharing or mounting Samba shares, it just happens to be my configuration.Laying the FoundationNext step in our journey is to install install all the pieces for our Samba client. We are going to execute:sudo apt-get install cifs-utils samba-common system-config-samba samba winbindStrictly speaking, ONLY the 'cifs-utils samba and samba-common' packages are required since we are going to configure everything at the command line and 'system-config-samba' gives us a GUI management tool, we are including it here for flexibility later. Additionally, the 'winbind' package will help with full hostname resolution when viewing your local network.System PrepTime to edit our configuration file. Open a console and edit the '/etc/nsswitch.conf' file and be sure the contents look like this:
passwd: compatgroup: compatshadow: compathosts: files mdns4_minimal [NOTFOUND=return] dns wins mdns4networks: filesprotocols: db filesservices: db filesethers: db filesrpc: db filesnetgroup: nis
The only difference in this configuration versus the default install is the 'wins' parameter added in the middle of the 'hosts:' line. Be sure that exists, save your file and reboot your system.Mount Up PartnerAt this point, opening any of your file management software (Dolphin, Gnome Commander, etc) will now allow you to scan your network and view any Windows shares that have been made available and that you have credentials for (if you want to see this in action, again, please visit Linux Academy for a video that shows all of this in detail). However, we are going to take a known share and mount it manually.First, let's create a directory to attach our Windows share to:sudo mkdir -p /mnt/tmpshare...and now connect our share to that new directory:sudo mount -t cifs //shareserver/sharename /mnt/tmpshare -o username=userOne of two things will happen now - (1) You will be prompted for a password so that the share will allow you access to mount it or (2) the mount command will simply succeed (if you do not have the share restricted by password). A simple 'df -h' command will check to see if the share now shows as locally mounted. At this point, let's do a 'sudo umount /mnt/tmpshare' and get this ready to become a permanent share on our system.A Permanent CommitmentSo we have our mount location but we want that share to come up every time we reboot the desktop, and we do not want to have to type our password during every connection attempt (which would cause the reboot mount to fail), nor do we want to include our password for that share in plain text in the fstab file itself. First thing is to create a file in our home directory called '.smbcredentials', which should look like this:
username=userpassword=pwd
Follow that up with:chmod 700 .smbcredentialsFinally, the '/etc/fstab' file should look like this for our example:
# / was on /dev/sdb5 during installationUUID=0d5cc054-7dbe-4799-83d8-e19160f748aa / ext4 errors=remount-ro 0 1# /boot was on /dev/sdc1 during installationUUID=a76dc3a1-933c-4c08-9e4f-7563d447cdf2 /boot ext3 defaults 0 2# swap was on /dev/sdb1 during installationUUID=4769b8e1-196e-4a83-bc7f-7a6b2a28edc5 none swap sw 0 0# our file share//shareserver/sharename /mnt/tmpshare cifs credentials=/home/user/.smbcredentials,rw,noauto,user 0 0
Once we save that file, we can test that everything works as expected by executing:sudo mount -aWhich will mount filesystems that are referred to in the '/etc/fstab' configuration file that are not currently mounted. A quick 'df -h' will check the mount command succeeded.Final ThoughtsSamba is a powerful client (and as you can see demonstrated at Linux Academy, a powerful Samba server as well) that allows you to easily and quickly share and edit files remotely. There are additional details that you may need, so visit our sister site or drop me a line and, as always, I will lend a hand where I can.