[SOLVED] Execute smartctl as user without password

If none of the more specific forums is the right place to ask
Post Reply
Message
Author
User avatar
ticojohn
Posts: 1086
Joined: 2009-08-29 18:10
Location: /home/heart
Has thanked: 5 times
Been thanked: 17 times

[SOLVED] Execute smartctl as user without password

#1 Post by ticojohn »

I would like to run smartctl to check the temperatures on three drives in my desktop without having to use sudo, or logging in as root.
There are several reasons I want to this, but primarily I want execute smartctl from a script. And hhdtemp and the disks utility don't give the same results as does smartctl. Besides which hddtemp also requires root authorization which I am trying to circumvent.

I have looked at a number of different references including the sudoers man page. It seems pretty straightforward but I can't get it to work. I started by creating a file in /etc/sudoers.d

Code: Select all

#nano /etc/sudoers.d/username
Then I edited that file with the following, and then saved the file

Code: Select all

username ALL=(ALL) NOPASSWD:/usr/sbin/smartctl
Finally I changed the mode of that file to 0440 as required

Code: Select all

chmod 0440 /etc/sudoers.d/username
I then rebooted the machine to ensure that the sudo changes got loaded. But when I try to execute as user I get the following

Code: Select all

usrname@debian:~$ /usr/sbin/smartctl --all /dev/sda
smartctl 7.2 2020-12-30 r5155 [x86_64-linux-5.10.0-8-amd64] (local build)
Copyright (C) 2002-20, Bruce Allen, Christian Franke, www.smartmontools.org

Smartctl open device: /dev/sda failed: Permission denied
Can somebody tell me what I am doing wrong?

EDIT: Forgot to mention that I am running Bullseye amd64.
Last edited by ticojohn on 2021-08-01 23:33, edited 1 time in total.
I am not irrational, I'm just quantum probabilistic.

CwF
Posts: 1191
Joined: 2018-06-20 15:16
Has thanked: 2 times
Been thanked: 6 times

Re: Execute smartctl as user without password

#2 Post by CwF »

Use polkit.

Code: Select all

pkaction -l
this list the available actions to tweak

Combine those actions with the user info in a pkla file, placed by root in etc/polkit/localautority/.

The details are in post on these boards!

User avatar
ticojohn
Posts: 1086
Joined: 2009-08-29 18:10
Location: /home/heart
Has thanked: 5 times
Been thanked: 17 times

Re: Execute smartctl as user without password

#3 Post by ticojohn »

I thought that I might have found the problem and changed the line in the /etc/sudoers.d/username file to the following

Code: Select all

username ALL=(ALL:ALL) NOPASSWD:/usr/sbin/smartctl
No joy! :(
I am not irrational, I'm just quantum probabilistic.

User avatar
ticojohn
Posts: 1086
Joined: 2009-08-29 18:10
Location: /home/heart
Has thanked: 5 times
Been thanked: 17 times

Re: Execute smartctl as user without password

#4 Post by ticojohn »

CwF wrote: 2021-08-01 21:23 Use polkit.

Code: Select all

pkaction -l
this list the available actions to tweak

Combine those actions with the user info in a pkla file, placed by root in etc/polkit/localautority/.

The details are in post on these boards!
Okay. I appreciate your suggestion, and I'll take a look. But I'd like to know why what I am trying to do doesn't work. It is also well documented and SHOULD work, shouldn't it? I mean, I think it is even in the sudoers man page.
I am not irrational, I'm just quantum probabilistic.

CwF
Posts: 1191
Joined: 2018-06-20 15:16
Has thanked: 2 times
Been thanked: 6 times

Re: Execute smartctl as user without password

#5 Post by CwF »

ticojohn wrote: 2021-08-01 21:03 Can somebody tell me what I am doing wrong?
You need to include 'sudo'.

Code: Select all

usrname@debian:~$ sudo smartctl --all /dev/sda

User avatar
ticojohn
Posts: 1086
Joined: 2009-08-29 18:10
Location: /home/heart
Has thanked: 5 times
Been thanked: 17 times

Re: Execute smartctl as user without password

#6 Post by ticojohn »

CwF wrote: 2021-08-01 21:48
ticojohn wrote: 2021-08-01 21:03 Can somebody tell me what I am doing wrong?
You need to include 'sudo'.

Code: Select all

usrname@debian:~$ sudo smartctl --all /dev/sda
Uh Huh. Tried that. Still asks for a password and then it tells me i don't have permission. I am trying to avoid that and thought that by adding the file to /etc/sudoers.d I could avoid all that. I'll see if I can figure out how to do it with polkit, but what I've read looks somewhat intimidating. Maybe it''s just my 72 year old brain. :roll:
I am not irrational, I'm just quantum probabilistic.

CwF
Posts: 1191
Joined: 2018-06-20 15:16
Has thanked: 2 times
Been thanked: 6 times

Re: Execute smartctl as user without password

#7 Post by CwF »

ticojohn wrote: 2021-08-01 21:23 username ALL=(ALL:ALL) NOPASSWD:/usr/sbin/smartctl
instead

Code: Select all

username ALL=(ALL) NOPASSWD: ALL

User avatar
ticojohn
Posts: 1086
Joined: 2009-08-29 18:10
Location: /home/heart
Has thanked: 5 times
Been thanked: 17 times

Re: Execute smartctl as user without password

#8 Post by ticojohn »

CwF wrote: 2021-08-01 22:16
ticojohn wrote: 2021-08-01 21:23 username ALL=(ALL:ALL) NOPASSWD:/usr/sbin/smartctl
instead

Code: Select all

username ALL=(ALL) NOPASSWD: ALL
OOPS! Found the problem. I did not have my user in the sudoers list. DUMB! After supper I will try again. Thanks for the help @CwF
I am not irrational, I'm just quantum probabilistic.

User avatar
ticojohn
Posts: 1086
Joined: 2009-08-29 18:10
Location: /home/heart
Has thanked: 5 times
Been thanked: 17 times

Re: Execute smartctl as user without password

#9 Post by ticojohn »

Thanks @CwF. It now works as expected. It helps to pay attention to details. :mrgreen: :roll:
I am not irrational, I'm just quantum probabilistic.

Post Reply