8

I've created a user on CentOS with useradd -M username. Now, I want to become that user just like su - username.

It asks me for a password for that user but I didn't set a password for that user. As a workaround, I have to become root then su - username works obviously.

First, I thought it asks for a root password but it is not the case. Any ideas?

1
  • You're familiar with sudo, right?
    – slm
    Nov 16, 2013 at 1:42

2 Answers 2

13

After creating the account with the command useradd you need to run the following command as root to set a password for this newly created account:

$ passwd <username>

Sudo

If you're absolutely positive that you, (userZ), want to become another user (userX) without providing userX's password, AND you don't want to have to become root first then you're likely looking for sudo.

The CentOS Wiki does a very good job of covering this entire topic that you're asking about here in this articled titled: How To Become Root.

Here's a synopsis.

  1. You want the ability to do anything as root, such as, becoming other users, without providing their password. Add the following rule to your sudoers file, /etc/sudoers. NOTE: You edit the sudoers file using the command visudo as root!

    userZ    ALL=(ALL)       ALL
    

    The above approach will still challenge you for userZ's password. You can get rid of this protection by adding this rule to sudoers instead.

    userZ ALL=NOPASSWD: ALL
    
  2. You want to parcel out the ability to only become a specific user using sudo.

    userZ ALL=(root) NOPASSWD: /bin/su - userX
    

    NOTE: The above can also be done so that userZ has to use their password to run the su command too. The above only let's userZ become userX, nothing more.

3
  • 1
    yes i usually do but is it really necessary to accomplish what I am trying to do? thanks. Nov 15, 2013 at 21:20
  • @fcukinyahoo - I don't understand your Q then. You always have to create a password for a user account. If it's a system account you're creating then you likely don't need the - since the account typically doesn't have a home directory that would need to have dot files sourced as part of the su.
    – slm
    Nov 15, 2013 at 21:24
  • I tried userZ ALL=(root) NOPASSWD: /bin/su - userX, and then as userZ, I ran su - userX (both users exist). But it still prompted for password. Any idea?
    – HCSF
    Dec 22, 2021 at 7:03
1

It asks me for a password for that user

That's how it works. If you're not root, you can't assume another user's identity without their password.

If CentOS comes with sudo installed you could add

youruserid ALL = (username) NOPASSWD: ALL

with visudo and then sudo -u username bash is like su - username

(edit: tried to type sudo specs w/o checking)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .