If you want to have drives auto mounted upon boot, the way to do that is by configuring the /etc/fstab file. By doing this, you don’t have to issue the mount command every time you want access to your drive. The basics of editing this file is pretty straightforward, though some other uses (like auto mounting Windows network shared drives) get slightly more complicated.
The first step to editing the file is by opening it for editing.
sudo vi /etc/fstab
Six entries are needed in order to auto mount a device.
# <file system> <mount point> <type> <options> <dump> <pass>
The file system is which physical device/partition/etc. you want mounted. This can be accomplished by adding the device path, like /dev/sda1, or by adding the unique identifier for it, UUID=some-long-hex-number.
The mount point is where you want the device to be mounted, like /home or /media/cdrom or /mnt/data. Note that just like the mount command, the mount point needs to exist beforehand
The type is the format of the drive. Ext2/3/4, swap, etc.
The options column is for things like ro (read-only), auto (i.e. mount -a mounts the drive), exec (allows binary execution), etc. See the man page for mount for all the options. The default option is used a lot, and provides the options, rw, suid, dev, exec, auto, nouser, async.
The fifth and sixth options refer to whether the filesystem needs to be dumped or not, and if/when the drives are to be checked by fsck.
Now I’ll cover a few scenarios. “Normal” partition mounting, mounting an LVM, an auxiliary hard drive, and finally a Windows shared drive.
A few commands you’ll find useful to determine your drive characteristics (filesystem, device name, UUID, etc.) are:
fdisk -l blkid df -h mount (if it has been auto mounted by your system)
Without futher ado, here is a /boot partition mounted using its UUID:
UUID=a5872082-935d-4069-8d2f-b2f8ebfa602b /boot ext4 defaults 0 2
Next is a / (root) and a /home partition existing in an LVM mounted using its mapped device name:
/dev/mapper/archvg-root / ext4 errors=remount-ro 0 1 /dev/mapper/archvg-home /home ext4 defaults 0 2
Here is a swap partition, also on an LVM:
/dev/mapper/archvg-swap none swap sw 0 0
And a data drive:
UUID=d5438215-452b-1584-e6c2-b7f5de23dd98 /media/data ext4 defaults 0 0
If the data drive is a Windows formatted drive (i.e. NTFS format), your entry will look something like this instead:
UUID=A4145E9B145E6FF2 /media/windows_files ntfs-3g defaults,locale=en_US.utf8 0 0
Be sure to have ntfs-3g installed
apt-get install ntfs-3g
Finally, a Windows network share. There are a couple things different about this then the previous /etc/fstab entries. First, instead of a /dev/sdXY or UUID entry, you add the path to the network share. Secondly, you mount the share as type cifs (which uses the SMB). Thirdly, you create a credentials file to securely hold your username and password, rather than putting them as plain-text in the /etc/fstab file.
Let’s start from the end, with the credentials file. Create it in /etc, and call it cifspw.
Add your username and password
#contents of /etc/cifspw username=your_username password=your_password
Then change the permissions on the cifspw file so only you have access to it.
chmod 600 /etc/cifspw
Next, setup the /etc/fstab entry
//windows_server_name/Shared /mnt/Shared cifs exec,credentials=/etc/cifspw 0 0
If your network shared drive has a space in its name, you’ll have to use \040, as in the below example for a shared name of “Shared File”
//windows_server_name/Shared\040File /mnt/Shared\040File cifs exec,credentials=/etc/cifspw 0 0
Finally, mount the entry with
or by restarting your computer and you’re good to go.