How to Mount SMB Share on Linux

Samba is a service in Linux that provides seamless file and print service to SMB/CIFS clients. One of the main features of Samba is that it allows interoperability between Linux/Unix servers and Windows-based clients. In general, you can share resource-like files and printers over the network between Unix to Unix as well as Unix to Windows.

A samba user can log into samba server (Linux Server) and depending on the rights they are granted, copy files to and from parts of the UNIX file system, submit print jobs and even send you WinPopup messages. Unix users can either mount the shares directly as part of their file structure using the smbmount command or alternatively, can use a utility, smbclient installed with Samba to read the shares with a similar interface to a standard command line program. In this tutorial , I will cover how list and mount smb shares on linux.

Mount SMB share

You manually mount the smb share using the mount command

# mount -t cifs // /smbdata -o user,uid=500,rw,suid,username=aloft,password=aloft123

# mount -t smbfs //my_serv/F_Drive /mnt/fdrive -o credentials=/etc/sambapassfile,uid=sam,gid=users

Mount large smb share point

The general syntax is below to use smbmount command.

smbmount //Hostname/Username /local/mountpoint -o username=username,password=password,lfs

For example:

#smbmount // /oraclefiles -o username=LOGINNAME,password=LOGINPASSWORD,lfs

Here NAS samba share is mounted with lfs (large file system) option enabled. This will avoid errors while copying a large file from NAS storage.

Adding SMB Entries in /etc/fstab

Below example, fstab entries show how to use smb share in fstab file.

# cat /etc/fstab
LABEL=/1 / ext3 defaults 1 1
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
LABEL=SWAP-sda2 swap swap defaults 0 0
// /smbdata cifs user,uid=500,rw,suid, username=aloft,password=aloft123 0 0

In the above example, local user (uid=500) will become the owner of the mounted files. In similar way, you can specify credentials such as uid=uid number or name of the account/user or group.

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. You would modify the line in the /etc/fstab file to look like below.

//servername/sharename /mountdirectory smbfs credentials=/home/myhomedirectory/.smbpasswd 0 0

Below echo command can be used to create the credentials file:

echo username=mywindowsusername > .smbpasswd
echo password=mywindowspassword >> .smbpasswd

chmod 600 .smbpasswd (Modify the permissions on the file so only you have permission to read and write to it)

Fstab options

<options> :  There are a variety of options that you can use for smb mounts. We can go through the important options that you need to know to setup fstab entry for smb mounts.

• username=<arg>

This specifies the username to connect as. If this is not given, then the environment variable USER (which may contain the username of the person using the client) is used.

• password=<arg>

This specifies the SMB password. If this option is not given then the environment variable PASSWD(which may contain the password of the person using the client) is used. If it cannot find password smbmount will prompt for a password.

• credentials=<filename>

Rather than giving username and password directly in fstab file, you can specify a file that contains a username and/or password. The format of the file is:

• uid=<arg>

This sets the uid that will own all files on the mounted filesystem. It may be specified as either a username or a numeric uid.

• gid=<arg>

This sets the gid that will own all files on the mounted filesystem. It may be specified as either a groupname or a numeric gid.

• fmask=<arg>

This sets the file mask. This determines the permissions that remote files have in the local filesystem. This is not a umask, but the actual permissions for the files. The default is based on the current umask.

• dmask=<arg>

This sets the directory mask. This determines the permissions that remote directories have in the local filesystem. This is not a umask, but the actual permissions for the directories. The default is based on the current umask.

• ro

mount read-only

• rw

mount read-write

• iocharset=<arg>

This sets the charset used by the Linux side for codepage to charset translations (NLS). The argument should be the name of a charset, like iso8859-1

List SMB Share

You find the shared resource on remote Samba Server using smbclient command.

# smbclient -L
Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.23c-2] Sharename Type Comment
--------- ---- -------
myshare Disk Mary's and Fred's stuff
IPC$ IPC IPC Service (Samba Server)
root Disk Home Directories
Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.23c-2] Server Comment
--------- -------
Workgroup Master
--------- -------

Connect Samba Share from terminal

Before trying to connect to samba share, you should have privileges to access to the particular share, should have account on that Samba Server and your password should mapped with Samba Password.

# smbclient // -U aloft
Domain=[LOCALHOST] OS=[Unix] Server=[Samba 3.0.23c-2] smb: \>

Note: In RHEL/CentOS smbfs is deprecated.

Read Also:

Bobbin Zachariah 10:38 pm


Your email address will not be published. Required fields are marked *

All comments are subject to moderation.