0

In most Linux distros I've used when you type for example ifconfig as a normal user, you get a prompt message telling you that only root can execute such command.

In Debian, it only says command not found (because it's looking for it in /usr/bin/).

I'm wondering if there is any way for prompt a message, like "ifconfig must be run as root" or something like that.

  • 5
    You should be able to execute ifconfig as a regular user. I have never seen ifconfig issuing such a prompt message in its default capacity. – Chris Down Dec 9 '12 at 17:44
  • 70% of the commands in */sbin don't need superuser privilege. If a command needs superuser priviledge, it will tell you. It's safer not to run a command as root, so I'd rather issue the opposite warning like beware, this is run as root when not needed. – Stéphane Chazelas Dec 9 '12 at 19:14
2

You can do it like this :

trap '
    if [[ $BASH_COMMAND =~ /sbin ]] && ((UID!=0)); then
        echo >&2 "${BASH_COMMAND##*/sbin/} must be run as root"
    fi
' DEBUG

But ifconfig is accessible for users too, just need to type /sbin/ifconfig.

Like Chris Down said, I have never seen ifconfig issuing such a prompt message in its default capacity.

  • +1 for telling me about =~ BASH_COMMAND and DEBUG in one post. But this solution will only work if the sbin folders are in path and the binary is actually about to be exectuted, won't it. This already implies execute rights for non root user which does semantically collide with the intention and error message. But I'm just trying to be smart :-) – Bananguin Dec 9 '12 at 19:11
  • yep, seem it was a problem of PATH. Btw, where should I write that? I tried in .bashrc but nothing changed. – yzT Dec 9 '12 at 20:54
  • Did you source your .bashrc or close and open a new terminal? – laebshade Dec 9 '12 at 21:51
0

In Debian, it only says command not found (because it's looking for it in /usr/bin/)

That just means your non-root users don't have /sbin in their PATH. Run /sbin/ifconfig explicitly and see what it says when you aren't root.

If the message is ok, you just need to add /sbin to your PATH.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.