LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-17-2009, 02:03 PM   #1
carlosinfl
Senior Member
 
Registered: May 2004
Location: Orlando, FL
Distribution: Arch
Posts: 2,905

Rep: Reputation: 77
Question Help With A "Remove Shell User" Script


I have a script I run on my mail server when deleting a user. This script just backs up the users data / account info before running the 'userdel' command in case there is any issues that come up post deletion. My reason for this post is simple. I know nothing about shell scripts yet and would like someone to look at this script and tell me if it looks OK or can be simplified for better function / perforamce and secondly...when I delete their account and it begins to create a tarball, can I also add to the tarball the users /etc/passwd & /etc/shadow entry so if I restore, I still have their old login info?

Thanks for any assistance:

#!/bin/bash

USERNAME=$1
HOMEDIR=/home/$USERNAME
BACKUPDIR=/var/backup
DATESTAMP=$(date +%m-%d-%Y_%H-%M)

if [ -d /home/$USERNAME ]; then
echo "* Backing up home directory to $BACKUPDIR:"
tar jcf $BACKUPDIR/$USERNAME-$DATESTAMP.tar.bz2 /home/$USERNAME
echo "--------------------------------------------------"
echo "* Removing user home directory - /home/$USERNAME:"
rm -rf /home/$USERNAME
echo "--------------------------------------------------"
fi

echo "* Checking alias files for entries:"
ALIASFILES=$(ls /etc/*alias* | grep -v '\.db' | grep -v '\.orig')
for f in $ALIASFILES; do
grep -H $USERNAME $f
done
echo "--------------------------------------------------"
userdel $USERNAME

Last edited by carlosinfl; 07-17-2009 at 02:07 PM.
 
Old 07-17-2009, 02:44 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello Carlwill

You might need to do something different if the user may have files outside /home/<username> (ignoring transient files that will be removed anyway, such as files in /tmp).

If there is a possibility that the user may be re-instated it is better to disable than to delete. That way you reserve both their user name and user ID. This can be done, for example, by replacing their login shell in /etc/passwd with /dev/null. If you take this advice there is no need to add their /etc/passwd and /etc/shadow records to the tarball.

Thank you for prettying the script with a fixed pitch font; you can also use the # icon to format text as CODE.

Performance is not an issue -- unless you are deleting thousands of users?!

Critiquing the script ...

Safer (and easy) to get the users' home directory by ~<username> rather than assume it's /home/<username>.

It would be safer to examine the return code from tar (check the value of $?) before removing the home directory.

If the [ -d /home/$USERNAME ] test fails it would be helpful to have an else clause giving an error message.

There may be other places that user-specific data is kept; the only one I can bring to mind is the samba password database.

Best

Charles
 
  


Reply



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
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
how to create and remove desktop icons or icons from shell scripts? kkpal Linux - Newbie 7 09-01-2008 10:04 AM
Can anybody check this copy,remove and chown scripts? sathyguy Linux - Newbie 1 03-18-2007 11:05 PM
how can i run a shell scripts (or a number of commands )when a user logs in to a linu rddreamz Programming 4 05-20-2004 03:08 PM
Is it safe to remove the startup scripts? Retrievil_Knievil Linux - Hardware 3 04-14-2004 04:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:03 PM.

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