0

I'm trying to get an oracle 6.2 system (same as redhat 6.2) to find all the users on the system, then take the username of each user discovered and query group membership of each user individually, and output to a file once a month, so I need to write the command as a shell script and schedule

I'm pretty new to Linux, and there seems to be many ways to call users and group memberships. I am currently using the below to output a list of users to file.

getent passwd >> /home/username/users.txt

I don't really know what to do next. The output always puts the username first, so I would like to grep the file for the first word of every line, and run

getent group | grep %

where % is the first word of every line, and to repeat that process until the end of the file is reached, then stop, and dump the output to a network location (windows share) that would be \windowsserver\share\usersandgroups.csv so I can open the file in excel, and hopefully have the username in cell A1, followed by each group in their own cells (B1, C1, D1 etc) with each user on a new row. If getent isn't ideal for this, can anyone suggest a command that'll do the job? I need the users and groups names as opposed to ID's.

I don't necessarily need to output the users to a file, so if that step can be avoided, that would be useful.

Thanks to anyone that can help.

1

Here's a one liner:

getent passwd | awk -F ':' {'print $1'} | xargs -I {}  groups {} | sed 's/ : /:/g' | tr ' ' ','

This dumps straight to console like so:

pulse:pulse
shearn89:shearn89,apache,monit,wireshark

UPDATE: To get this in 'pure' .csv format, you could use this:

getent passwd | awk -F ':' {'print $1'} | xargs -I {}  groups {} | sed 's/ : /,/g' | tr ' ' ','

This would give you user,group1,group2,group3, so for users who are only in 'their' group, you'd get pulse,pulse (for example).

  • Thanks, that seems to have worked to dump to console, which solves half the problem. I'll have to try and figure out how to get that to a file on a windows box ( >> (network location)). Also, it didn't like the space between { and 'print $1', so I removed it. I also don't understand some of the output. Some users are coming up as 'User:Group', others come up as 'User:User', as per your example (Pulse:Pulse). I would have expected to see 'Pulse:Users', same as 'Games:Users' and not 'Pulse:Pulse'. This is likely my misunderstanding. Does it mean they have no group membership/extra permissions? – Simkill Jan 14 '15 at 15:59
  • Ah, I'll edit the answer for the space, that's a typo! The pulse:pulse example is because many Linux distributions automatically create a group for the user on creation, with the name of the user. So the pulse user is only a member of the pulse group, whereas I am a member of my group and also the apache,monit,wireshark groups. – shearn89 Jan 14 '15 at 16:02
  • To dump to a network location you'd need to share that from Windows, and mount it as a CIFS share on the Linux box. You could then write straight to it using redirects > /path/to/share/file.csv. – shearn89 Jan 14 '15 at 16:03
  • You're very welcome! – shearn89 Jan 14 '15 at 16:20
  • The first form works fine, at least on bash 4.2.46 with awk 4.0.2. – shearn89 Dec 19 '17 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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