Friday, December 12, 2014

Ubuntu: Mount a Network Share

Introduction


I want to permanently mount a data directory on the NAS for use by Ubuntu.
  1. The NAS is attached to my network.
  2. I am running Ubuntu 14.04
    1. in VirtualBox 4.3.20
    2. on top of Windows 7 x64 [6.1.7601]
    3. with a Bridged Network Adapter to a USB Wireless LAN Card
  3. Using a Netgear WND3700 Wireless Router

Caution: When following a tutorial like this, it's best to avoid using copy+paste from the article into a resource file being edited.  There are a lot of spaces and other characters that can get picked up and transferred into the destination file on Ubuntu (or other host OS) that will cause configuration errors down the road.


Install cifs-utils


The cifs-utils package provides a means for mounting SMB/CIFS shares on a Linux system.

This package can be installed on Ubuntu using:
sudo apt-get install cifs-utils

Operational Output

 craig@U14BASE01:~$ sudo apt-get install cifs-utils  
 [sudo] password for craig:   
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following extra packages will be installed:  
  keyutils  
 Suggested packages:  
  winbind  
 The following NEW packages will be installed:  
  cifs-utils keyutils  
 0 upgraded, 2 newly installed, 0 to remove and 254 not upgraded.  
 Need to get 120 kB of archives.  
 After this operation, 410 kB of additional disk space will be used.  
 Do you want to continue? [Y/n] Y  
 Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main cifs-utils amd64 2:6.0-1ubuntu2 [86.7 kB]  
 Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty/main keyutils amd64 1.5.6-1 [33.6 kB]  
 Fetched 120 kB in 0s (209 kB/s)     
 Selecting previously unselected package cifs-utils.  
 (Reading database ... 165772 files and directories currently installed.)  
 Preparing to unpack .../cifs-utils_2%3a6.0-1ubuntu2_amd64.deb ...  
 Unpacking cifs-utils (2:6.0-1ubuntu2) ...  
 Selecting previously unselected package keyutils.  
 Preparing to unpack .../keyutils_1.5.6-1_amd64.deb ...  
 Unpacking keyutils (1.5.6-1) ...  
 Processing triggers for man-db (2.6.7.1-1) ...  
 Setting up cifs-utils (2:6.0-1ubuntu2) ...  
 Setting up keyutils (1.5.6-1) ...  



Editing the Name Service Switch configuration file


The Name Service Switch (NSS) is a list of sources that provide name resolution mechanisms.

A name resolution mechanism that you may already be familiar with is the hosts file. This file is configured and used in an identical manner on both Windows and Linux environments. The hosts file, and other name resolution mechanisms, are used to resolve host names to IP addresses.

The Name Service Switch exists here:
/etc/nsswitch.conf
The format of the file is is plain ASCII text, with columns separated by spaces or tab characters. The first column specifies the database name. The remaining columns describe the order of sources to query and a limited set of actions that can be performed by lookup result.

We're going to edit this file by typing:
sudo gedit /etc/nsswitch.conf

and adding the text highlighted in red below:
 # /etc/nsswitch.conf  
 #  
 # Example configuration of GNU Name Service Switch functionality.  
 # If you have the `glibc-doc-reference' and `info' packages installed, try:  
 # `info libc "Name Service Switch"' for information about this file.  
 passwd:     compat  
 group:     compat  
 shadow:     compat  
 hosts:     files mdns4_minimal [NOTFOUND=return] wins dns  
 networks:    files  
 protocols:   db files  
 services:    db files  
 ethers:     db files  
 rpc:      db files  
 netgroup:    nis  


We've added WINS (Windows Internet Name Service). This is a computer name registration and resolution service that maps computer NetBIOS names to IP addresses.

 WINS and DNS are both name resolution services for TCP/IP networks. While WINS resolves names in the NetBIOS namespace, DNS resolves names in the DNS domain namespace. Environments that include some computers that use NetBIOS names and other computers that use domain names must include both WINS servers and DNS servers.


Install winbind


We need to give Ubuntu the ability to resolve a Windows computer name on a DHCP network:
sudo apt-get install libnss-winbind winbind
This is the samba nameservice integration plugin.

Operational Output

 craig@U14BASE01:~$ sudo apt-get install libnss-winbind winbind  
 Reading package lists... Done  
 Building dependency tree      
 Reading state information... Done  
 The following extra packages will be installed:  
  attr libaio1 libhdb9-heimdal libkdc2-heimdal libsmbclient libwbclient0  
  python-dnspython python-samba samba samba-common samba-common-bin  
  samba-dsdb-modules samba-libs samba-vfs-modules smbclient tdb-tools  
 Suggested packages:  
  libpam-winbind bind9 bind9utils ldb-tools ntp smbldap-tools heimdal-clients  
 The following NEW packages will be installed:  
  attr libaio1 libhdb9-heimdal libkdc2-heimdal libnss-winbind python-dnspython  
  samba samba-dsdb-modules samba-vfs-modules tdb-tools winbind  
 The following packages will be upgraded:  
  libsmbclient libwbclient0 python-samba samba-common samba-common-bin  
  samba-libs smbclient  
 7 upgraded, 11 newly installed, 0 to remove and 247 not upgraded.  
 Need to get 1,930 kB/7,881 kB of archives.  
 After this operation, 16.9 MB of additional disk space will be used.  
 Do you want to continue? [Y/n] y  
 Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main libaio1 amd64 0.3.109-4 [6,364 B]  
 Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty/main libhdb9-heimdal amd64 1.6~git20131207+dfsg-1ubuntu1 [59.1 kB]  
 Get:3 http://us.archive.ubuntu.com/ubuntu/ trusty/main python-dnspython all 1.11.1-1build1 [83.1 kB]  
 Get:4 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main samba-dsdb-modules amd64 2:4.1.6+dfsg-1ubuntu2.14.04.3 [219 kB]  
 Get:5 http://us.archive.ubuntu.com/ubuntu/ trusty/main tdb-tools amd64 1.2.12-1 [23.2 kB]  
 Get:6 http://us.archive.ubuntu.com/ubuntu/ trusty/main libkdc2-heimdal amd64 1.6~git20131207+dfsg-1ubuntu1 [52.7 kB]  
 Get:7 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main samba amd64 2:4.1.6+dfsg-1ubuntu2.14.04.3 [839 kB]  
 Get:8 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main winbind amd64 2:4.1.6+dfsg-1ubuntu2.14.04.3 [399 kB]  
 Get:9 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/universe libnss-winbind amd64 2:4.1.6+dfsg-1ubuntu2.14.04.3 [12.3 kB]  
 Get:10 http://us.archive.ubuntu.com/ubuntu/ trusty/main attr amd64 1:2.4.47-1ubuntu1 [29.3 kB]  
 Get:11 http://us.archive.ubuntu.com/ubuntu/ trusty-updates/main samba-vfs-modules amd64 2:4.1.6+dfsg-1ubuntu2.14.04.3 [208 kB]  
 Fetched 1,930 kB in 3s (600 kB/s)        
 Preconfiguring packages ...  
 Selecting previously unselected package libaio1:amd64.  
 (Reading database ... 165809 files and directories currently installed.)  
 Preparing to unpack .../libaio1_0.3.109-4_amd64.deb ...  
 Unpacking libaio1:amd64 (0.3.109-4) ...  
 Selecting previously unselected package libhdb9-heimdal:amd64.  
 Preparing to unpack .../libhdb9-heimdal_1.6~git20131207+dfsg-1ubuntu1_amd64.deb ...  
 Unpacking libhdb9-heimdal:amd64 (1.6~git20131207+dfsg-1ubuntu1) ...  
 Preparing to unpack .../libwbclient0_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking libwbclient0:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) over (2:4.1.6+dfsg-1ubuntu2.14.04.2) ...  
 Preparing to unpack .../libsmbclient_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking libsmbclient:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) over (2:4.1.6+dfsg-1ubuntu2.14.04.2) ...  
 Preparing to unpack .../smbclient_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking smbclient (2:4.1.6+dfsg-1ubuntu2.14.04.3) over (2:4.1.6+dfsg-1ubuntu2.14.04.2) ...  
 Preparing to unpack .../samba-common-bin_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking samba-common-bin (2:4.1.6+dfsg-1ubuntu2.14.04.3) over (2:4.1.6+dfsg-1ubuntu2.14.04.2) ...  
 Preparing to unpack .../python-samba_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking python-samba (2:4.1.6+dfsg-1ubuntu2.14.04.3) over (2:4.1.6+dfsg-1ubuntu2.14.04.2) ...  
 Preparing to unpack .../samba-libs_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking samba-libs:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) over (2:4.1.6+dfsg-1ubuntu2.14.04.2) ...  
 Preparing to unpack .../samba-common_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_all.deb ...  
 Unpacking samba-common (2:4.1.6+dfsg-1ubuntu2.14.04.3) over (2:4.1.6+dfsg-1ubuntu2.14.04.2) ...  
 Selecting previously unselected package python-dnspython.  
 Preparing to unpack .../python-dnspython_1.11.1-1build1_all.deb ...  
 Unpacking python-dnspython (1.11.1-1build1) ...  
 Selecting previously unselected package samba-dsdb-modules.  
 Preparing to unpack .../samba-dsdb-modules_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking samba-dsdb-modules (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Selecting previously unselected package tdb-tools.  
 Preparing to unpack .../tdb-tools_1.2.12-1_amd64.deb ...  
 Unpacking tdb-tools (1.2.12-1) ...  
 Selecting previously unselected package libkdc2-heimdal:amd64.  
 Preparing to unpack .../libkdc2-heimdal_1.6~git20131207+dfsg-1ubuntu1_amd64.deb ...  
 Unpacking libkdc2-heimdal:amd64 (1.6~git20131207+dfsg-1ubuntu1) ...  
 Selecting previously unselected package samba.  
 Preparing to unpack .../samba_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking samba (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Selecting previously unselected package winbind.  
 Preparing to unpack .../winbind_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking winbind (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Selecting previously unselected package libnss-winbind:amd64.  
 Preparing to unpack .../libnss-winbind_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking libnss-winbind:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Selecting previously unselected package attr.  
 Preparing to unpack .../attr_1%3a2.4.47-1ubuntu1_amd64.deb ...  
 Unpacking attr (1:2.4.47-1ubuntu1) ...  
 Selecting previously unselected package samba-vfs-modules.  
 Preparing to unpack .../samba-vfs-modules_2%3a4.1.6+dfsg-1ubuntu2.14.04.3_amd64.deb ...  
 Unpacking samba-vfs-modules (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Processing triggers for man-db (2.6.7.1-1) ...  
 Processing triggers for ufw (0.34~rc-0ubuntu2) ...  
 Processing triggers for ureadahead (0.100.0-16) ...  
 ureadahead will be reprofiled on next reboot  
 Setting up libaio1:amd64 (0.3.109-4) ...  
 Setting up libhdb9-heimdal:amd64 (1.6~git20131207+dfsg-1ubuntu1) ...  
 Setting up libwbclient0:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up samba-libs:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up libsmbclient:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up samba-common (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up smbclient (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up python-samba (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up samba-common-bin (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up python-dnspython (1.11.1-1build1) ...  
 Setting up samba-dsdb-modules (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Setting up tdb-tools (1.2.12-1) ...  
 update-alternatives: using /usr/bin/tdbbackup.tdbtools to provide /usr/bin/tdbbackup (tdbbackup) in auto mode  
 Setting up libkdc2-heimdal:amd64 (1.6~git20131207+dfsg-1ubuntu1) ...  
 Setting up samba (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 smbd start/running, process 8424  
 nmbd start/running, process 8462  
 samba-ad-dc start/running, process 8501  
 Setting up attr (1:2.4.47-1ubuntu1) ...  
 Setting up samba-vfs-modules (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Processing triggers for ufw (0.34~rc-0ubuntu2) ...  
 Processing triggers for ureadahead (0.100.0-16) ...  
 Setting up winbind (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 winbind start/running, process 8556  
 Processing triggers for ureadahead (0.100.0-16) ...  
 Setting up libnss-winbind:amd64 (2:4.1.6+dfsg-1ubuntu2.14.04.3) ...  
 Processing triggers for libc-bin (2.19-0ubuntu6) ...  

At this point, a reboot is recommended.


Creating the Credentials File


A username and password is required for the remote drive. To get Ubuntu to automatically mount the remote drive when started, we'll need a file that contains the username and password.

Create the file in your home directory:
gedit ~/.smbcredentials

The file will look like this:
username=MyUsername
password=MyPassword
# OR:  
# username=MyUsername@MyDomain  
# password=MyPassword  
# OR: (for cifs on Windows Serve 2003)  
# username=MyDomain/MyUsername  
# password=MyPassword  
The information in red must contain your username and password.

Some Additional Security

Because placing a username and password in plain text on disk is a security risk, it is recommended to lock down access to this file to root:
sudo chown root ~/.smbcredentials
sudo chmod 600 ~/.smbcredentials



Editing the File Systems Table (fstab)


Editing the fstab file can be dangerous. Backing up this file prior to editing is recommended.

Edit the file by typing:
sudo gedit /etc/fstab

and inserted this line at the end of the file:
 # /etc/fstab: static file system information.  
 #  
 # Use 'blkid' to print the universally unique identifier for a  
 # device; this may be used with UUID= as a more robust way to name devices  
 # that works even if disks are added and removed. See fstab(5).  
 #  
 # <file system> <mount point>  <type> <options>    <dump> <pass>  
 # / was on /dev/sda1 during installation  
 UUID=78ed2fd4-77af-4dad-8973-d116f68d1303 /        ext4  errors=remount-ro 0    1  
 # swap was on /dev/sda5 during installation  
 UUID=4204cb31-b0a3-4496-9b2f-11b892f13fcf none      swap  sw       0    0  
 //192.168.1.5/Data /media/data cifs credentials=/home/craig/.smbcredentials,iocharset=utf8,gid=1000,uid=1000,file_mode=0777,dir_mode=0777 0 0  

Let's take this line apart one section at a time:
  1. The Remote Drive
     //192.168.1.5/Data  
    
    1. This should be changed to reflect your remote path
  2. The Mount Point in Ubuntu
     /media/data  
    
    1. This should be changed to reflect your preferred mount point
  3. The Credentials Path
     credentials=/home/craig/.smbcredentials  
    
    1. Unless we share a username, you should modify this line
  4. The UID
     gid=1000,uid=1000  
    
    1. This is your user-id and group-id in Ubuntu (Debian).
    2. You can find this information by typing id on the terminal like this:
      1.  craig@U14BASE01:~$ id  
         uid=1000(craig) gid=1000(craig) groups=1000(craig),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)  



Mounting the Drive


If the configuration is correct, this command should successfully mount your drive:
sudo mount -a
You should not be prompted for a password.


You can browse the remote directory contents using
ls -lah /media/data



References

  1. [Ubuntu Handbook] How To Map A Network Drive Onto Ubuntu 14.04 Permanently
    1. Primary source for this article.  
    2. I've made a few modifications suitable for my specific environment.
  2. [Blog] How to access an external network drive using Ubuntu
    1. Helpful reference with an alternative fstab configuration.
  3. WINS
    1. [MS Technet] What is WINS?
      1. If you do not use WINS ... you cannot connect to a remote network resource by using its NetBIOS name unless you use Lmhosts files, and you might be unable to establish file and print sharing connections.
    2. [MS Technet] Windows Internet Name Service (WINS) Overview
      1. Windows Internet Name Service (WINS) is a computer name registration and resolution service that [allows] ... software and other services running on computers and other devices can perform name queries against your WINS server to resolve names to IP addresses.
  4. UIDs and GIDs
    1. [Ubuntu Forums] What is the UID and GID?
      1. Ubuntu, as a Debian derivative, starts numbering users at 1000. 
    2. [Debian Policy Manual] Users and Groups
      1. Dynamically allocated user accounts. By default adduser will choose UIDs and GIDs for user accounts in this range, though adduser.conf may be used to modify this behavior

1 comment:

  1. I get this msg " unable to find a suitable address". My win share has no password
    mount -t cifs //192.168.0.1/winbox /mnt/win -o guest

    ReplyDelete