How to Mount Windows SMB Share on Linux with CIFS

PowerADM.com / Linux / CentOS / How to Mount Windows SMB Share on Linux with CIFS

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) protocol, formerly known as CIFS (Common Internet File System) to share and access shared folders over the network. 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 to access the shared network folder via SMB/CIFS. UDP ports 137, 138, and TCP ports 139 are used for name resolution. If these ports are closed, you will only be able to connect to a shared folder on Windows by using a host IP address.

Mount Windows Share on Linux with Cifs-util

You can mount a shared folder hosted on a Windows computer using the tools in the cifs-util package. Run this command to install the package:

  • Ubuntu/Debian:
    $ sudo apt-get install cifs-utils
  • CentOS/Oracle/RHEL:
    $ sudo dnf install cifs-utils

Create a mount point:

$ sudo mkdir /mnt/share

You can now mount the SMB share from a Windows computer using the User03 account:

$ sudo mount.cifs //192.168.31.33/backup /mnt/share -o user=User03

You must have a Windows user password to connect to a shared folder.

mount cifs share from linux cli

You can set additional parameters when mounting a network SMB folder:

$ sudo mount -t cifs -o username=User03,password=PasswOrd1,uid=1000,iocharset=utf8 //192.168.31.33/backup /mnt/share
  • //192.168.31.33/backup – shared network folder in Windows;
  • /mnt/share – mount point;
  • -t cifs – specify the type of file system to mount;
  • -o mount options (this option can only be used as root, so sudo is used in the command above);
  • username=User03,password=PasswOrd1 – Name and password of the Windows user who has permission to access the share. If anonymous access to the network folder is allowed on Windows, you can specify the guest username here;
  • iocharset=utf8 – enable support for UTF8 encoding to display the name of files on SMB shares;
  • uid=1000 – use this Linux user as the owner of the files in the mounted folder.

mount windows share on linux with user credentials

By default, Windows shares will be mounted on Linux with full permissions (0755). Add the following options to the command if you want to change the default mount permissions:

dir_mode=0755,file_mode=0755

Add the following line to the /etc/hosts file if you want to use the computer name instead of the IP address when mounting a Windows share:

IP_ADDRESS    HOSTNAME

If you do not want to enter Windows user credentials in the mount command, you can save them to a file.

For example:

$ mcedit ~/.windowscredentials

Insert into the file:

username=User03
password=PasswOrd1

save file with windows credentials to access shared folder

If you need to use a user account from an Active Directory domain, you will need to add a third line to the file:

domain = poweradm.com

To anonymously mount a Windows folder:

username=guest
password=

Change file permissions:

$ chmod 600 ~/.windowscredentials

When mounting a shared folder, you can now specify the file path instead of the plaintext credentials:

$ sudo mount -t cifs -o credentials=/home/sysops/.windowscredentials,uid=1000,iocharset=utf8 //192.168.31.33/backup /mnt/share

Unmount a shared SMB folder:

$ sudo umount /mnt/share

Automatically Mount Network SBM Shares on Linux

The /etc/fstab file can be used to enable the automatic mounting of a Windows-shared folder on Linux.

$ sudo mcedit /etc/fstab

Add the following string to the file to connect the SMB shared folder:
//192.168.31.33/backup /mnt/share cifs user,rw,credentials=/home/sysops/.windowscredentials,iocharset=utf8,nofail,_netdev 0 0

  • rw – mount SBM folder in read/write mode
  • nofail – resume Linux boot if file system mount fails
  • _netdev – indicates that the filesystem is network connected. Linux will not mount such filesystems until networking has been successfully initialized on the host.

You can specify the version of the SMB protocol to be used for the connection (SMB version 1.0 is insecure and disabled by default in modern Windows versions). Add the parameter vers=3.0 to the end of the cifs connection string.

//192.168.31.33/backup /mnt/share cifs user,rw,credentials=/home/sysops/.windowscredentials,iocharset=utf8,nofail,_netdev,vers=3.0 0 0

If an incompatible (old version) of SMB is used on the remote Windows host, a connection error will occur:
mount error(112): Host is down 
or
mount error(95): Operation not supported

To immediately mount a shared folder from the fstab configuration file

$ mount -a

How to Access Windows Share on Linux with a Samba Client

Install the samba-client on Linux:

  • Ubuntu/Debian:
    $ sudo apt-get install smbclient
  • CentOS/Oracle/RHEL:
    $ sudo dnf install smbclient

To view the SMB hosts on the local network:

$ smbtree -N

List the SMB folders that are available on a remote Windows host:

$ smbclient -L //192.168.31.33 -N

If you have disabled anonymous access in Windows, you will get an error:

session setup failed: NT_STATUS_ACCESS_DENIED

In this case, you must specify the Windows user account that you want to use to access the shared folder:

$ smbclient -L //192.168.31.33 -U User03

Add the -W option if you want to use a domain user account:

$ smbclient -L //192.168.31.33 -U User03 –W Domain

linux smblient: list shares on windows

To establish an interactive connection to a Windows network share, use the following command

$ smbclient //192.168.31.33/backup -U User03 -W Domain

or

$ smbclient //192.168.31.33/backup -U User03

To access the SMB folder anonymously:

$ smbclient //192.168.31.33/backup -U Everyone

After logging in, the following prompt will appear:

smb: \>

List the files in a shared SMB folder:

dir

browse windows shared folder with smblient

Download the file from the Windows shared folder:

get remotefile.txt /home/sysops/localfile.txt

Save a local file from Linux to an SMB directory:

put /home/sysops/localfile.txt remotefile.txt

You can run multiple smbclient commands one after the other:

$ smbclient //192.168.31.33/backup -U User03 -c "cd MyFolder; get arcive.zip /mnt/backup/archive.zip"
You can use the help command to display a full list of commands in smbclient. The syntax of the smbclient commands is similar to the ftp client commands.

An error may occur when using the smbclient command:

Unable to initialize messaging context
smbclient: Can't load /etc/samba/smb.conf - run testparm to debug it.

Create the file /etc/samba/smb.conf to fix the error.

If the SMB 1.0 protocol is disabled on the Windows host, the smbclient connection will fail:

Reconnecting with SMB1 for workgroup listing.
protocol negotiation failed: NT_STATUS_CONNECTION_RESET
Unable to connect with SMB1 -- no workgroup available.

Leave a Reply

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