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.

Closest I can come is:

useradd --home / -r --shell /sbin/nologin someuser

But this creates an entry into /etc/htpasswd that looks something like this:

someuser:x:100:100::/:/sbin/nologin

I want that '/' gone, so that it looks like this:

someuser:x:100:100:::/sbin/nologin

Which is achievable through usermod:

usermod -d '' someuser

But I think this is a bit backwards.

Any ideas?

share|improve this question
    
I highly question the motivation for doing this. Look in /etc/passwd at all the existing entries for users like 'nobody', 'news', 'daemon', etc. All of them have the home directory set to something even though these accounts aren't real user accounts. On my system, a few accounts are set to /dev/null, but every single account has the home directory field set. Generally when linux/unix consistently does something a certain way, there's a reason for it, and you shouldn't try to go against it. –  Patrick Jul 7 '13 at 5:00
add comment

2 Answers

Firstly, I must agree with Jim that you should be very clear on your motivation to create such user account(s).

That being said:

useradd -d /REPLACEME someuser && perl -p -i -e 's/\/REPLACEME//;' /etc/passwd



This assumes, of course, that /REPLACEME is not used anywhere else as a valid home directory. The more complex, but more precise, expression:

's/(someuser:.*:)\/REPLACEME(:.*)/$1$2/'

will ensure that only the user created by useradd above is modified.

share|improve this answer
    
My motivation for it is that I am jailing an apache instance in a chroot, and I am starting the apache instance as this user. There is no need for the user to have a home directory at all. Thank you for your reply, I will consider doing that, but I'm a bit reluctant to parse the file as text. –  mattis Nov 18 '10 at 7:37
    
I see. I can understand your reluctance as someone who has inadvertently made text edits. If you would like I can modify the perl to be more robust and perform various validation before making its edit. It might also be beneficial to have perl print what text it will be modifying before making any changes. Additionally you can pass a string to the -i flag* to create an in-place backup file. *perl -p -i.bak -e ... –  Tok Nov 18 '10 at 14:16
add comment

Really, it is not backwards. An account with no login directory cannot login directly. It can own resources and files. What are you trying to do?

Does useradd complain without --home?

You can add users directly with vi or vim. But you have to be painfully careful. You also have added any groups ahead of time.

share|improve this answer
    
useradd without --home will usually make the directory field using some default, often /home/username. –  Dennis Williamson Nov 17 '10 at 21:38
    
I haven't done Linux for a long time. Solaris useradd creates :/: for home by default. –  jim mcnamara Nov 17 '10 at 21:48
    
A user can login without a home directory, by default the most systems will set $HOME to / –  echox Nov 17 '10 at 22:41
    
Thank you for your reply. I am creating this user because I am jailing an apache instance in a chroot, and want to start the apache process as this user. I just can't see a reason why that user needs anything in the HOME_DIR field in /etc/passwd –  mattis Nov 18 '10 at 7:40
add comment

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.