How to mount a hard disk as read-only from the terminal

How do you mount a hard disk from the command line as read-only? I don't want or need a link to the man page, I want the exact thing I will have to type in if the following is true:

  • disk to mount is on /dev/sda
  • it is 2 TB -it is critical that I mount it read-only and not read-write. Very critical.
  • I'm doing it from a live Ubuntu CD so I have no business to edit the fstab or any file for that matter

Solution 1:

You do not mount /dev/sda, that refers to the entire disk. You mount /dev/sda1 or whatever partition you want.

Make a mount point, call it anything you like.

sudo mkdir /media/2tb

Mount

sudo mount -o ro /dev/sda1 /media/2tb

When your done, you should unmount the disk

sudo umount /media/2tb

See man mount or https://help.ubuntu.com/community/Fstab

Solution 2:

When mounting the filesystem read-only, some trouble may happen. The system may try to write into the device anyway and fail.

For that reason the noload flag may be used, to notify to the system that the disk is blocked.

The best solution I found was:

sudo mount -o ro,noload /dev/sda1 /media/2tb

The manual of mount(8) explains this options as follows:

-r, --read-only

Mount the filesystem read-only. A synonym is -o ro.

Note that, depending on the filesystem type, state and kernel behavior, the system may still write to the device. For example, Ext3 or ext4 will replay its journal if the filesystem is dirty. To prevent this kind of write access, you may want to mount ext3 or ext4 filesystem with ro,noload mount options or set the block device to read-only mode, see command blockdev(8).

[…]

norecovery/noload

Don't load the journal on mounting. Note that if the filesystem was not unmounted cleanly, skipping the journal replay will lead to the filesystem containing inconsistencies that can lead to any number of problems.

For more info see the great explanation in “How to Mount Dirty EXT4 File Systems” on the SANS Digital Forensics and Incident Response Blog.

Solution 3:

I am plugging a USB connected drive into Ubuntu 12.04 and the system is mounting it automatically. In Terminal, if I just say mount it shows me the current info. I want to remount it read-only.

Extrapolated from man mount(8):

sudo mount -o remount,ro /dev/sdb4 /media/HP_TOOLS

Seemed to work nicely. Had to do it for each automounted partition.

Solution 4:

Step 1: After connecting the disk to the machine, give the command below to see what it shows the disk as.

sudo fdisk -l

It will show the disk as /dev/sda or /dev/sdb with a partion table.

Disk /dev/sdb: 7.5 GiB, 8053063680 bytes, 15728640 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: 0x0e0e8e70

    Device     Boot   Start     End Sectors  Size Id Type
    /dev/sdb1  *          0 2902111 2902112  1.4G  0 Empty
    /dev/sdb2       2888004 2892739    4736  2.3M ef EFI (FAT-12/16/32)

Step 2: Execute the command below to see where it is mounted. For example,

$ sudo df -HT

Filesystem                  Type      Size  Used Avail Use% Mounted on
udev                        devtmpfs  4.2G     0  4.2G   0% /dev 
tmpfs                       tmpfs     829M   10M  819M   2% /run
/dev/mapper/ubuntu--vg-root ext4      484G  149G  311G  33% /
tmpfs                       tmpfs     4.2G   20M  4.2G   1% /dev/shm
tmpfs                       tmpfs     5.3M  4.1k  5.3M   1% /run/lock
tmpfs                       tmpfs     4.2G     0  4.2G   0% /sys/fs/cgroup
/dev/sda1                   ext2      495M  111M  359M  24% /boot
/dev/sdb1                   iso9660   1.5G  1.5G     0 100% /media/username/Ubuntu

Step 3: Finally execute the command below to remount it as an ro only.

sudo mount -o remount,ro /dev/sdb1   /media/username/Ubuntu