This is a typical use case for sudo
.
You're mixing sudo
which allows running commands as another user and is highly configurable (you can selectively specify which user can run which command as which user) and su
which switches to another user if you know the password (or are root). su
always runs the shell written in /etc/passwd
, even if su -c
is used. Because of this su
isn't compatible with /usr/sbin/nologin
.
You should use
sudo -u secure /home/someuser/secure.script
As sudo
is configurable you can control who can use this command and if he/she needs to enter a password to run it. You need to edit /etc/sudoers
using visudo
to do this. (Be careful when editing /etc/sudoers and always use visudo to do it. The syntax isn't trivial and one error can lock you out from your root account.)
This line in sudoers allows anyone in group somegroup
to run the command as secure
:
%somegroup ALL=(secure) /home/someuser/secure.script
This allows anyone in group somegroup
to run the command as secure
without entering a password:
%somegroup ALL=(secure) NOPASSWD: /home/someuser/secure.script
This allows user1
to run the command as secure
without entering a password:
user1 ALL=(secure) /home/someuser/secure.script
sudo su
, see http://unix.stackexchange.com/questions/218169/is-there-ever-a-good-reason-to-run-sudo-su – Kusalananda Apr 13 '17 at 10:01