MountWindowsSharesPermanently

Differences between revisions 33 and 55 (spanning 22 versions)
Revision 33 as of 2008-08-06 17:00:19
Size: 9530
Editor: localhost
Comment:
Revision 55 as of 2024-04-18 09:12:09
Size: 9284
Editor: sally-makin
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#refresh 0 https://ubuntu.com/server/docs/how-to-mount-cifs-shares-permanently
Line 4: Line 6:
This will help you mounting smbfs shares permanently. These can be shares on a Windows computer or on a Linux/UNIX server running Samba.

''Is it true that smbfs is depricated? According to [[http://joey.ubuntu-rocks.org/blog/2007/04/25/resolution-to-mounting-samba-shares-dont-use-smbfs/|a blog post by Joey Stanford]] we need to use CIFS instead of smbfs!''

''It [[https://bugzilla.samba.org/show_bug.cgi?id=1920#c0|appears to be true]]. I just fought through [[http://ubuntuforums.org/showthread.php?p=2911178|several strange problems]] after following this guide, only to have everything solved by using CIFS instead.''

I can confirm the above as well. I have changed "smbfs" to "cifs" where appropriate in this guide.

= Samba File System installation =
This document describes how to mount CIFS shares permanently. The shares might be hosted on a Windows computer/server, or on a Linux/UNIX server running [[https://help.ubuntu.com/community/Samba|Samba]]. This document also applies to SMBFS shares, which are similar to CIFS but are deprecated and should be avoided if possible ([[https://bugzilla.samba.org/show_bug.cgi?id=1920#c0|link]]).

(This document does ''not'' describe how to host the shares yourself, only how to access shares that are hosted somewhere else. For hosting shares, use [[https://help.ubuntu.com/community/Samba|Samba]].)

= Prerequisites =
We're assuming that:
 * Network connections have been configured properly.
 * Your local (Ubuntu) username is ''ubuntuusername''.
 * Share username on Windows computer is ''msusername''.
 * Share password on Windows computer is ''mspassword''.
 * The Windows computer's name is ''servername'' (this can be either an IP address or an assigned name).
 * The name of the share is ''sharename''.
 * You want to mount the share in ''/media/windowsshare''.

= CIFS installation =

{{{
sudo apt-get install cifs-utils
}}}

On older systems:
Line 18: Line 33:
Assumed that:
 * Network connections have been configured properly.
 * The Windows computer name is ''servername'', this can be either an IP address or an assigned name.
 * The name of the share is ''sharename''.
 * You want to mount the share in a folder ''mountname''.

First, let's create the mount folder. You will need a separate folder for each mount.
{{{
sudo mkdir /media/mountname
}}}

Then edit your /etc/fstab file (you need root privileges) to add this line:
{{{
//servername/sharename /media/mountname cifs guest,uid=1000,iocharset=utf8,codepage=unicode,unicode 0 0

First, let's create the mount directory. You will need a separate directory for each mount.
{{{
sudo mkdir /media/windowsshare
}}}

Then edit your /etc/fstab file (with root privileges) to add this line:
{{{
//servername/sharename /media/windowsshare cifs guest,uid=1000,iocharset=utf8 0 0
Line 36: Line 46:
 * '''uid=1000''' makes the Linux-user with specified uid or username owner of the mounted share, thereby allowing that user to rename files,
 * the combination '''iocharset=utf8,codepage=unicode,unicode''' allows access to files with names in non-English languages. This doesn't work with shares of devices like the Buffalo Tera Station, or Windows machines that export their shares using ISO8895-15. With these the codepage argument has to be '''codepage=cp850''', otherwise characters like the German 'Umlaute' are displayed as garbage.

After you added the entry to /etc/fstab type:
 * '''uid=1000''' makes the Linux user specified by the id the owner of the mounted share, allowing them to rename files,
 * '''iocharset=utf8''' allows access to files with names in non-English languages. This doesn't work with shares of devices like the Buffalo Tera Station, or Windows machines that export their shares using ISO8895-15.
 * If there is any '''space in the server path''', you need to replace it
by \040, for example //servername/My\040Documents

After you add the entry to /etc/fstab type:
Line 43: Line 54:
This will (re)mount all entries in /etc/fstab  This will (re)mount all entries listed in /etc/fstab.
Line 46: Line 57:
Assume the previous, plus:
 * Share username on Windows computer is ''myusername''.
 * Share password on Windows computer is ''mypassword''.

You could add the following to /etc/fstab:
{{{
//servername/sharename /media/mountname cifs username=myusername,password=mypassword 0 0
}}}

However, the /etc/fstab is readable by everyone so it obviously wouldn't be a good idea to have your Windows password in it. The way to get around this is by using what is known as a credentials file. This is a file that contains just the username and password.
{{{
sudo gedit ~/.smbcredentials
}}}

Add the following lines:
{{{
username=myusername
password=mypassword
}}}
and save it.

Modify the permissions on the file so only you have permission to read and write to it. The only thing in the file is your Windows username and password.
{{{
sudo chmod 600 ~/.smbcredentials
}}}

Now add the following line in fstab for '''Read and write permission for everyone''':
{{{
//servername/sharename /media/mountname cifs credentials=~/.smbcredentials,dmask=777,fmask=777 0 0
}}}

or '''for only read permission''':
{{{
//servername/sharename /media/mountname cifs credentials=~/.smbcredentials 0 0
}}}

'''instead of cifs one can also use smbfs.'''

'''On some systems, smbfs automount does not work correctly (is this true for cifs as well?)'''. As a workaround, you can add the "noauto" parameter to your smbfs fstab entry and then have the share mounted at login

/etc/fstab:
{{{
//servername/sharename /media/mountname cifs noauto,credentials=~/.smbpasswd 0 0
}}}

then edit your /etc/rc.local to mount the smbfs when you log in (make sure the permissions on your /etc/rc.local will allow users to execute it:

The quickest way to auto-mounting a password-protected share is to edit /etc/fstab (with root privileges), to add this line:
{{{
//servername/sharename /media/windowsshare cifs username=msusername,password=mspassword,iocharset=utf8 0 0
}}}

This is not a good idea however: /etc/fstab is readable by everyone and so is your Windows password in it. The way around this is to use a credentials file. This is a file that contains just the username and password.

Using a text editor, create a file for your remote servers logon credential:

{{{
gedit ~/.smbcredentials
}}}

Enter your Windows username and password in the file:

{{{
username=msusername
password=mspassword
}}}

Save the file, exit the editor.

Change the permissions of the file to prevent unwanted access to your credentials:

{{{
chmod 600 ~/.smbcredentials
}}}

Then edit your /etc/fstab file (with root privileges) to add this line (replacing the insecure line in the example above, if you added it):

{{{
//servername/sharename /media/windowsshare cifs credentials=/home/ubuntuusername/.smbcredentials,iocharset=utf8 0 0
}}}

Save the file, exit the editor.

Finally, test the fstab entry by issuing:

{{{
sudo mount -a
}}}

If there are no errors, you should test how it works after a reboot. Your remote share should mount automatically.

== Special permissions ==

If you need special permission (like chmod etc.), you'll need to add a ''uid'' (short for 'user id') or ''gid'' (for 'group id') parameter to the share's mount options.

{{{
//servername/sharename /media/windowsshare cifs uid=ubuntuuser,credentials=/home/ubuntuuser/.smbcredentials,iocharset=utf8 0 0
}}}

== Mount password protected shares using libpam_mount (Ubuntu 9.04) ==

In addition to the initial assumptions, we're assuming that
 * '''Your username and password are the same on the Ubuntu machine and on the network drive.'''

Install libpam-mount:
{{{
sudo apt-get install libpam-mount
}}}

Edit /etc/security/pam_mount.conf.xml using your preferred text editor.

{{{
gksudo gedit /etc/security/pam_mount.conf.xml
}}}

First, we're moving the user specific config bits to a file which users can actually edit themselves: remove the commenting tags (<!-- and -->) surrounding the section called <luserconf name=".pam_mount.conf.xml" />. Save the file when done. With this in place, users can create their own ~/.pam_mount.conf.xml.

{{{
gedit ~/.pam_mount.conf.xml
}}}

Add the following:

{{{
<?xml version="1.0" encoding="utf-8" ?>

<pam_mount>

<volume options="uid=%(USER),gid=100,dmask=0700" user="*" mountpoint="/media/windowsshare" path="sharename" server="servername" fstype="cifs" />

</pam_mount>
}}}

= Troubleshooting =

== Login errors ==

If you get the error "mount error(13) permission denied", then the server denied your access. Here are the first things to check:
 * Are you using a valid username and password? Does that account really have access to this folder?
 * Do you have whitespace in your credentials file? It should be {{{password=mspassword}}}, not {{{password = mspassword}}}.
 * Do you need a domain? For example, if you are told that your username is {{{SALES\sally}}}, then actually your username is {{{sally}}} and your domain is {{{SALES}}}. The fstab entry should read: {{{...username=sally,password=pass,domain=SALES,...}}} Or: {{{...credentials=/path/to/file,domain=SALES,...}}}
 * The security and version settings are interrelated. SMB1 is insecure and no longer supported by default. At first, try to not specify either security or version: do not specify {{{sec=}}} or {{{vers=}}}. If you still have authentication errors then you may need to specify either {{{sec=}}} or {{{vers=}}} or both. You can try the options listed at the [[http://manpages.ubuntu.com/manpages/raring/en/man8/mount.cifs.8.html|mount.cifs man page]]. The man page list leaves out the option {{{sec=lanman}}} for some reason, but you should try that one as well ([[https://bugs.launchpad.net/ubuntu/+source/cifs-utils/+bug/1113395|see discussion]]).

== Unprotected network folder won't automount ==

I've had a situation where an unprotected network folder wouldn't automount during bootup, but after manually entering "sudo mount -a" was mounted correctly. I solved this by replacing the "guest" option by "username=guest,password=". If anyone has an explanation for this, please leave a comment.

{{{
//servername/sharename /media/windowsshare smbfs username=guest,password=,uid=1000,iocharset=utf8,codepage=unicode,unicode 0 0
}}}

== Mount during login instead of boot ==

If for some reason/etc/rc0.d/S31umountnfs.sh (networking problems for example) the automatic mounting during boot doesn't work, you can add the "noauto" parameter to your smbfs fstab entry and then have the share mounted at login.

In /etc/fstab:
{{{
//servername/sharename /media/windowsshare cifs noauto,credentials=/home/ubuntuusername/.smbpasswd 0 0
}}}

In /etc/rc.local:
Line 94: Line 174:
mount /media/mountname mount /media/windowsshare
Line 98: Line 178:
= Special permissions =

If you need special permission (like chmod etc.) you need to add '''uid''' or '''gid''' flag. You
can set either UID or username
{{{
//servername/sharename /media/mountname cifs uid=uros,credentials=~/.smbcredentials,dmask=777,fmask=777 0 0
}}}
In that case /media/sharename will be owned by user uros.

= Fixing a CIFS bug with network manager =

If you use network manager, and are getting really slow shutdowns. It's probably because the network manager shuts down before unmounting the network shares. That will cause CIFS to hang, and wait for 60 seconds or so. Here's what you do to fix it:
== Slow shutdown due to a CIFS/Network Manager bug ==

If you use Network Manager, and are getting really slow shutdowns, it's probably because NM shuts down before unmounting the network shares. That will cause CIFS to hang and wait for 60 seconds or so. Here's how to fix it:/etc/rc0.d/S31umountnfs.sh
Line 116: Line 187:
= Mounting a Share with Read/Write rights with a regular User (Without Prompt) =
## isn't this more or less the same as the previous paragraph?

Open a Terminal and type:
{{{
id
}}}

This should output something similar to:
{{{
uid=1000(craize) gid=1000(craize) groups=...
}}}

So for my user (craize) the User ID is 1000 and the Group ID is 1000 too.

Add the following to /etc/fstab:

{{{
//servername/sharename /media/mountname cifs defaults,uid=1000,gid=1000,credentials=~/.smbpasswd,umask=777 0 0
}}}

## is "umask" a valid parameter ???

= Mount password protected network folders without credentials file using libpam_mount =

Assumed that:

 * '''Your username and password are the same on the Ubuntu machine and on the network drive.'''
 * Network connections have been configured properly.
 * The Windows computer name is ''servername'', this can be either an IP address or an assigned name.
 * The name of the share is ''sharename''.
 * You want to mount the share in a folder ''mountname'' inside a user's home folder.

First, let's create the mount folder. You will need a separate folder for each mount.

{{{
$ sudo mkdir /home/username/mountname
}}}

Then, install the required package.

{{{
$ sudo apt-get install libpam-mount
}}}

Then edit the file /etc/security/pam_mount.conf using gedit or your preferred text editor (in Kubuntu, the default is Kate)

{{{
$ sudo gedit /etc/security/pam_mount.conf
}}}

Find the session:

{{{
# Volumes that will be mounted when user triggers the pam_mount module
}}}

And add this line at the end of this session (bellow the provided examples):

{{{
volume username cifs servername sharename /home/&/mountname uid=&,iocharset=utf8,fmask=0770,dmask=0770 - -
}}}

Where
 * '''username''' is the user that, when logged, ''pam_mount'' will mount the volume. It is possible to use ''*'' to all users or ''@groupname'' to all users that have ''groupname'' as their primary group
 * '''cifs''' is the filesystem. It can be any of the filesystems listed on the session '''''# Commands to mount/unmount volumes''''', such as ''cifs'' (preferred) or "smbfs" (deprecated).
 * '''servername''' can be the ip address or the assigned name (don't use // before them).
 * '''sharename''' is the name of the share on the network folder. If the name of the share is your ''username'' (like a home folder) you can use the ''&'' character.
 * '''/home/&/mountname''' means ''/home/username/mountname''. The ''&'' character means the ''username'' in this file.
 * '''uid=&''' makes the Linux-user with specified ''uid'' or ''username'' owner of the mounted share, thereby allowing that user to rename files. You can specify a ''username'' instead of using the ''&'' character
 * '''fmask=0770,dmask=0770''' will set ''rwx'' permissions on files and directories for the user and the group specified with ''uid'' and ''gid''.


Back up the pam gdm file:

{{{
$ sudo cp /etc/pam.d/gdm /etc/pam.d/gdm_backup
}}}

Then configure PAM to mount the external drive every time you login using gdm (change to kdm or xdm if you use Kubuntu or Xubuntu).

{{{
$ echo "@include common-pammount" | sudo tee -a /etc/pam.d/gdm
}}}

Next time you reboot and login, the volume should be mounted without the need to enter your login password twice or to write it down on a file.

= Troubleshooting =

== Unprotected network folder won't automount ==

I've had a situation where an unprotected network folder wouldn't automount during bootup, but after manually entering "sudo mount -a" was mounted correctly. I solved this by replacing the "guest" option by "username=guest,password=". If anyone has an explanation for this, please leave a comment.

{{{
//servername/sharename /media/mountname smbfs username=guest,password=,uid=1000,iocharset=utf8,codepage=unicode,unicode 0 0
}}}
Ubuntu 12.04 already runs '''umountnfs.sh''' at reboot and shutdown by default (/etc/rc0.d/S31umountnfs.sh and /etc/rc6.d/S31umountnfs.sh) so this is no longer necessary.
Line 222: Line 199:
Instead use this format: file_mode=0777,dir_mode=0777 Instead use this format: file_mode=0777,dir_mode=0777 . Or in some cases you might need to use file_mode=0777,dir_mode=0777,nounix ([[http://superuser.com/a/410561|see discussion]])
Line 225: Line 202:
== Use of "credentials=~/.smbcredentials" == == Use of tilde in pathnames such as "credentials=~/.smbcredentials" ==
Line 229: Line 206:
Curiously using credentials=~/.smbcredentials in fstab
would only work if used the full path,
i.e. /home/username/.smbcredentials
Curiously, using credentials=~/.smbcredentials in fstab didn't work.
I had to use the full path, i.e. /home/username/.smbcredentials

(This is likely because the tilde "~" is only a shell short-hand alias for
"$HOME"; it isn't something recognized system-wide by all programs,
especially not in a system file table where the concept of "HOME"
doesn't really exist. -Ian!)
 

This document describes how to mount CIFS shares permanently. The shares might be hosted on a Windows computer/server, or on a Linux/UNIX server running Samba. This document also applies to SMBFS shares, which are similar to CIFS but are deprecated and should be avoided if possible (link).

(This document does not describe how to host the shares yourself, only how to access shares that are hosted somewhere else. For hosting shares, use Samba.)

Prerequisites

We're assuming that:

  • Network connections have been configured properly.
  • Your local (Ubuntu) username is ubuntuusername.

  • Share username on Windows computer is msusername.

  • Share password on Windows computer is mspassword.

  • The Windows computer's name is servername (this can be either an IP address or an assigned name).

  • The name of the share is sharename.

  • You want to mount the share in /media/windowsshare.

CIFS installation

sudo apt-get install cifs-utils

On older systems:

sudo apt-get install smbfs

Mounting unprotected (guest) network folders

First, let's create the mount directory. You will need a separate directory for each mount.

sudo mkdir /media/windowsshare

Then edit your /etc/fstab file (with root privileges) to add this line:

//servername/sharename  /media/windowsshare  cifs  guest,uid=1000,iocharset=utf8  0  0

Where

  • guest indicates you don't need a password to access the share,

  • uid=1000 makes the Linux user specified by the id the owner of the mounted share, allowing them to rename files,

  • iocharset=utf8 allows access to files with names in non-English languages. This doesn't work with shares of devices like the Buffalo Tera Station, or Windows machines that export their shares using ISO8895-15.

  • If there is any space in the server path, you need to replace it by \040, for example //servername/My\040Documents

After you add the entry to /etc/fstab type:

sudo mount -a

This will (re)mount all entries listed in /etc/fstab.

Mount password protected network folders

The quickest way to auto-mounting a password-protected share is to edit /etc/fstab (with root privileges), to add this line:

//servername/sharename  /media/windowsshare  cifs  username=msusername,password=mspassword,iocharset=utf8  0  0

This is not a good idea however: /etc/fstab is readable by everyone and so is your Windows password in it. The way around this is to use a credentials file. This is a file that contains just the username and password.

Using a text editor, create a file for your remote servers logon credential:

gedit ~/.smbcredentials

Enter your Windows username and password in the file:

username=msusername
password=mspassword

Save the file, exit the editor.

Change the permissions of the file to prevent unwanted access to your credentials:

chmod 600 ~/.smbcredentials

Then edit your /etc/fstab file (with root privileges) to add this line (replacing the insecure line in the example above, if you added it):

//servername/sharename /media/windowsshare cifs credentials=/home/ubuntuusername/.smbcredentials,iocharset=utf8 0 0 

Save the file, exit the editor.

Finally, test the fstab entry by issuing:

sudo mount -a

If there are no errors, you should test how it works after a reboot. Your remote share should mount automatically.

Special permissions

If you need special permission (like chmod etc.), you'll need to add a uid (short for 'user id') or gid (for 'group id') parameter to the share's mount options.

//servername/sharename  /media/windowsshare  cifs   uid=ubuntuuser,credentials=/home/ubuntuuser/.smbcredentials,iocharset=utf8   0       0

Mount password protected shares using libpam_mount (Ubuntu 9.04)

In addition to the initial assumptions, we're assuming that

  • Your username and password are the same on the Ubuntu machine and on the network drive.

Install libpam-mount:

sudo apt-get install libpam-mount

Edit /etc/security/pam_mount.conf.xml using your preferred text editor.

gksudo gedit /etc/security/pam_mount.conf.xml

First, we're moving the user specific config bits to a file which users can actually edit themselves: remove the commenting tags (<!-- and -->) surrounding the section called <luserconf name=".pam_mount.conf.xml" />. Save the file when done. With this in place, users can create their own ~/.pam_mount.conf.xml.

gedit ~/.pam_mount.conf.xml

Add the following:

<?xml version="1.0" encoding="utf-8" ?>

<pam_mount>

<volume options="uid=%(USER),gid=100,dmask=0700" user="*" mountpoint="/media/windowsshare" path="sharename" server="servername" fstype="cifs" />

</pam_mount>

Troubleshooting

Login errors

If you get the error "mount error(13) permission denied", then the server denied your access. Here are the first things to check:

  • Are you using a valid username and password? Does that account really have access to this folder?
  • Do you have whitespace in your credentials file? It should be password=mspassword, not password = mspassword.

  • Do you need a domain? For example, if you are told that your username is SALES\sally, then actually your username is sally and your domain is SALES. The fstab entry should read: ...username=sally,password=pass,domain=SALES,... Or: ...credentials=/path/to/file,domain=SALES,...

  • The security and version settings are interrelated. SMB1 is insecure and no longer supported by default. At first, try to not specify either security or version: do not specify sec= or vers=. If you still have authentication errors then you may need to specify either sec= or vers= or both. You can try the options listed at the mount.cifs man page. The man page list leaves out the option sec=lanman for some reason, but you should try that one as well (see discussion).

Unprotected network folder won't automount

I've had a situation where an unprotected network folder wouldn't automount during bootup, but after manually entering "sudo mount -a" was mounted correctly. I solved this by replacing the "guest" option by "username=guest,password=". If anyone has an explanation for this, please leave a comment.

//servername/sharename  /media/windowsshare  smbfs  username=guest,password=,uid=1000,iocharset=utf8,codepage=unicode,unicode  0  0

Mount during login instead of boot

If for some reason/etc/rc0.d/S31umountnfs.sh (networking problems for example) the automatic mounting during boot doesn't work, you can add the "noauto" parameter to your smbfs fstab entry and then have the share mounted at login.

In /etc/fstab:

//servername/sharename  /media/windowsshare  cifs  noauto,credentials=/home/ubuntuusername/.smbpasswd  0  0

In /etc/rc.local:

mount /media/windowsshare
exit 0

Slow shutdown due to a CIFS/Network Manager bug

If you use Network Manager, and are getting really slow shutdowns, it's probably because NM shuts down before unmounting the network shares. That will cause CIFS to hang and wait for 60 seconds or so. Here's how to fix it:/etc/rc0.d/S31umountnfs.sh

sudo ln -s /etc/init.d/umountnfs.sh /etc/rc0.d/K14umountnfs.sh
sudo ln -s /etc/init.d/umountnfs.sh /etc/rc6.d/K14umountnfs.sh

Ubuntu 12.04 already runs umountnfs.sh at reboot and shutdown by default (/etc/rc0.d/S31umountnfs.sh and /etc/rc6.d/S31umountnfs.sh) so this is no longer necessary.


CIFS Options Deprecated

20 Feb 2008 TW

Using dmask or fmask in the fstab file produces the following warnings: WARNING: CIFS mount option 'dmask' is deprecated. Use 'dir_mode' instead. WARNING: CIFS mount option 'fmask' is deprecated. Use 'file_mode' instead.

Instead use this format: file_mode=0777,dir_mode=0777 . Or in some cases you might need to use file_mode=0777,dir_mode=0777,nounix (see discussion)


Use of tilde in pathnames such as "credentials=~/.smbcredentials"

20 Feb 2008 TW

Curiously, using credentials=~/.smbcredentials in fstab didn't work. I had to use the full path, i.e. /home/username/.smbcredentials

(This is likely because the tilde "~" is only a shell short-hand alias for "$HOME"; it isn't something recognized system-wide by all programs, especially not in a system file table where the concept of "HOME" doesn't really exist. -Ian!)


CategoryDocumentation

MountWindowsSharesPermanently (last edited 2024-04-18 09:12:09 by sally-makin)