Linux : how to search for a user and delete his account

Posted on 2014-02-28
Last Modified: 2014-03-13
Hi , i have 50 servers and i need to look for a specific account h80613 ( hs name is Jimmy) and if found on any file etc/pawwd , etc/sudoers , i need the account removed.

whats the easiest way to do this ?
Question by:c_hockland
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
LVL 23

Expert Comment

ID: 39895318
userdel h80613

Author Comment

ID: 39895350
will that remove the entry from all files ?
LVL 35

Expert Comment

by:Seth Simmons
ID: 39895391
it will remove the account and from /etc/passwd
any files or folders the user owned will only show UID/GID since the mapped user name is gone
also need to manually remove sudo access
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

LVL 13

Expert Comment

ID: 39895423
in case you want to delete home directly with it use

#userdel -r <userame>


Expert Comment

ID: 39896342

userdel -r username will help with part of that, but if the user create any file on an other path i won't be removed

so i will do the following:

userdel -r username -- > remove user and his home directory

find / -user h80613  -exec rm -rf {} \; -- will look for everything that own to that user and then delete it .

I hope this helps
LVL 23

Expert Comment

ID: 39896535
@Pepe2323 - If you delete the user first, your find command will not return any results because the user doesnt exist and permissions will show his/her UID and GUID.

You can do them in reverse and it will work.
LVL 13

Expert Comment

ID: 39896764
savone rocks

Expert Comment

by:Dave Gould
ID: 39896905
I think you've missed the main point though. The OP needs to do this on 50 servers. I presume he wants to centralise the work. If this is true, we need to know if he has any platform installed for centralised package managment or if ssh keys have been exchanged between servers in order to run a script from a single server.
Also, he mentioned /etc/sudoers. Deleting the user will not remove him from this file either so that would be a manual job. This could be done using sed but people always say not to manually update this file. It is recommended to use visudo.
LVL 30

Expert Comment

ID: 39897461
If the user account created files in another path, you might just want to archive it or chown it rather than remove it.  I don't delete user content when the account is disabled.  I'm required to save it, so it really depends on what required.

If the /etc/sudo files are identical, he could run visudo on one system and then scp or rsync it over to the other 49.

visudo, vipw, userdel, and useradd are recommended to prevent errors in file formatting.  If you are absolutely sure about the edits, you could just edit the files directly or replace them.

Newer admins with less knowledge of the inner workings of the system should definitely use visudo, vipw, useradd, and userdel.

Author Comment

ID: 39900643
my boss gave me this one as a hint

for x in `< servers_linux`; do echo ""; echo $x; ssh -l h10267 $x "grep h66148 /etc/passwd"; done

where servers_linux is the file that contains the names of all ( 50 ) servers

so i guess if his accoutn exists in /etc/passwd i will go and take the account out of passwd , sudo etc...

The /etc/sudo files are not identical   :-(
LVL 30

Accepted Solution

serialband earned 500 total points
ID: 39901280
That just looks for the account in /etc/password.  You can keep it simple and use that same line with userdel instead of grep.  If the account doesn't exist, it just returns an exit code of 6

Do you have root access?  While you can view /etc/password as a user, you can't delete a user without either sudo or root access.

for x in `< servers_linux`; do echo ""; echo $x; ssh  root@$x "userdel h66148"; done

The sudoers file will require some more checking, but you should be able to use sed to either remove entry.  You can run a test first  with  sed s/h10267// /etc/sudoers to see what it will do, then when you're certain, add the -i option to change the file in place.

for x in `< servers_linux`; do echo ""; echo $x; ssh root@$x "userdel h66148; sed -i s/h1267// /etc/sudoers"; done

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question