Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-02-28
11
Medium Priority
?
476 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 36

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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
 
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 31

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 31

Accepted Solution

by:
serialband earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

886 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