User Tools

Site Tools


networking:linux:linux_smb

Mounting SMB (Windows) Shares on Linux

Newer Distros

sudo apt-get install cifs-utils
sudo mkdir /mnt/sharename
sudo mount -t cifs //server/sharename /mnt/sharename -o rw,_netdev,username=<smbusername>,password=<smbpassword>,domain=<domain>,uid=<username>,gid=<usergroup>,iocharset=utf8,file_mode=0777,dir_mode=0777

:!: If name resolution fails, try the IP address of the server.

:!: If you need multiple users to access the share, you'll need to work with gid and group memberships. Otherwise, you can usually just use the <username> in the gid field.

Older Distros

Windows (SMB, CIFS) shares can be accessed in various ways from a modern Linux desktop, but if you want everything (such as rsync) to work (most) all of the time, it's best to just mount the Windows share to a local mountpoint. Then, regardless of the type of access you desire, you just access the filesystem as if you were browsing local files.

You can use the fstab file and automount the SMB shares on bootup, but this method forces all access to the Windows share as a particular user regardless of which user is actually accessing the Windows share. It kind of goes against the grain…

So, sometimes it's best to just run a script as a user logs in to mount the Windows share as that user.

Ubuntu users may need to install the smbfs package.

sudo apt-get install smbfs

Allowing Regular Users to Mount SMB Shares

Make sure the smbmnt and smbumount commands are setuid root.

As root:

chmod u+s /usr/bin/smbmnt
chmod u+s /usr/bin/smbumount

Verify these commands are now setuid root:

ls -al /usr/bin/smbmnt
ls -al /usr/bin/smbumount

Create Mountpoint(s)

:!: These mountpoints need to be within the user's home directory to assure ease of access and the proper privileges.

As the regular user:

mkdir -p ~/mnt/sharename
mkdir -p ~/mnt/sharename2

Create a Script to Mount the Share(s)

This script could be on the user's desktop, or most anywhere you want to put it. It should be owned by the regular user, have limited access and be executable.

Use your favorite editor to create a script named mymounts (for example):

vi ~/Desktop/mymounts

Containing:

smbmount //server/sharename /home/regularusername/mnt/sharename -o dmask=0777,fmask=0777,password=smbpassword
smbmount //server/sharename2 /home/regularusername/mnt/sharename2 -o dmask=0777,fmask=0777,password=smbpassword

Set the script permissions to protect your password and make it executable:

chmod 700 mymounts

If the script is on the user's desktop, they can just double-click it whenever they need access to the Windows shares. The mounts are persistent throughout the current login session.

Mount the Shares at Login

In Gnome, you can have your script run automatically upon login like this:

System → Preferences → Sessions → Startup Programs → Add

Add the full path to the script:

/home/username/Desktop/mymounts

Script

:!: Script must run as root.

:!: Limit visibility as the script contains passwords (not secure).

sudo chown root.root /home/username/bin/mymounts

sudo chmod 4700 /home/username/bin/mymounts
#!/bin/bash
# ~/bin/mymounts
# Make sure package cifs-utils is installed
# Store this file in your home directory with 4700 perms as it needs to run suid and contains SMB password

# Set variables here:
SMBSERVER=<SMB Server Name>
SHARENAME1=<SMB Share Name>
SHARENAME2=<another SMB Share Name>
SMBUSER=<SMB User Name>
SMBPASS=<SMB Password (escape BASH symbols)>
SMBDOM=<SMB Domain Name>
USERNAME=<Linux User Name>
USERGROUP=<Linux User Group>
# End of variables

mkdir -p /mnt/$SHARENAME1
mkdir -p /mnt/$SHARENAME2

#These older commands did not need to be run as root
#smbumount ~/mnt/$SHARENAME1
#smbmount //$SMBSERVER/$SHARENAME1 ~/mnt/$SHARENAME1 -o username=,password=
#smbumount ~/mnt/$SHARENAME2
#smbmount //$SMBSERVER/$SHARENAME2 ~/mnt/$SHARENAME2 -o username=$SMBUSER,password=$SMBPASS

umount /mnt/$SHARENAME1
mount -t cifs //$SMBSERVER/$SHARENAME1 /mnt/$SHARENAME1 -o rw,_netdev,username=$SMBUSER,password=$SMBPASS,domain=$SMBDOM,uid=$USERNAME,gid=$USERGROUP,iocharset=utf8,file_mode=0777,dir_mode=0777
umount /mnt/$SHARENAME2
mount -t cifs //$SMBSERVER/$SHARENAME2 /mnt/$SHARENAME2 -o rw,_netdev,username=$SMBUSER,password=$SMBPASS,domain=$SMBDOM,uid=$USERNAME,gid=$USERGROUP,iocharset=utf8,file_mode=0777,dir_mode=0777
networking/linux/linux_smb.txt · Last modified: 2018/01/15 12:08 by gcooper