Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
You're familiar with sudo, right? –  slm Nov 16 '13 at 1:42

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
    
yes i usually do but is it really necessary to accomplish what I am trying to do? thanks. –  fcukinyahoo Nov 15 '13 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 '13 at 21:24

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.