How to Automatically Mount a Windows SMB Share in Linux Using the /etc/fstab File

If you manage a mixed-OS environment, you frequently need your Linux servers to communicate with Windows file servers. You can manually mount a Windows Shared Folder (SMB/CIFS) by typing a complex mount command in the terminal. However, the exact second the Linux server reboots, the connection is instantly severed, and you have to type the command all over again.

To create a permanent, persistent connection that automatically reconnects every time the Linux machine boots up, you must hardcode the network location directly into the Linux File System Table (/etc/fstab).

Step 1: Install the CIFS Utilities

Before Linux can understand the proprietary Windows SMB protocol, it needs the correct translation libraries.

  1. Open your Linux terminal.
  2. Run the package manager to install the CIFS utilities:
    • For Ubuntu/Debian: sudo apt install cifs-utils
    • For RHEL/CentOS: sudo yum install cifs-utils

Step 2: Create the Local Mount Point

Linux needs an empty physical folder on its own hard drive to act as the “doorway” to the Windows server.

  1. Create a new directory in the /mnt folder:
    sudo mkdir -p /mnt/windows_share

Step 3: Create a Secure Credentials File

You should never type your Windows password directly in plaintext into the fstab file, as that file is readable by all users on the system. You must create a hidden credentials file.

  1. Create a hidden file in your root directory:
    sudo nano /root/.smbcredentials
  2. Add your Windows username and password exactly like this:
    username=your_windows_username
    password=your_windows_password
    domain=YOUR_DOMAIN (optional)
  3. Save and exit nano (Ctrl+O, Enter, Ctrl+X).
  4. Lock down the permissions so only the root user can read the file:
    sudo chmod 600 /root/.smbcredentials

Step 4: Hardcode the fstab Entry

Now, you will inject the persistent mounting rule into the File System Table.

  1. Open the fstab file in nano:
    sudo nano /etc/fstab
  2. Scroll to the absolute bottom of the file and paste the following line (all on one single line). Replace the IP address with your actual Windows Server IP:
    //192.168.1.100/SharedFolder /mnt/windows_share cifs credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777 0 0
  3. Save and exit nano.

The Result

To test if your configuration is perfect without having to reboot the server, simply run:

sudo mount -a

This commands Linux to read the fstab file and instantly mount anything that isn’t already connected. If you receive no error messages, type ls /mnt/windows_share. You will instantly see all the files hosted on your remote Windows server. Moving forward, the Linux kernel will automatically execute this connection within seconds of booting up, guaranteeing persistent cross-platform file access.

RELATED POSTS

  • How to Prevent a Linux System from Sleeping via Terminal
  • How to Use the Linux nc (Netcat) Command for Network Troubleshooting and Port Scanning
  • How to Generate a Secure Random Password Using the Linux Terminal
  • How to View the Process Hierarchy in Linux Using the pstree Command
  • How to Add Line Numbers to Text Files in Linux Using the nl Command
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.