Solved

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

Posted on 2014-02-28
11
459 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 28

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

912 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

16 Experts available now in Live!

Get 1:1 Help Now