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

I'm trying to find out the total size of my hard drive using df -h, but it prints the size of each partition and requires sudo.

Is there any way to get total size of my hard drive using the command line, without requiring root permission?

1

7 Answers

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...

4

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
4
cat /sys/block/sda/size

But the size is in block-based unit
I found this answer here.
Other solutions here.

Or you can try udisks

udisks --show-info /dev/sda | egrep "^[[:space:]]*size" | awk '{print $2}'

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).

2

I'm not at my Linux box right now, but you could try:

hdparm -I /dev/sda

or

fdisk -l

which will also probably require a sudo.

I haven't seen anywhere that df requires root. That's odd.

4

You either have a bad install or an absurdly paranoid sysadmin. I've never heard of df requiring root privileges before. And I've worked with quite a few different flavors of unix/unix-like operating systems.

Anyhow, this will give you the total size of all disk partitions (first df column is /dev/hd... or /dev/sd...) in blocks (kB):

df | grep '^/dev/[hs]d' | awk '{s+=$2} END {print s}'

or in GB:

df | grep '^/dev/[hs]d' | awk '{s+=$2} END {print s/1048576}'
1

Checking Disk Space From Gnome and Kubuntu

Disk Usage Analyzer is a graphical menu driven application that reports disk usage in Gnome and Kubuntu environments. DUA (Disk Usage Analyzer) can scan the entire file system tree or individual directories, either local or remote. DUA is also dynamic, in that it will report in real time any devices that are mounted and unmounted.

To access Disk Usage Analyzer in Gnome, click on: Applications \ Accessories \ Disk Usage Analyzer its that simple.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like