Get total size of my hard drive in Linux, using the command line, without root permissions?

A hacky way is to bypass the need for sudo by reading out the system log with:

dmesg | grep blocks

Please note that this might not be ideal, so your mileage may vary...


If you want the size in bytes and only the value e.g. for scripting:

lsblk -b --output SIZE -n -d /dev/sdX
12220202

-b: Output in bytes.
-n: No headings. We only want the pure number value.
--output SIZE: Only print the size-column.
-d /dev/sdXn: The device which size we want to know. X is e.g. d, n is e.g, 1 for first partition of disk d.

Advantages:

  • root privileges are not needed
  • grep is not needed
  • lsblk is available on most linux systems

If you want to use that information in a script for example, using

sudo blockdev --getsz /dev/sda

might be easier than fdisk or hdparm as it only gives you the relevant information (just multiply by 512). However, it will also require sudo.

You could of course change the configuration of sudo such that it allows to execute this specific command without asking for the password (I guess blockdev --getsz is pretty safe even when executed by a normal non-privileged user).

This would be done by adding the following line to /etc/sudoers:

ALL ALL= NOPASSWD:/sbin/blockdev --getsz /dev/*

When you edit this file, be sure to use the command sudo visudo and not your usual editor. Otherwise it is very easy to make a syntax error, which would result in not being able to use sudo anymore (you would have to reboot into rescue mode to fix this).