Quickly check if user can sudo
Here’s a snippet to quickly test if a user can run sudo commands. For this to work you need to know which group is your system’s sudoers group. You can find that in the
/etc/sudoers file. The relevant line looks like
%admin ALL=(ALL:ALL) ALL where ‘admin’ is the name of the group.
groups $(logname) | grep admin && echo yay || echo noo
For example to check sudo permissions in order to decide if the username should get system stats on login, you can do this:
#!/bin/bash canSudo=$(groups $(logname) | grep admin) # not a sudoer, exit quietly if [ !$canSudo ]; then exit 0; fi # hello admin echo HOME=/home/$(logname) /usr/bin/pydf -h echo
HOME= line. Scripts in
update-motd.d run as the root user without useful environment variables. As some commands like pydf require the
HOME var, you need to set it yourself. The command logname is doing all the magic to get the right username.