LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2009, 04:00 PM   #1
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Rep: Reputation: 15
No entries in /etc/passwd


I know that /home contains all the home directorys for all users. I also know that /etc/passwd current list of all valid users. However, I am trying to figure out a command that lists users that do not have entries within the /etc/passwd.

I know that using the following command:

Code:
$ awk -F":" '{ print $1 }' /etc/passwd
and

$ cat /etc/passwd
Lists all the users. But how do I go about writing a command that lists users that do not have entries in /etc/passwd?
 
Old 03-31-2009, 04:17 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
First of all - not *all* users have a home under /home ... most system
accounts don't. The easiest way to determine whether someone who has
a home under /home, but no actual account anymore is to just look at
the long output of ls.

This is true on most variants (with default installations) of Linux/Unix
that I've come across: if the 3rd field is numeric only, the user isn't
in /etc/passwd anymore ...


Cheers,
Tink
 
Old 03-31-2009, 04:20 PM   #3
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Okay, I'm lost. lol
 
Old 03-31-2009, 04:23 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
ls -l /home | awk '$3 !~ /a-zA-Z/'
 
Old 03-31-2009, 04:26 PM   #5
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thank you, trying that.

This is what I see when I enter in the command that you gave me:

Code:
$ ls -l /home | awk '$3 !~ /a-zA-Z/' | more
total 51772
d---rwx--t  12 directory aadams01    4096 Sep 21  2006 aadams01
d---rwx--t   5 directory aadams03    4096 Dec  4 21:31 aadams03
d---rwx--t   4 directory aadams24    4096 Jan  3  2008 aadams24
d---------   2 root      root        4096 Jan 27 13:13 aaebig01
So how do I know which one has any entries in /etc/passwd?

Last edited by shorte85; 03-31-2009 at 04:29 PM.
 
Old 03-31-2009, 04:27 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
To elaborate: in Unix, the file-ownership is determined by
numeric user and group ids (the third field in /etc/passwd,
and /etc/group). Linux commands like ls will be "friendly"
and map the numbers to names by default. If someone doesn't
exist in password any more, you'll see a numeric only ID in
the output of ls.
 
Old 03-31-2009, 04:33 PM   #7
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
So how would I go about having it just list the ones with no entries into /etc/passwd, instead of all? I hope that makes sense.
 
Old 03-31-2009, 04:37 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
In your example above they all do have a numeric owner.
root or directory. Whether that's desired or not I cannot
tell ;}
 
Old 03-31-2009, 04:43 PM   #9
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Maybe I didn't explain what I wanted correctly in the first place? Let me see if I can explain a little better. (puts on thinking cap)

I guess this is what I am trying to figure out for a command to do. I am trying to figure out a command that list users who have home directories, but do not have entry in /etc/passwd.

Maybe I did word it wrong the first time?
 
Old 03-31-2009, 04:44 PM   #10
dandart
Member
 
Registered: Dec 2005
Posts: 34

Rep: Reputation: 18
If it's a number instead of a user, their account has been deleted.

If it's not in /etc/passwd, it's not a user.
 
Old 03-31-2009, 04:45 PM   #11
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
You could try this:
Code:
for dir in $(ls -d /home/*)
do
   name=${dir#/home/} # remove the leading /home/
   if ! grep '^$name:' /etc/passwd # if no line in passwd begins with $name<colon>
   then
      echo $name
   fi
done
Or, more simply, you could make this the test:
Code:
if ! grep "$dir' /etc/passwd # if no user has this directory set as home
a user's home directory is the sixth field in /etc/passwd - which, incidentally need not be in home or have a name that matches the username, as Tinkster said.

Rob

Last edited by Robhogg; 03-31-2009 at 04:50 PM.
 
Old 03-31-2009, 04:54 PM   #12
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
I don't know, I am so confused now. lol All I am trying to do is get a list of users who have home directories, but do not have entry in /etc/passwd. But I am not sure if I am wording it correctly as to what I am after.
 
Old 03-31-2009, 05:15 PM   #13
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
OK - I added a directory called "george" to my /home (no such user - there's also no user called lost+found!):
Code:
rob:~$ ls -d /home/*
/home/george  /home/lost+found  /home/rob
rob:~$ for dir in $(ls -d /home/*); do name=${dir#/home/}; \
if ! grep "^$name:" /etc/passwd >/dev/null; then echo $name; fi; done
george
lost+found
I don't know of a single command to do this - it does take a little scripting.
 
Old 03-31-2009, 05:50 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by shorte85 View Post
I don't know, I am so confused now. lol All I am trying to do is get a list of users who have home directories, but do not have entry in /etc/passwd. But I am not sure if I am wording it correctly as to what I am after.
First of all my apologies - I forgot the character class
braces around my awk regex.

It should have said:
Code:
ls -l /home | awk '$3 !~ /[a-zA-Z]/'
I think the basic problem you're facing is that you're
setting (in your mind) a user name to be equivalent to
the name of the users home directory. While this commonly
is the case, it's not always like that, and it's certainly
not required. When you say you want to find a directory
whose owner isn't in /etc/passwd looking at the names is
technically speaking not correct. However, it would appear
that in your practical example output above there's a whole
lot of directories under home owned by two users whose names
aren't reflected in the directories names, namely a user
directory and root.

If the directories above weren't owned by anyone I'd expect
to see something like this in ls' output.
Code:
ls -l /home | awk '$3 !~ /[a-zA-Z]/' | more
total 51772
d---rwx--t  12 1009      aadams01    4096 Sep 21  2006 aadams01
d---rwx--t   5 1019      aadams03    4096 Dec  4 21:31 aadams03
d---rwx--t   4 1005      aadams24    4096 Jan  3  2008 aadams24
d---------   2 1004      root        4096 Jan 27 13:13 aaebig01

Cheers,
Tink
 
Old 03-31-2009, 06:18 PM   #15
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
I give up. But thanks for your guy's help! I appreciate it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to sort output at latest entries without disturbing the previous entries record nabmufti Programming 4 02-11-2008 11:36 PM
/etc/passwd entries needed in /etc/security/access.conf? bbtg Linux - Enterprise 3 10-16-2007 07:47 AM
Best way to copy passwd entries rtcary Linux - Newbie 3 07-17-2005 01:42 PM
scrpit for finding out new entries in /etc/passwd icefairy83 Linux - Security 8 05-27-2005 11:35 AM
Default entries in /etc/passwd - Slackware J.W. Linux - Security 1 09-23-2003 04:15 PM


All times are GMT -5. The time now is 10:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration