LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-19-2020, 11:27 AM   #1
danmartinj
Member
 
Registered: Oct 2009
Posts: 117

Rep: Reputation: 1
Help Writing Sudoers Command Alias Complex Command


Hello All,

I have been trying to figure out how to get a complex command to work in the sudoers file over the past couple days using visudo but have failed and I do not know what else to try.

Basically, the command I want to work is this:
Code:
lsmod | grep -q _conntrack_ipv4 && iptables -L -n -v -t nat
And a couple others but I figure if I can get at least one to work the others will not be so hard.

I have a test user created and can get some other commands to work with sudo such as:
Code:
testuser ALL = (root) NOPASSWD: /usr/bin/cat /sys/kernel/debug/x86/pti_enabled, /usr/sbin/dmidecode
However, for the more complex commands I need to get to work it does not. I have tried using things like wild cards (*), played around with using quotes (" commandline args ..."), and etc but looking in the log files it looks like it is only seeing just the initial command before the pipe and nothing else:

Code:
sudo: testuser : TTY=pts/1 ; PWD=/home/testuser ; USER=root ; COMMAND=/sbin/lsmod
So, perhaps someone else can provide me some advise or help as it would be greatly appreciated right now.

Thanks,
Joe
 
Old 04-19-2020, 11:38 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Just put the commands into a shell script, put the script into /usr/local/sbin and reference it from sudoers.
 
Old 04-19-2020, 11:51 AM   #3
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by danmartinj View Post
it looks like it is only seeing just the initial command before the pipe and nothing else:
As far as I know sudoers does NOT use a shell, unless you specify a shell script (with a starting shebang en.wikipedia.org/wiki/Shebang_%28Unix%29 to specify the shell) to execute, so shell constructs like pipelines, redirection, command concatenation and/or wildcards are not available TO it. It rather specifically looks at the command you have given in the suduers file itself.
 
Old 04-19-2020, 01:26 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
additionally you do not need to sudo grep:
Code:
sudo <your command> | grep whatever
may work and you only need to use sudo for <your command>

The best advice (just to underline it) is to write a shell script and use sudo to execute it.
 
Old 04-19-2020, 04:35 PM   #5
danmartinj
Member
 
Registered: Oct 2009
Posts: 117

Original Poster
Rep: Reputation: 1
Hello,

Thanks for the great comments as now it does make sense to me what the problem is and I feel like there is not solution by just editing the sudoers file. However, I am not sure I like the idea of creating a dedicated shell script either. I have been trying to create a command alias in the test users .bashrc file as some kind of middle ground but so far it has not yet been working. Does everyone think this should work at least?
 
Old 04-19-2020, 05:22 PM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
As ehartman stated above sudoers doesn't use a shell. Thus, it knows nothing about shell aliases. Moreover, sudo is very restrictive about command environment and by default inherits only a few environment variables from the user.

You may use shell-style wildcards when specifying commands in sudoers though, but be aware of some quirks. From sudoers(5) manpage:
Quote:
Note that the following characters must be escaped with a ‘\’ if they are used in command arguments: ‘,’, ‘:’, ‘=’, ‘\’.
Quote:
Character classes may be used if your system's glob(3) and fnmatch(3) functions support them. However, because the ‘:’ character has special meaning in sudoers, it must be escaped. For example:
Code:
/bin/ls [[\:alpha\:]]*
Would match any file name beginning with a letter.

Note that a forward slash (‘/’) will not be matched by wildcards used in the file name portion of the command. This is to make a path like:
Code:
/usr/bin/*
match /usr/bin/who but not /usr/bin/X11/xterm.

When matching the command line arguments, however, a slash does get matched by wildcards since command line arguments may contain arbitrary strings and not just path names.

Wildcards in command line arguments should be used with care.
Command line arguments are matched as a single, concatenated string. This mean a wildcard character such as ‘?’ or ‘*’ will match across word boundaries, which may be unexpected. For example, while a sudoers entry like:
Code:
%operator ALL = /bin/cat /var/log/messages*
will allow command like:
Code:
$ sudo cat /var/log/messages.1
It will also allow:
Code:
$ sudo cat /var/log/messages /etc/shadow
which is probably not what was intended. In most cases it is better to do command line processing outside of the sudoers file in a scripting language.
As you can see, even if wildcards are allowed in command arguments in sudoers, using them there is not the best idea.

But putting all that aside, just imagine security implications of shell aliases being allowed in sudoers. They are controlled by user and could be changed on whim. How about alias lsmod='rm -rf /'?

Last edited by shruggy; 04-20-2020 at 02:35 AM.
 
1 members found this post helpful.
Old 04-19-2020, 11:58 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A sudo script must be protected. I.e. root-owned, and stored at a root-owned location like /usr/local/bin/.

Alternatively use sudo for each sub-command:
Code:
sudo lsmod | grep -q _conntrack_ipv4 && sudo iptables -L -n -v -t nat
This can be stored in a script or alias without further protection.
Sudoers example:
Code:
%operator ALL = /usr/sbin/lsmod, /usr/sbin/iptables -L *
 
Old 04-20-2020, 12:41 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I wouldn't recommend you anything in ~user/.bashrc because the user (owner) can modify it. Also alias is not a good idea, because it is not handled very well among different shells (for example when sudo was executed).
 
Old 04-20-2020, 12:46 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
Quote:
Originally Posted by danmartinj View Post
Hello,

Thanks for the great comments as now it does make sense to me what the problem is and I feel like there is not solution by just editing the sudoers file. However, I am not sure I like the idea of creating a dedicated shell script either. I have been trying to create a command alias in the test users .bashrc file as some kind of middle ground but so far it has not yet been working. Does everyone think this should work at least?
I'd reiterate what has been written already: writing a short script is the way to accomplish your task and, specifically, that script must be not be writable in any way, directly or indirectly, by any other account than root. So keeping it in /usr/local/bin/ or /usr/local/sbin/ would be one good choice, since that's where scripts generally go anyway: A place for everything and everything in its place, see "man hier".

Scripting is a useful and important part of using the computer. It might seem challenging at first but it's not hard once you get familiar with the process.
 
Old 04-20-2020, 02:26 AM   #10
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by shruggy View Post
You may use shell-style wildcards when specifying commands in sudoers though, but be aware of some quirks.
Didn't know that. Thanks!
But I'm not really a sudo user, so I do not go that deep into the sudoers file.
 
Old 04-20-2020, 02:17 PM   #11
danmartinj
Member
 
Registered: Oct 2009
Posts: 117

Original Poster
Rep: Reputation: 1
Quote:
Alternatively use sudo for each sub-command:
MadeInGermany has a good idea. However, I tried that and it does not work. I believe the pipe (I) and And (&&) breaks this functionality. Basically from my understanding you can only alias a single command from the sudoers file. This means that no other shell functionality will work and so breaking up the original command will not work.

Honestly, in my case another application is running the:

sudo lsmod | grep -q _conntrack_ipv4 && sudo iptables -L -n -v -t nat

which means that application has to run that command exactly and cannot run a separate script unless that script is aliased as was my last idea I tried.

So, at this time my feeling is what I am trying to do cannot be done as the more I think about it creating a script and aliasing from a users profile does not seem like a valid solution either. If someone else has any other ideas please let me know. Either way thanks again for everyone's time as I definitely have learned from this.

Joe
 
Old 04-20-2020, 05:52 PM   #12
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by danmartinj View Post
cannot run a separate script unless that script is aliased as was my last idea I tried.
Aliasing is a shell function too (and is rather "which shell" dependant). So you cannot use any aliases in a sudoers file.
And - for instance - sourcing an executable through the PATH is a shell task too, the sudoers file need absolute paths to the commands to be executed.

Last edited by ehartman; 04-20-2020 at 05:53 PM.
 
Old 04-21-2020, 12:07 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I would suggest you to write a script like this (put into /usr/local/bin/myscript):
Code:
mode=$1
shift
function iptables_func() {
    .....
}

case $mode in
   lsmod) (call its own function) 
   iptables) iptables_func "$@";;
   ....
   *) echo "incorrect mode" >&2; exit 1;;
esac
invoke it like this: myscript <subcommand> <arguments>
and you can make alias for the users like:
Code:
alias iptables=sudo /usr/local/bin/myscript iptables
and it will invoke the function you put into that script (as root).
 
  


Reply

Tags
sudoers



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: how to avoid a command inside an alias being used as another alias? dedec0 Linux - Newbie 25 12-11-2017 10:20 AM
[SOLVED] User not in sudoers: How to add user? Permtion Denied for sudoers file esgol Linux - Newbie 3 07-13-2012 07:44 AM
Fedora /etc/sudoers file and sudoers.d directory davejjj Linux - Newbie 2 10-21-2011 06:19 PM
Alias question (2 commands for one alias) gflores Linux - Newbie 3 01-21-2006 12:40 AM
I deleted /etc/sudoers and creates a new file call sudoers but now it doesnt for visu abefroman Linux - Software 1 11-10-2005 05:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration