Mount Windows/Samba Shares with Autofs in CentOS 6/7/8

Samba and Windows that are used on-demand should be mounted with autofs.  The system will mount or remount the share automatically as needed.

  1. Create /etc/smbpasswd to store the credentials for the user accessing the share
    domain=domain.int
    username=storage
    password=p@ssw0rd

  2. Set the file permissions to 600 to prevent non-root users from viewing the password
    # chmod 600 /etc/smbpasswd
  3. Install autofs and cifs-utils
    # yum install autofs cifs-utils
  4. Create a folder that will serve as the root for mounts
    # mkdir /mnt/smb
  5. Add the folder and corresponding config file to the end of /etc/auto.master
    /mnt/smb /etc/auto.smb.top
  6. Add the following to /etc/auto.smb.top
    *       -fstype=autofs,-Dhost=& file:/etc/auto.smb.sub
  7. Add the following to /etc/auto.smb.sub
    * -fstype=cifs,credentials=/etc/smbpasswd,uid=99,gid=99,file_mode=0664,dir_mode=0775,nounix,iocharset=iso8859-15 ://${host}/&
    1. Replace the UID and GID values with the user the share will be mounted as
    2. file_mode is the permissions assigned to share files
    3. dir_mode is the permissions assigned to share folders
  8. Enable and start the autofs service
    CentOS 7/8

    # systemctl enable autofs
    # systemctl start autofs

    CentOS 6

    # chkconfig autofs on
    # service autofs start
  9. Test accessing a share.  It may take a couple of tries to mount.
    # ls /mnt/smb/<server>/<share>

Leave a Reply

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