Solved

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

Posted on 2014-02-28
11
457 Views
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 ?
0
Comment
Question by:c_hockland
  • 2
  • 2
  • 2
  • +4
11 Comments
 
LVL 23

Expert Comment

by:savone
ID: 39895318
userdel h80613
0
 

Author Comment

by:c_hockland
ID: 39895350
will that remove the entry from all files ?
0
 
LVL 34

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
0
 
LVL 13

Expert Comment

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

#userdel -r <userame>

TY/SA
0
 
LVL 8

Expert Comment

by:Pepe2323
ID: 39896342
Hi

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
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 23

Expert Comment

by:savone
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.
0
 
LVL 13

Expert Comment

by:Sandy
ID: 39896764
savone rocks
0
 
LVL 5

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.
0
 
LVL 27

Expert Comment

by:serialband
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.
0
 

Author Comment

by:c_hockland
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   :-(
0
 
LVL 27

Accepted Solution

by:
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 http://www.unix.com/man-page/Linux/8/userdel/

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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now