2

I have a user user1 which is part of wheel group and has all the root privileges .

user1 is able to change password for user2 using the below command (My understanding is that here passwd is run as root):

[user1@rhel-85 ~]$ sudo passwd user2 

However, when user1 tries to run passwd as user2, I am getting an error:

[user1@rhel-85 ~]$ su -u user2 passwd Try 'su --help' for more information. [user1@rhel-85 ~]$ 
2
  • 2
    You seem to confuse sudo and su. These two programs have different command line syntax and slightly different purpose. Do you mean sudo -u user2 passwd? Commented Jan 27, 2022 at 18:09
  • 1
    You are right. This is working fine sudo -u user2 passwd. By the way, can you please help understand difference between sudo and su? Commented Jan 27, 2022 at 18:18

1 Answer 1

2

su is used to launch a root shell that allows all further commands to use root access. su has no -u option...

https://man7.org/linux/man-pages/man1/su.1.html

This is why it is saying/complains;

Try 'su --help' for more information.

You would need to add user1 to the wheel group, then as Bodo points out, execute;

sudo -u user2 passwd 

It's clear to me that sudo is your intended use here, as you are trying to execute a single command with temporary privilege elevation. (which is what sudo is for).

However, for completeness and as user Bib points out, you could execute;

su - _user_ foo 

...which would allow you to run as root for another user.

This is the 'login shell' option for su (-, -l or --login). This is also handy for granting root privileges in the scenario, where the user doesn't have sudo privileges (and where the admin don't want them to, either.).

2
  • 1
    Well, you sure as heck can use su - _user_ to run a command as another user. It's not limited to root. Commented Jan 27, 2022 at 18:26
  • yes, Bib. you can do that. that's the login shell option. I never said that wasn't a possibility or that su was limited to root... i was stating why they got the error and what the difference between su and sudo are. Commented Jan 27, 2022 at 18:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.