Mounting Drives in Ubuntu Using fstab

In the Linux ecosystem, the ability to mount drives - including network drives - is crucial for managing data and resources. The fstab file is a system configuration file in Ubuntu and other Linux distributions that contains information about filesystems. This file is located at /etc/fstab and the system reads it to mount additional filesystems at boot time. This post will guide you on how to mount drives in Ubuntu using fstab.

Mounting with the mount command

The first step in mounting a remote drive is to use the mount command, which allows us to mount filesystems immediately. If you’re dealing with Samba shares, you will need the cifs-utils package installed.

mount -t cifs -o username=username //server-name/sharename /mountpoint

Replace username, server-name/sharename, and /mountpoint with your username, the name of your server and share, and where you want the share to be mounted, respectively. After running the command, you’ll be prompted for your Samba password.

If you want to provide the password directly in the command (though be aware this may be a security risk), you can add it to the command like so:

mount -t cifs -o username=username,password=password //server-name/sharename /mountpoint

Making Mounts Permanent with fstab

To have your mounts persist after a reboot, you’ll need to add them to the /etc/fstab file. Here’s an example entry you might add to the file:

//192.168.1.100/documents /mnt cifs username=smbuser,password=abc123@# 0 0

With this entry, Ubuntu will mount the Samba share located at 192.168.1.100/documents to the /mnt directory upon reboot. The username and password options provide the credentials to access the share.

After editing the fstab file, you can test your changes without rebooting by running mount -a, which will mount all filesystems listed in fstab.

Using a Credentials File

For better security, instead of writing your password directly into the fstab file, you can use a credentials file. This file should contain the username and password for the Samba share. Here’s how you can create and use one:

  1. Create the credentials file:
vim /var/credentials
  1. Add your username and password to the file:
username=username
password=password
  1. Use the credentials file when mounting:
mount -t cifs -o credentials=/var/credentials //192.168.1.100/documents /mnt

And in your fstab file:

//192.168.1.100/documents /mnt cifs credentials=/var/credentials 0 0

This method allows you to keep your password separate and more secure.

Remember incorrect fstab entries can result in an unbootable system or data loss. Always make sure to back up any important data before making changes to system files like fstab.



Buy Me a Coffee