LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   No entries in /etc/passwd (https://www.linuxquestions.org/questions/linux-newbie-8/no-entries-in-etc-passwd-715879/)

shorte85 03-31-2009 04:00 PM

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?

Tinkster 03-31-2009 04:17 PM

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

shorte85 03-31-2009 04:20 PM

Okay, I'm lost. lol

Tinkster 03-31-2009 04:23 PM

Code:

ls -l /home | awk '$3 !~ /a-zA-Z/'

shorte85 03-31-2009 04:26 PM

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?

Tinkster 03-31-2009 04:27 PM

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.

shorte85 03-31-2009 04:33 PM

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.

Tinkster 03-31-2009 04:37 PM

In your example above they all do have a numeric owner.
root or directory. Whether that's desired or not I cannot
tell ;}

shorte85 03-31-2009 04:43 PM

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?

dandart 03-31-2009 04:44 PM

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.

Robhogg 03-31-2009 04:45 PM

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

shorte85 03-31-2009 04:54 PM

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.

Robhogg 03-31-2009 05:15 PM

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.

Tinkster 03-31-2009 05:50 PM

Quote:

Originally Posted by shorte85 (Post 3494184)
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

shorte85 03-31-2009 06:18 PM

I give up. But thanks for your guy's help! I appreciate it.


All times are GMT -5. The time now is 03:26 PM.