JRehkemper.de

Mount CIFS or SMB Share on Linux

Linux can not only mount NFS Share but CIFS / SMB shares from Windows environments as well. Provided you have the right helper tools installed.

Install Helper-Tools

RHEL

On RHEL-based systems they are called cifs-utils.

[tux@server]$ sudo dnf install cifs-utils

Debian

On Debian they are called cifs-utils.

[tux@server]$ sudo apt install cifs-utils

Mount share temporary

While NFS authenticates the client by the hostname, CIFS uses credentials. Therefor you need to provide the credentials either via command-line or in a credentials-file.

With inline credentials the command looks like this.

[tux@server]$ sudo mount -t cifs -o username=<username>,password=<password> //<server-ip or hostname>/<share-name> <mountpoint>
[tux@server]$ sudo mount -t cifs -o username=tux,password=ILoveLinux //share-server/share /mnt/share

If your user is part of an Active Directory you should provide the domain as well.

[tux@server]$ sudo mount -t cifs -o username=<username>,password=<password>,domain=<domain> //<server-ip or hostname>/<share-name> <mountpoint>
[tux@server]$ sudo mount -t cifs -o username=tux,password=ILoveLinux,domain=home.org //share-server/share /mnt/share

if you want to use a credentials-file you need to create one first. The directory is irrelevant, but I would recommend to use your home-directory.

username=tux
password=ILoveLinux
domain=home.org

After that you should restrict the permissions, so only you can read it.

[tux@server]$ chown tux:tux /home/tux/creds-file
[tux@server]$ chmod 600 /home/tux/creds-file

Now you can mount the share and provide the credentials-file.

[tux@server]$ sudo mount -t cifs -o credentials=<path to creds-file> //<ip or hostname>/<share> <mountpoint>
[tux@server]$ sudo mount -t cifs -o credentials=/home/tux/creds-file //share-server/share /mnt/share

Mount Share permanently

To mount a share after every reboot, you need to specify it in you /etc/fstab. Our example would look like this.

# With Inline-Credentials
//<ip or hostname>/<share> <mountpoint> cifs username=<username>,password=<password>,domain=<domain> 0 0
//share-server/share /mnt/share cifs username=tux,password=ILoveLinux,domain=home.org 0 0

# Whith credentials-file
//<ip or hostname>/<share> <mountpoint> cifs credentials=<path to creds-file> 0 0
//share-server/share /mnt/share cifs credentials=/home/tux/creds-file 0 0

Other Options

Cifs offers more mount-options that just username, password and domain. Here is a short list of some of the most important ones.

Specify Permissions

You can specify which permission the mounted folder will have. That way you can restrict access to the share right on you share. This will also affect the permissions of newly created files and directories inside the share.

file_mode=0755
dir_mode=0755

UserId and GroupID

By default the share will be mounted as user root. If you want to use the share with a different user, you have to specify the uid and gid while mounting. This will also change the Permissions on all files and directories on the share.

//<ip or hostname>/<share> <mountpoint> cifs uid=<uid>,gid=<gid> 0 0
//share-server/share /mnt/share cifs uid=1000,gid=1000 0 0
profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.