LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 10-21-2015, 11:57 PM   #1
Hotchips
Member
 
Registered: Nov 2006
Location: Brisbane, Australia
Distribution: RHEL4
Posts: 33

Rep: Reputation: 15
delete users with userdel -r on multple Linux (rhel) servers


Hi all

I have a few (over a hundred) servers that we need to remove a user from. I'd like to do this through a shell script with the following...

1. Test user is on the node
if yes, continue script, if no, report.
2. Test if user has files/folders in home dir (apart from hidden profile files)
if no, continue. If yes, skip removal and report!
3. Remove user if exists and home dir is empty.

Servers are various shades of RHEL / OEL (4.x,5.x,6.x)

The user acct I will connect with (mine) will be present on all nodes and have sudo capability.

I just don't have the days and days it would take me to something, and i'm not too confident. Heavily commented scripts would be most useful too!

Help would be much appreciated!

Thanks.
 
Old 10-22-2015, 12:17 AM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
What have you done so far?
 
Old 10-22-2015, 12:38 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
It won't take you 'days & days', if you just write a user_remove.sh bash script to do all the checking and email a result, then just write an even simpler script to scp the user_remove.sh script over and run it.
Test the user_remove.sh script on one machine first with a temp user created just for testing the script.
 
Old 10-22-2015, 10:28 AM   #4
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by chrism01 View Post
It won't take you 'days & days', if you just write a user_remove.sh bash script to do all the checking and email a result, then just write an even simpler script to scp the user_remove.sh script over and run it.
Test the user_remove.sh script on one machine first with a temp user created just for testing the script.
I agree with making a user_remove script.. but the rest seems needlessly complex.
You can run a local script over ssh without needing to copy the file over or make multiple connections to the same server.

Code:
ssh domain 'bash' <user_remove
 
Old 10-22-2015, 08:15 PM   #5
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Hotchips View Post
1. Test user is on the node
if yes, continue script, if no, report.
2. Test if user has files/folders in home dir (apart from hidden profile files)
if no, continue. If yes, skip removal and report!
3. Remove user if exists and home dir is empty.
Point 1 - grep user /etc/passwd. echo "user doesn't exist" if this fails.
Point 2 - As superuser, cd ~user and use ls to check if there are any files. echo "user has files" if not.
Point 3 - sudo userdel -r user

Then:
Code:
for server in $(< list-of-your-servers)
do
    echo Processing ${server}
    scp abovescript.sh ${server}:
    ssh ${server} abovescript.sh
done
Profit!

Quote:
Heavily commented scripts would be most useful too!
I agree, but I am not sure you would get them for free.

Edit: For bonus points, implement Sefyir's suggestion to avoid scp.

Last edited by berndbausch; 10-22-2015 at 08:17 PM.
 
Old 10-23-2015, 01:28 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Frankly, if you have "hundreds of" servers to manage, and you are doing it this way, then IMHO you are definitely doing it the wrong way!

You should be using a centralized authorization/authentication system, i.e. LDAP (nee "Microsoft OpenDirectory"). All hundreds-of servers should be referencing this central authority to validate their login credentials, instead of using a passwd file.

So far as I know, every enterprise of any size does things this way ... on Windows, on OS/X, on Linux, or ... "any and every combination thereof," i.e. "single sign-on."

Thanks to PAM = Pluggable Authentication Modules, this is easy to do with Linux and very well-documented. Linux can easily "play well together" with everybody else under a common, over-arching management system.

Last edited by sundialsvcs; 10-23-2015 at 01:30 PM.
 
Old 11-05-2015, 10:54 PM   #7
Hotchips
Member
 
Registered: Nov 2006
Location: Brisbane, Australia
Distribution: RHEL4
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by berndbausch View Post
Point 1 - grep user /etc/passwd. echo "user doesn't exist" if this fails.
Point 2 - As superuser, cd ~user and use ls to check if there are any files. echo "user has files" if not.
Point 3 - sudo userdel -r user

Then:
Code:
for server in $(< list-of-your-servers)
do
    echo Processing ${server}
    scp abovescript.sh ${server}:
    ssh ${server} abovescript.sh
done
Profit!



I agree, but I am not sure you would get them for free.

Edit: For bonus points, implement Sefyir's suggestion to avoid scp.
Thanks, will look into this now I am back from interstate - and when I catch up again...
 
Old 11-05-2015, 11:10 PM   #8
Hotchips
Member
 
Registered: Nov 2006
Location: Brisbane, Australia
Distribution: RHEL4
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by sundialsvcs View Post
Frankly, if you have "hundreds of" servers to manage, and you are doing it this way, then IMHO you are definitely doing it the wrong way!

You should be using a centralized authorization/authentication system, i.e. LDAP (nee "Microsoft OpenDirectory"). All hundreds-of servers should be referencing this central authority to validate their login credentials, instead of using a passwd file.

So far as I know, every enterprise of any size does things this way ... on Windows, on OS/X, on Linux, or ... "any and every combination thereof," i.e. "single sign-on."

Thanks to PAM = Pluggable Authentication Modules, this is easy to do with Linux and very well-documented. Linux can easily "play well together" with everybody else under a common, over-arching management system.
yes, it is the way the once small company did things which rapidly became the - "is this really how we manage users???" way. That is being addressed through a new access and authentication management solution the company is now rolling out which is quite comprehensive - however - this roll out will take time. I do have a need to remove users from a given list of servers from time to time so I will be trying my hand at some scripting. Unfortunately, this is low on my competing priorities list (writing something) - I have what needs to happen in my head, its just time to put it together. I'll just have to make time. When I get something down, I'll post out of interest. Any ideas welcome.

Any thoughts on this?

ssh me@$server <<- EOF
check user exists > $serverresult.txt
check homedir >> $serverresult.txt
exit
EOF
if $serverresult.txt=removeuser (look for string based on the check tests) then
ssh back into $server and run remove user commands.
elseif - echo servername and status to text file. (build the list of exceptions)
 
Old 11-06-2015, 08:32 AM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator response

Moved: This thread is more suitable in <Red Hat> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply

Tags
multiple, script, servers, userdel



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation of RHEL guest on CentOS5 with Multple CDs kranthij123 Linux - Software 0 05-23-2007 02:48 PM
migrating users between 2 linux servers javier_ccs Linux - General 3 09-12-2006 01:21 PM
removing users with userdel linuxtesting2 Linux - Newbie 1 09-15-2004 01:35 AM
creating new maildirs multple users Jonasx Linux - Software 0 12-18-2003 09:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 01:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration