How to mount an SMB/CIFS share on Linux

A temporary SMB/CIFS mount gives a Linux client one working path to a Windows, NAS, or Samba share without changing boot-time configuration. The mount has to prove that the client attached the intended server/share source and that the local user can read or write through the mounted directory with the expected ownership.

mount.cifs comes from the cifs-utils package and hands the share to the Linux kernel cifs filesystem driver. A root-owned credentials file keeps the password out of shell history, while uid, gid, file_mode, and dir_mode make the mounted tree appear with usable Linux ownership and permissions when the server does not provide Unix ownership metadata.

Use a manual mount for temporary access, for testing a share before adding /etc/fstab, or for proving client options during troubleshooting. Replace the sample server, share, mount point, user, UID, and GID with values from the client and SMB server, and unmount the path when the temporary access is no longer needed.

Steps to mount an SMB/CIFS share on Linux:

  1. Confirm that the CIFS mount helper is available.
    $ mount.cifs -V
    mount.cifs version: 7.4

    Install the cifs-utils package first when this command is missing. On Debian and Ubuntu clients, use sudo apt install cifs-utils.

  2. Create a protected directory for SMB credentials.
    $ sudo install -d -m 0750 /etc/samba/credentials
  3. Create the credentials file for the share account.
    $ sudo vi /etc/samba/credentials/team-share
    /etc/samba/credentials/team-share
    username=alex
    password=replace-with-share-password
    domain=WORKGROUP

    Omit domain= when the account is local to the server or the server does not require a workgroup or domain value. Keep the file as key=value lines with no spaces around the equals sign.

  4. Set the credentials file owner to root.
    $ sudo chown root:root /etc/samba/credentials/team-share
  5. Restrict the credentials file permissions.
    $ sudo chmod 600 /etc/samba/credentials/team-share
  6. Verify that only root can read the credentials file.
    $ stat -c "%a %U %G %n" /etc/samba/credentials/team-share
    600 root root /etc/samba/credentials/team-share
  7. Create the local mount point.
    $ sudo install -d -m 0755 /mnt/team
  8. Identify the local user and group that should own files in the mounted tree.
    $ id alex
    uid=1001(alex) gid=1001(alex) groups=1001(alex)

    The SMB server still enforces access through the account in the credentials file. The uid and gid values control how files appear to local Linux processes after the share is mounted.

  9. List the shares advertised by the SMB server.
    $ smbclient -L //files.example.net -U alex -m SMB3
    Password for [WORKGROUP\alex]:
    
    	Sharename       Type      Comment
    	---------       ----      -------
    	print$          Disk      Printer Drivers
    	team            Disk
    	IPC$            IPC       IPC Service (files server)
    SMB1 disabled -- no workgroup available

    Use the target server name and SMB account. This check confirms the share name before the kernel mount is attempted.
    Related: How to browse SMB shares with smbclient

  10. Mount the share at the local mount point.
    $ sudo mount -t cifs //files.example.net/team /mnt/team -o credentials=/etc/samba/credentials/team-share,uid=1001,gid=1001,file_mode=0660,dir_mode=0770

    Let the client negotiate the SMB dialect unless the server requires a specific version. Add vers= only for a known compatibility requirement.
    Related: How to force an SMB protocol version

  11. Verify that Linux mounted the expected CIFS source.
    $ findmnt --mountpoint /mnt/team --output TARGET,SOURCE,FSTYPE
    TARGET    SOURCE                   FSTYPE
    /mnt/team //files.example.net/team cifs
  12. List files through the mounted directory.
    $ ls -l /mnt/team
    total 4
    -rw-rw---- 1 alex alex 15 Jun 16 02:29 quarterly-plan.txt
  13. Create a temporary file on the mounted share.
    $ touch /mnt/team/manual-mount-check.txt

    Use a read-only check such as ls /mnt/team instead when the share should not be written to.

  14. Confirm that the temporary file uses the intended local owner and mode.
    $ ls -l /mnt/team/manual-mount-check.txt
    -rw-rw---- 1 alex alex 0 Jun 16 02:29 /mnt/team/manual-mount-check.txt
  15. Remove the temporary file.
    $ rm /mnt/team/manual-mount-check.txt
  16. Unmount the share when temporary access is finished.
    $ sudo umount /mnt/team