Script to delete users in the servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete users in the servers
# 1  
Old 01-02-2015
RedHat Script to delete users in the servers

Hi Team,
Hope you are doing good.I am new to scripting.I have a requirement of deleting around 10 users in 100 servers.It is very time consuming by logging into each servers and delete the user.Here I have redhat 6 ,Suse linux 10&11 environment servers.

In one set of servers I have root access I can directly login.In another set of servers I need to do sudo to root and delete the users.

I really thankful if someone provide me the script to delete the users.
  1. Script to delete users logged in as root
  2. Script to login as normal id, sudo to root and then delete the users.

Advance thanks for your replies.

Last edited by rbatte1; 01-02-2015 at 10:28 AM.. Reason: Remove extraneous ICODE tags (Con Cragun). Converted text to numeric list (RBATTE1)
# 2  
Old 01-02-2015
Did you consider the threads proposed at the bottom of this page?
# 3  
Old 01-02-2015
Some additional things to consider:
1. At a chance of ~ 99% those 10 users will be all the same on all the servers.
2. Why login as normal user and do sudo?
3. What if no sudo was installed
4. So just login as root and delete those 10 users, see: man userdel
5. It might not be safest to 'delete' currently (as root) loged in accounts. (eg: What if the fstab was open, or it was in the middle of rebuilding dracut?)

Hope this helps
# 4  
Old 01-02-2015
Hello muraliinfy04,

Assuming that these are all local accounts (i.e. there is no LDAP in play) I have a few to questions pose in response first:-
  • What have you tried so far?
  • What output/errors do you get?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • What logical steps do you think would e most suitable?
  • Are any firewall rules going to stop any single server connecting to any other?
  • Do you have any trust between servers, for instance rlogin or ssh?
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.
# 5  
Old 01-02-2015
Hi thanks for your reply.Here is my response.
@Sea:
  1. All the 10 users are same in all the servers.But there could be chances that in few servers the users may not exist.As these are different environments(eg:prod, dev etc)
  2. As per the security compliance of the client(only few clients) we need to login as normal user and then sudo to root.Then we need to work as root.This is only for few clients.Rest of the clients we can directly login as root.
  3. Sudo is installed and available.
  4. As now a days we are getting this activity frequently I just want to automate it.We can login as root and delete users if it is single server.Here we have multiple servers.Logging into each server and deleting users will be time consuming.
  5. Those 10 users have already left organisation.They are inactive users so we can delete them.

@rbatte1:
  1. yes all the accounts are local accounts.
  2. I have tried to delete users manually login to the servers. As it appears to be time consuming I just thought of automating it.
  3. As I have tried manually I did not get any errors.
  4. preferred tool is shell/perl
  5. I have verified that users are inactive and they are not logged in recently.We generally use userdel command and comment out entries in /etc/passwd & /etc/shadow if user details exists.We will take backup of these two files before editing them.
  6. No firewall restrictions imposed.we can connect servers as we want which are in same environment(ex:prod as one environment,Development as one environment)
  7. yes. Servers are trusted.We can use ssh and rlogin.

Thanks again for your replies.

Last edited by rbatte1; 01-05-2015 at 06:19 AM.. Reason: Remove extraneous ICODE tags, turn off smilies (Don Cragun) Added LIST=1 tags (rbatte1)
# 6  
Old 01-07-2015
If you can submit remote commands in with ssh or remsh/resh/rsh, then you you set up a double loop, a bit like this:-
Code:
for server in $server_list
do
   ssh $server "for userid in $user_list
      do
         userdel $user_list
      done"
done

This is entirely untested and just theoretical, but as a structure it might give you something to work with. You would be sensible to add error checking and some sort of response/logging so that you can verify the actions afterwards. If the command is different on different servers, you will have to handle that somehow.

Of course, the server that you choose to run this loop would need to be able to get through the firewall to each server listed, but I hope that this gives you something to work with. Have a go and let us know if you get stuck.

If you want to do a dummy run, then perhaps change the remote command to run to something like this:-
Code:
userdel ; for userid in $user_list
      do
         echo userdel $user_list
      done

The extra userdel will ensure you can see the command in your path (and give the usage statement)
The echo will then show you what commands it would try to issue.


I hope that this helps,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script to delete files from 5 different servers

Hello, I'm new to shell scripting and need a quick note on how to write a shell script to perform deletion of files from 5 different hostnames in various locations. Found out to delete files from one path by using below command and made it to work on cron job but need to do it in a shell... (2 Replies)
Discussion started by: Teja G
2 Replies

2. Shell Programming and Scripting

Script To Delete User Accounts On Multiple Servers

Hello All, The servers in question are AIX/Unix servers. I was hoping to find a scripting solution where I could use one server as a jump server and run a script that would check each server for a user account (the source file for the user accounts would be a text file or csv file) , and delete... (4 Replies)
Discussion started by: k45bryant
4 Replies

3. Homework & Coursework Questions

Script to delete multiple users

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi This is my first post.I am learning Unix and finding it difficult to get a handle on the scripting side of... (5 Replies)
Discussion started by: dom17
5 Replies

4. Shell Programming and Scripting

Script to add new users to a group on multiple servers using SSH

Hi Experts, I am new to scripting. We have around 400 Linux servers in our environment. I want to add a new user to a perticular group on all the servers using SSH. Requirements: 1) Need to take the server names from a text file. 2) Login into each server and check whether perticular... (1 Reply)
Discussion started by: Satya1983
1 Replies

5. Shell Programming and Scripting

I need to add 30 users to 50 servers

Hi Y'all, I need help adding 30 users to 50 servers. Is there a way to automate this? I'm using this command: sudo mkuser pgrp=srvadm gecos=Spears, Brittney auditclasses=ALL sugroups=system,security user01 But I'm doing it per user, per server...HOW CAN I MAKE THIS EASIER?... (7 Replies)
Discussion started by: jennie28n
7 Replies

6. Shell Programming and Scripting

Wants to create 3 users in 400 servers

I wants to create 3 users and set password for the users in 400 servers. I can run this script without error. If wants to set the password in the same command it is not working. Like that i have to create 3 users #!/usr/bin/ksh for server in `cat /tmp/servers` do echo "servername =... (5 Replies)
Discussion started by: G0kulakrishnan
5 Replies

7. Shell Programming and Scripting

how to delete files on two remote servers simultaneously?

dear all, i'm preparing a script which can do these actions : 1. stop remove server's certain service 2. clean the files on remote servers simultaneously (because lots of files need to be deleted) 3. after files/logs are removed, restart the service again i'm stuck on how to clean remote... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

Using userdel -r in a script? To delete multiple users.

I'm getting ready to upgrade to Solaris 10 (from 8) and I'm trying to write a script that will delete approximately 800 user accounts that are no longer required. I'm trying to use the userdel -r command and read the input from a file with the list of user names. I've never been very good at... (3 Replies)
Discussion started by: sol10admin
3 Replies

9. UNIX for Advanced & Expert Users

Application Servers Installation and Users

Hi, When most of the server applications get installed, they create their own user. I believe this is to not use the "root" account. For example, Apache when installed creates a user called "apache". And the directories which it uses are all owned by this user. This seems to be the... (2 Replies)
Discussion started by: srikanths
2 Replies

10. Shell Programming and Scripting

script to create users on many servers

Hi all, working on script to create a user acct on all our servers. for i in `cat $host_file`; do ssh $i /usr/bin/sudo /usr/bin/mkuser id='bpadm' gecos='NetBackup Admin' 2>&1 >> $log done error i get is: 3004-692 Error changing "id" to "bpadm" : Value is invalid. I have tried this in... (1 Reply)
Discussion started by: dnidiffer
1 Replies
Login or Register to Ask a Question