Answer the question
In order to leave comments, you need to log in
What is the correct way to use NOPASSWD?
Hello. The task is this. Run script as sudo without password as user 'user'.
In /etc/sudoers I did this: user ALL=NOPASSWD: /opt/scripts/create_user
output sudo -l
Matching Defaults entries for 'user' on s-apps:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin
User 'user' may run the following commands on s-apps:
(root) NOPASSWD: /opt/scripts/create_user
(ALL : ALL) ALL
sudo -u user /bin/bash create_user user_xe1qGnFNmatHzycPKKvk PXeV5NSIhhhiSRg7OHxk
#!/bin/bash
#Create user
useradd -d /home/$1 -m $1 -s /bin/bash
#Create dir
mkdir /opt/jails/$1
#Include staf
jk_init -v -j /opt/jails/$1 sftp scp
jk_init -j /opt/jails/$1 jk_lsh
jk_init -j /opt/jails/$1 basicshell
jk_init -j /opt/jails/$1 extendedshell
jk_cp -v -f /opt/jails/$1 /bin/bash
#Add user in jail
jk_jailuser -m -j /opt/jails/$1 $1
#Set password for user
echo "$1:$2" | chpasswd
Answer the question
In order to leave comments, you need to log in
Apparently like. In sudoers, the user user should be allowed to execute commands:
And there should be write permissions to /opt/jails/
In general, insert echo after each command and catch where it doesn’t work.
So in sudoers the user 'user' is given the rights to run /opt/scripts/create_user as root, and the command runs /bin/bash on behalf of user'a.
Nowhere can they really answer normally, damn it. It's time to settle the matter once and for all
sudo
time executed under the specified user (root by default) until the end of the command/script. Those. sudo /opt/scripts/create_user
in this case, it will execute all script commands as root. If you need to execute the command from under another user, add it -u <username>
to sudo.user ALL=NOPASSWD: /opt/scripts/create_user
, but the sequence is important here - the author overwrites this line with a rule ALL=(ALL) ALL
(written for a group of superusers, for example, in CentOS usually:) %wheel ALL=(ALL) ALL
, which is located below in sudoers file.man sudoers
on and may the force be with you.user ALL=NOPASSWD: /opt/scripts/create_user
below the rest,sudo /opt/scripts/create_user
- inside everything will be executed already under rootDidn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question