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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

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 29

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 29

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Mailserver setup & redundancy 2 124
Internal CA server 6 134
Can't connect to FTP 18 161
need a help to fix one if else shell script issue 6 33
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
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

751 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