In this article, we’ll look at how to mount a shared network folder hosted on a Windows computer in Linux. Windows uses the SMB (Server Message Block, formerly CIFS/Common Internet File System) protocol to share network folders. On Linux, you can use the cifs-utils or Samba client to access Windows network shares via SMB protocol.
Hint. Port TCP/445 is used for SMB/CIFS shared network folder access. UDP ports 137, 138, and TCP ports 139 are used for name resolution. If these ports are closed, you can only connect to a Windows shared folder using an IP address.
Mounting a Windows Share on Linux with cifs-util
You can mount a network folder located on a Windows host using the tools from the cifs-util package. To install the package, run the command:
- Ubuntu/Debian:
apt-get install cifs-utils
- CentOS/Oracle/RHEL:
dnf install cifs-utils
Now you can mount the Windows share folder manually with the command:
mount.cifs //192.168.200.30/docs /mnt
The previous example assumed that the user was allowed to anonymously connect to a network share in Windows. If a user needs to authenticate to access the folder, use the command:
mount.cifs //192.168.200.30/docs /mnt -o user=localuser1
If you need to use an AD domain user account to connect:
mount.cifs //192.168.200.30/docs /mnt -o user=s.brion,domain=poweradm.com
Tip. If there are spaces in the shared folder name, you need to replace them with \040:
mount.cifs //192.168.200.30/shared\040docs /mnt -o user=localuser1
You can mount a network folder automatically using fstab. To do this, create a user credential file:
vim /root/.smbshare
Specify username, password, and your AD domain name:
username=s.brion
password=Pa$$word2022y
domain=poweradm.com
And add the SMB connection string to the /etc/fstab file:
//192.168.200.30/docs /mnt cifs user,rw,credentials=/root/.smbshare 0 0
rw means that the shared folder will be mounted for reading and writing.
You can specify the version of SMB protocol used for the connection (SMB 1.0 is a legacy, unsecure protocol that is disabled by default in current versions of Windows). To mount a shared folder using SMB version 3.0, add the line:
//192.168.200.30/docs /mnt cifs user,rw,credentials=/root/.smbshare,vers=3.0 0 0
If an incompatible version of SMB is enabled on the Windows host, an error will appear:
mount error(112): Host is down
To mount a network from the fstab folder immediately:
mount -a
Connecting an SMB Share on Linux Using a Samba Client
Install the samba-client on Linux:
- Ubuntu/Debian:
sudo apt-get install smbclient
- CentOS/Oracle/RHEL:
dnf install smbclient
Allow access for the client by adding the following rule to the firewall:
firewall-cmd --add-service samba-client --permanent; firewall-cmd –reload
In order to discover all SMB shares on the local network:
smbtree -N
To list the available SMB shares on a remote host under a specific account, run the command:
smbclient –L \\\\192.168.200.30 –U 'administrator'
To connect to a Windows shared folder, use the command:
smbclient -U usernam1 \\\\192.168.200.30\\shared_docs
For anonymous access:
smbclient -U Everyone \\\\192.168.200.30\\shared_docs
When using the smbclient command, an error may appear:
Unable to initialize messaging context
smbclient: Can't load /etc/samba/smb.conf - run testparm to debug it
To fix the error, create the /etc/samba/smb.conf file.