Accessing Windows SMB shares from Linux with cifs
List SMB shares with smbclient and mount them with mount -t cifs, keeping credentials in a protected file.
Linux can access Windows shares and NAS devices that use the SMB protocol (CIFS is the name of the Linux client module). smbclient lists and browses shares, and the cifs mount type mounts them into the file system.
Installation
| Distribution | Packages |
|---|---|
| Debian, Ubuntu | sudo apt install smbclient cifs-utils |
| RHEL, Fedora, AlmaLinux | sudo dnf install samba-client cifs-utils |
| Arch Linux | sudo pacman -S samba cifs-utils |
List the shares of a server
smbclient -L //192.0.2.10 # anonymous (guest) listing
smbclient -L //192.0.2.10 -U username # prompts for the password
Connect to a share:
smbclient //192.0.2.10/share -U username
Mount a share with cifs
Store the credentials in a file that only root can read, for example /root/.smbcredentials:
username=username
password=<password>
sudo chmod 600 /root/.smbcredentials
Mount the share:
sudo mount -t cifs //192.0.2.10/share /mnt/share -o rw,credentials=/root/.smbcredentials,vers=2.0
Mount the share in the current user's home directory:
sudo mount.cifs //192.0.2.11/share ~/WindowsShare/ -o user=username,uid=$UID
Unmount:
sudo umount /mnt/share
Notes
- The
versoption selects the SMB protocol version (for example 2.0, 2.1 or 3.0). Use a version the server supports; the example uses 2.0. - A password typed on the command line is stored in the shell history, so a credentials file is preferable.