How to mount a Network Share under Linux with a Credentials file and On-Demand Mounting

From steamWiki
Jump to: navigation, search

This is a quick how to showing the steps to setup a network share under Linux using a protected credentials file to store the username and password as well as enabling "on-demand" mounting. This means that the shares won't be mounted at boot but instead will be mounted the first time the directory is accessed.

This guide also assumes that you want to access the files as your standard user (not root). The uid= and gid= options are included in /etc/fstab to accomplish this. Without those flags your user will be able to read but not write to the network share. Look up the "permissions" option to learn how to do other things.

  1. Create your Credentials file
    1. run sudo mkdir /root/.credentials to create a place to store your credentials files
    2. run sudo chmod 700 /root/.credentials to make it so that only root can access this directory
    3. run sudo nano /root/.credentials/server-credentials and enter in the following into that file (replacing USER and PASSWORD with the appropriate values for your server)
      • username=USER
      • password=PASSWORD
    4. run chmod 600 /root/.credentials/server-credentials to make it so that only root can read this file
  2. Create your Local Share Directory and make your user the owner
    1. run sudo mkdir /mnt/server/share
    2. run sudo chown USER:GROUP /mnt/server/share (replacing USER:GROUP with your user and group)
  3. Figure out the User Id and Group Id of your user, for the next step, by running the following
    • id -u USERNAME (replacing USERNAME with your username)
    • id -g USERNAME (replacing USERNAME with your username)
  4. Modify your fstab file
    1. run sudo nano /etc/fstab
    2. add the following line to the bottom of the file
      • //server/share /mnt/server/share cifs uid=UID,gid=GIDcredentials=/root/.credentials/server-credentials (replacing UID and GID with the User Id and Group Id you just found)
  5. Test your changes
    1. run sudo mount -a to reload the fstab file
    2. run cd /mnt/server/share to navigate to the network share directory
    3. run ls to see what's there. It should now be populated with whatever is on the network share