I'm trying to automatically execute a command that mounts a password protected Samba share when my user logs in. The catch is, I share my laptop with another user, and I don't want the Samba share to be mounted when she logs in. I only want it to be mounted when I log in. That rules out editing my fstab file because I want this to be user-specific.

Prior research

I've searched and reviewed lots of questions and answers related to mounting Samba shares, including "Mounting Samba Shares at boot/login and using script" and "Proper fstab entry to mount a samba share on boot," but most answers involve editing fstab, which works great for most people but that's not what I'm looking for here.

What I've done so far

I can manually mount the Samba share from the command line by doing this:

sudo mount -t cifs //centaroo.local/Me /media/Me -o uid=scott,gid=scott,credentials=/home/scott/.smbcredentials,iocharset=utf8,sec=ntlm,file_mode=0600,dir_mode=0700

Problem statement

The previous command works great when I execute it from the command line - it prompts me for my password to gain sudo permission, I enter it, and the command runs. The problem is, when I add the exact same command to the "Command" field in Startup Applications Preferences > Edit Startup Program, save my changes, reboot, and log back in, the Samba share isn't mounted like I expected. I suspect it's because I need root permission to run as sudo, and with the script in the Startup Programs Preferences, there's no way for me to enter it, so the script simply fails.

How can I work around this?


2 Answers 2


Okay, I managed to solve this on my own (with the help of others). The underlying problem was, as I suspected, the fact that I was trying to run a command that required root privileges without supplying a password. I solved this problem by following the instructions in the thread "How do I sudo a command in a script without being asked for a password". The long and short of it is, I created a shell script called "script-mount-samba-share.sh" that contains nothing other than a single line with the mount command that I want to run:

mount -t cifs //centaroo.local/Me /media/Me -o uid=scott,gid=scott,credentials=/home/scott/.smbcredentials,iocharset=utf8,sec=ntlm,file_mode=0600,dir_mode=0700

Then, I changed the file's ownership to root:root and changed its permissions to 700. Next, I set up sudo to allow running that script without entering a password, and then I changed the "Command" field in Startup Applications Preferences > Edit Startup Program to "sudo /home/scott/script-mount-samba-share.sh" (without the quotes), and voila. Next time I rebooted and logged in with my user, the Samba share mounted without my lifting a finger. I also tested that when I reboot and log in with a different user, the Samba share does not get mounted, which is what I want.

The only minor catch that I'm now trying to overcome is, when I'm already logged in and then switch users to someone else, my Samba share still shows up in the left hand nav bar in Nautilus, which makes sense as it was mounted when I logged in with my own user, but I don't want the mounted share to display in Nautilus for the other user, so my next task is to figure out how to make that part happen.


It is important to add the username and password for the mount command, otherwise it might ask for the password. for me this worked:

sudo mount -t cifs //centaroo.local/Me /media/Me -o uid=scott,gid=scott,user=,password=,iocharset=utf8,sec=ntlm,file_mode=0600,dir_mode=0700

of course with all the changes in /etc/sudoers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.