LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Mounting samba shares at startup (https://www.linuxquestions.org/questions/linux-software-2/mounting-samba-shares-at-startup-473600/)

somebodycr 08-13-2006 08:38 PM

Mounting samba shares at startup
 
Hi everybody!

I've been trying to achieve this for about 2 weeks now... it's driving me crazy!!

I know there are a lot of posts about this, but I've read all of them :study: (REALLY!) and I still can't figure out how to solve my problem.


I have 1 box with Slackware 10.1 and a XP box, I want the XP share to be mount at startup.

I created a local folder in linux to mount the share at /windows

Then I edited /etc/fstab, this is the line I use:

//xpbox/share /windows smbfs users,rw,password='',dmask=777,fmask=777 0 0

The only problem I have is that the share doesn't get mounted at startup, because any user can mount them when they log in, just by doing:

mount /windows

During boot when it tries to mount them it shows some errors:

#the network initializes fine...
...
dhcpcd: Your IP Address = 192.168.0.141

# this is done automaticaly... but fails...

Mounting remote (SMB) filesystems: /sbin/mount -a -t smbfs
104: tree connect failed: ERRDOS - ERRnoaccess (Acces denied.)
SMB connection failed
...
Starting Samba: /usr/sbin/smbd -D
/usr/sbin/nmbd -D
Starting gpm: /usr/sbin/gpm -m /dev/mouse -t -ms3

# I thought the error could be because Samba wasn't running yet...
# then I put this on /etc/rc.d/rc.local : mount /windows
# but it brings up the same error again !!!

104: tree connect failed: ERRDOS - ERRnoaccess (Acces denied.)
SMB connection failed
Starting X11 Session manager...
Then just after *any* user logs in, if I open a console window I just do:

mount /windows #--> the same line written in /etc/rc.d/rc.local :scratch:

And the share mounts perfectly!!!

So... I don't know what to do...:confused:

Thanks in advance for the help...

centauricw 08-14-2006 01:43 AM

You can mount it after you login because smbmount needs to send a username and password to the Windows machine in order to mount the SMB file share. You don't have either of these when the system is booting. I've solved this problem by creating a script that will mount SMB file shares at boot time.

Code:

#!/bin/bash
#
# uncmount
# Script to mount a remote SMB file share using a standard UNC address.
#
# Written by Charles Rutledge
# Copyright (c) 2004 Centauri Computer Works, Inc.
#
# Version 1.00
#
# This program is licensed under the terms of the GNU General Public License,
# Version 2 (or at your option, any later version) as published by the Free
# Software Foundation, Inc., 59 Temple Place, Suite 220, Boston MA 0211, and
# available online at http://www.gnu.org/copyleft/gpl.html.
#
# THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
# EXPRESSED OR IMPLIED.  YOU ASSUME ALL RISKS FOR THE USE OF THIS PROGRAM.

# -----------------------  LOCAL SITE VARIABLES  -----------------------
#
# This is the default credentials file that will be used to authenicate the
# remote connection if no credentials file is specified.
#
SMB_CREDENTIALS=/etc/smb-credentials

#
# This is the username and group that will be used for the mounted UNC file
# share.
#
MOUNT_AS_USER=root
MOUNT_AS_GROUP=users

# -------------  DO NOT MODIFY ANYTHING BELOW THIS LINE  --------------
#
# The first argument must be a valid (formatted, anyway) UNC file share.  It
# can include directories beyond the //server-name/file-share, but these will
# be silently ignored.
#
if [ -z "$1" ]; then
    echo "USAGE: uncmount <unc-file-share> [rw] [<credentials-file>]"
    exit 1
fi
if [ `echo $1 | cut -c1-2` = '\\' ]; then
    # --- Windows-style UNC format
    UNC_SERVER=`echo $1 | cut -d'\' -f3`
    UNC_SHARE=`echo $1 | cut -d'\' -f4`
    if [ -z $UNC_SHARE ]; then
        echo "ERROR: $1 is not a valid UNC file share."
        exit 1
    fi
elif [ `echo $1 | cut -c1-2` = '//' ]; then
    # --- UNIX-style UNC format
    UNC_SERVER=`echo $1 | cut -d'/' -f3`
    UNC_SHARE=`echo $1 | cut -d'/' -f4`
    if [ -z $UNC_SHARE ]; then
        echo "ERROR: $1 is not a valid UNC file share."
        exit 1
    fi
else
    echo "ERROR: $1 is not a valid UNC file share."
    exit 1
fi

#
# The second argument can either be the optional 'rw' parameter, indicating
# that the share should be mounted read-write (read-only is the default).  If
# the 'rw' parameter is omitted, then the second argument could specify the
# credentials files to use.
#
ACCESS_MODE="ro"
if [ -n "$2" ]; then
    if [ "$2" = "rw" ]; then
        ACCESS_MODE="rw"
        shift
    elif [ -n "$3" ]; then
        #
        # If a credendials file is specified in third argument, then this is a
        # syntax error.
        #
        echo "USAGE: uncmount <unc-file-share> [rw] [<credentials-file>]"
        exit 1
    fi
fi

#
# See if a credentials file was specified.  It will have been shifted into the
# second argument position by the preceeding step.  And we need to be sure the
# credentials file exists even if it is the default file.
#
[ -n "$2" ] && SMB_CREDENTIALS="$2"
if [ ! -r $SMB_CREDENTIALS ]; then
    echo "ERROR: Credentials file $SMB_CREDENTIALS does not exist."
    exit 1
fi

#
# See if the UNC file share is already mounted.
#
MOUNT_POINT=`echo "/mnt/smb/$UNC_SERVER/$UNC_SHARE" | tr -d [$]`
if [ `cat /etc/mtab | grep -cw $MOUNT_POINT` -eq 1 ]; then
        echo "UNC file share //$UNC_SERVER/$UNC_SHARE already mounted"
        exit 0
fi

#
# Mount the UNC file share on the mount point /mnt/smb/UNC_SERVER/UNC_SHARE,
# stripping off any trailing '$' which Windows uses to identify hidden shares.
# If the mount point does not already exist, it will be created on demand.
#
[ ! -d /mnt ] && mkdir /mnt
[ ! -d /mnt/smb ] && mkdir /mnt/smb
[ ! -d /mnt/smb/$UNC_SERVER ] && mkdir /mnt/smb/$UNC_SERVER
# --- Use MOUNT_POINT here to avoid a trailing '$' on the share name.
[ ! -d $MOUNT_POINT ] && mkdir $MOUNT_POINT
if [ "$ACCESS_MODE" = "rw" ]; then
    /sbin/mount.smbfs //$UNC_SERVER/$UNC_SHARE $MOUNT_POINT -o credentials=$SMB_CREDENTIALS,uid=$MOUNT_AS_USER,gid=$MOUNT_AS_GROUP,fmask=660,dmask=770,rw
else
    /sbin/mount.smbfs //$UNC_SERVER/$UNC_SHARE $MOUNT_POINT -o credentials=$SMB_CREDENTIALS,uid=$MOUNT_AS_USER,gid=$MOUNT_AS_GROUP,fmask=440,dmask=550,ro
fi

The syntax for the script is:

Code:

uncmount <smb-file-share> [rw] [<credentials-file>]
where <smb-file-share> is the file Windows share you want to mount using //<server>/<share> syntax, rw is an optional flag to make the mounted share on Linux read-write (the default is read-only and if the Windows file permissions are not read-write, you can't write the share even if you specify rw), and <credentials-file> is an optional credentials file with the username and password you should use to connect to the Windows file share. You will want to use this at boot.

The creditials file uses the format specified in the smbmount man page:

Code:

username = <value>
password = <value>

Call this from rc.local and your SMB file shares will be automagically mounted at system boot in the mount point /mnt/smb/<server>/<share>. If you don't like this mount point you can change the script or (better yet) use a symbolic link.

Cheers.

somebodycr 08-14-2006 11:56 AM

Tnx for the answer!...

I used your script and it worked perfectly...

... then I started looking what was I doing wrong... and found it :P

In the fstab line:

//xpbox/share /windows smbfs users,rw,password='',dmask=777,fmask=777 0 0

I have to put the username... like this:

//xpbox/share /windows smbfs users,rw,username='',password='',dmask=777,fmask=777 0 0

... and Voilá!...

this way I don't need a script, nor a credentials file... just by adding a line like that for each shared folder that I want mounted at startup it does the trick...

But again, thank's a lot, without your help I couldn't have figured it out...

;)

soulestream 08-14-2006 11:55 PM

you can also use a credentials file

just make a file called "cred" or whatever

username=yourusername
password=yourpassword
domain=yourdomain or workgroup

then

//servername/share /mnt/mntpoint smbfs credentials=/path/to/cred,users,dmask=0777,fmask=0777 0 0

that way you can make your "cred" file only readable by root, so not every user can see your password.


Soule

Charlie Evatt 09-16-2010 09:38 AM

Permission issue for mounting
 
Hi Guys

I've been trying to sort this out myself too, by adding a mount command to rc.local.

Problem is that the mount command needs to have root permissions and this doesn't seem to work.

Is there something I'm missing?

It's basically this that works as root when I'm logged in and have done my su:

mount -t cifs //server/share -o username=me,password=blah /mnt/mountpoint

But if I put it in the rc.local then the whole system gets totally messed up cos I reckon it can't run that command without being root.

Surely the rc.local is executed at boot time by a root level user - or is that not the case?

This should be pretty straightforward - just need to connect to this share at startup!

Thanks in advance for your help.

suprstar 09-16-2010 10:33 AM

rc.local runs as root, I put mount commands like that on some of my systems and have no issues whatsoever. What do you mean, exactly, by 'totally messed up' ?

Charlie Evatt 09-17-2010 06:08 AM

Basically it wouldn't log in - got jammed on the login screen.

suprstar 09-17-2010 06:20 AM

The login screen happens AFTER rc.local runs. So if you add one mount command to rc.local, you get a login screen, but when you type in a login/password, you don't get your shell? Then if you remove it, everything works perfectly?

Or do you not get a login prompt? A mount in rc.local should work fine as long as the network is up. If it fails in rc.local, I'd expect the same command to fail on a command line after you log in as root.


All times are GMT -5. The time now is 05:49 PM.