Mounting Tips & Tricks for Linux.

Mounting Filesystems[edit | edit source]

ISO Disk Images[edit | edit source]

To mount a .iso image, use iso9660 type:

mount -o loop -t iso9660 <isofile> <destination>

Samba Network Shares[edit | edit source]

Ensure you have samba-utils installed and then run:

# mount -t cifs -o username=leo,password=53cr3T$ //10.1.1.123/share /mnt/share

You can also have the secrets placed in a separate file rather than have it in line with the mount command. To do so, create a separate credentials file with the following contents:

# cat /etc/cifs_passwd  # can be placed anywhere. Ensure it's not world readable.
username=leo
password=53cr3T$

Then to mount, use -o credentials=/etc/cifs_passwd like:

# mount -t cifs -o credentials=/etc/cifs_passwd //10.1.1.123/share /mnt/share

To set the default user/group ID, define the uid and gid value as part of the mount options.

For example:

# mount -t cifs -o uid=1024,gid=512,credentials=/etc/cifs_passwd //10.1.1.123/share /mnt/share

/etc/fstab[edit | edit source]

The filesystem tab file contains all filesystems that the system should mount on start up. Each line of the file defines a filesystem that should be mounted. Example entries from Wikipedia shows various examples for different file systems:

# device-spec   mount-point     fs-type      options                                          dump pass
LABEL=/         /               ext4         defaults                                            1 1
/dev/sda6       none            swap         defaults                                            0 0
none            /dev/pts        devpts       gid=5,mode=620                                      0 0
none            /proc           proc         defaults                                            0 0
none            /dev/shm        tmpfs        defaults                                            0 0

# Removable media
/dev/cdrom      /mnt/cdrom      udf,iso9660  noauto,owner,ro                                     0 0

# NTFS Windows 7 partition
/dev/sda1       /mnt/Windows    ntfs-3g      quiet,defaults,locale=en_US.utf8,umask=0,noexec     0 0

# Partition shared by Windows and Linux
/dev/sda7       /mnt/shared     vfat         umask=000                                           0 0

# mounting tmpfs
tmpfs           /mnt/tmpfschk   tmpfs        size=100m                                           0 0

# mounting cifs
//cifs_server_name/ashare  /store/pingu    cifs         credentials=/root/smbpass.txt                       0 0

# mounting NFS
nfs_server_name:/store    /store          nfs          rw                                                  0 0

Each field in order are:

Device Spec The device name, label, UUID that defines the data source, hardware, or partition
Mount Point The mount point of the filesystem. Swap partitions or specific files is set to 'none'
Filesystem Type The filesystem type of this device.
Mount Options Mount options used by the filesystem. 'defaults' refers to the filesystem defaults.
Dump Determines how often the filesystem should be backed up by the dump program. 0 to never backup.
Pass Determines the order in which the fsck program will check devices for errors on startup. 1 for root filesystem. 2 to check after root. 0 to disable.


Tips[edit | edit source]

Remounting from Read-Only to Read-Write[edit | edit source]

You can remount something that's mounted read-only by using the remount option.

# mount -o remount,rw /

Mounting a disk image without root[edit | edit source]

Using udisksctl, you can mount disk images without requiring root or sudoers access.

$ dd if=/dev/zero of=test.img bs=1M count=100
$ udisksctl loop-setup -f test.img
Mapped file $PATH_TO_IMAGE as /dev/loop0.

Depending on your system, this may have triggered the loopmount to automount somewhere in /run/media/username. If nothing was mounted, manually mount the loopmount device:

$ udisksctl mount -b /dev/loop0
Mounted /dev/loop0 at /media/$USER/$IMAGE_NAME

To unmount the filesystem:

$ udisksctl unmount -b /dev/loop0
$ udisksctl loop-delete -b /dev/loop0

See Also: https://wiki.debian.org/ManipulatingISOs

Mounting disk image partitions[edit | edit source]

To mount a specific partition inside a disk image, use the fdisk utility to see where the partition boundaries are.

# fdisk -l out.bin
Disk out.bin: 58.6 MiB, 61440000 bytes, 120000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000981cb

Device     Boot  Start     End Sectors  Size Id Type
out.bin1          8192  122879  114688   56M  c W95 FAT32 (LBA)
out.bin2        122880 5785599 5662720  2.7G 83 Linux

The first partition starts on sector 8192. Since each sector is 512 bytes, we can mount this particular partition by passing an offset of 8192 * 512 bytes.

# mount -o loop,offset=$((8192 * 512)) out.bin /mnt/card

Alternatively, you may want to set up a loopback device specifically for a partition using losetup:

# losetup /dev/loop0 disk.img -o $((10860003 * 512))
# file -s /dev/loop0
/dev/loop0: Linux rev 1.0 ext3 filesystem data
# mount /dev/loop0 /mnt
[...]
# umount /mnt
# losetup -d /dev/loop0


Troubleshooting[edit | edit source]

Samba Network Shares[edit | edit source]

The first time I ran mount -a, I got the following error:

root@server:~# mount -a
mount: wrong fs type, bad option, bad superblock on //box/Volume_1,
missing codepage or other error
In some cases useful info is found in syslog - try
dmesg | tail or so

with dmesg showing:

CIFS VFS: No username specified
CIFS VFS: cifs_mount failed w/return code = -22

Error -22 means the executable doesn't exist. To resolve this, install cifs-utils

# yum -y install cifs-utils


See Also[edit | edit source]

  1. http://madduck.net/blog/2006.10.20:loop-mounting-partitions-from-a-disk-image/
  2. https://en.wikipedia.org/wiki/Fstab