Vous pouvez le faire avec suou sudosans avoir besoin des deux.
sudo -H -u otheruser bash -c 'echo "I am $USER, with uid $UID"'
Les parties pertinentes de man sudo:
-H The -H (HOME) option requests that the security policy set
the HOME environment variable to the home directory of the
target user (root by default) as specified by the password
database. Depending on the policy, this may be the default
behavior.
-u user The -u (user) option causes sudo to run the specified
command as a user other than root. To specify a uid
instead of a user name, use #uid. When running commands as
a uid, many shells require that the '#' be escaped with a
backslash ('\'). Security policies may restrict uids to
those listed in the password database. The sudoers policy
allows uids that are not in the password database as long
as the targetpw option is not set. Other security policies
may not support this.
sune peut changer d’utilisateur sans fournir de mot de passe si vous êtes root. Voir la réponse de Caleb
Vous pouvez modifier le /etc/pam.d/sufichier pour permettre susans mot de passe. Voir cette réponse .
Si vous modifiez votre fichier d'authentification comme suit, tout utilisateur appartenant à un groupe somegrouppeut le sufaire otherusersans mot de passe.
auth sufficient pam_rootok.so
auth [success=ignore default=1] pam_succeed_if.so user = otheruser
auth sufficient pam_succeed_if.so use_uid user ingroup somegroup
Puis testez depuis le terminal
rubo77@local$ su otheruser -c 'echo "hello from $USER"'
hello from otheruser
suaussi?