Thursday, May 19, 2011
Adding Multiple Users Script
The script reads from file containing first field as "User ID", second field as "First Name" and third field as "Last Name":
#!/bin/bash
export USER=null;
export NAME=null;
/bin/cp -p /etc/passwd /etc/passwd.$(date +%d%m%Y);
/bin/cp -p /etc/sudoers /etc/sudoers.$(date +%d%m%Y);
export NUM=`cat /var/tmp/useradd.txt|wc -l`;
for ((i = 1; i <= $NUM; i++));
do
export USER=`/usr/bin/head -$i /var/tmp/useradd.txt|tail -1|awk '{print $1}'`;
export NAME=`/usr/bin/head -$i /var/tmp/useradd.txt|tail -1|awk '{print $2" "$3}'`;
/usr/sbin/useradd -c "$NAME" -m -d /home/$USER -s /bin/bash $USER;
/bin/echo 'PASSWORD' |passwd --stdin $USER;
/usr/bin/chage -d 0 -M 90 $USER;
echo "$USER ALL=(ALL) ALL" >> /etc/sudoers;
done
Script with EXPECT
################HOST1######################
#!/usr/bin/expect -f
#!/bin/bash
# set Variables
set host1 "HOST1"
set login "testuser"
set PASSWORD "testpass"
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh $login@$host1 "ps -aef|grep -i java"
# Look for passwod prompt
expect "*?assword:*"
# Send PASSWORD aka $PASSWORD
send -- "$PASSWORD\r"
# send blank line (\r) to make sure we get back to ui
send -- "\r"
expect eof
################HOST1######################
################HOST2######################
#!/usr/bin/expect -f
#!/bin/bash
# set Variables
set host1 "HOST2"
set login "testuser"
set PASSWORD "testpass"
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh $login@$host1 "ps -aef|grep -i java"
# Look for passwod prompt
expect "*?assword:*"
# Send PASSWORD aka $PASSWORD
send -- "$PASSWORD\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
################HOST2######################
################Consolidated#################
#!/bin/bash
########### Script by R A J E S H D O G R A for #########################
##### Setting variables null in case re-run of script ####################
set VAR1 = null;
set VAR2 = null;
set VAR3 = null;
set VAR4 = null;
set VAR5 = null;
############## Collect Data From Remote Servers ############################
/bin/echo "Checking HOST1 processes"
/bin/echo "*****************************************"
/home/monitor/host1 |grep -v testuser|awk '{print $1}'|uniq|sort|tee /home/monitor/temp1
/bin/echo "*****************************************"
sleep 1
/bin/echo "Now Checking CENTIME for processes"
/bin/echo "*****************************************"
/home/monitor/hl-centime |grep -v sscope|awk '{print $1}'|uniq|sort|tee /home/monitor/temp2
/bin/echo "*****************************************"
############ Process the data to find if services are OK ###################
export VAR1=`sed -n '1p' /home/monitor/temp1`;
export VAR2=`sed -n '2p' /home/monitor/temp1`;
export VAR3=`sed -n '1p' /home/monitor/temp2`;
export VAR4=`sed -n '2p' /home/monitor/temp2`;
export VAR5=`sed -n '3p' /home/monitor/temp2`;
#echo $VAR1 $VAR2 $VAR3 $VAR4 $VAR5;
if [ "$VAR1" == "proc1" -a "$VAR2" == "proc2" -a "$VAR3" == "proc3" -a "$VAR4" == "proc4" -a "$VAR5" == "proc5" ];
then
echo "Chill Buddy, its just a regular alert, no need to panic !!"
sleep 2
echo "*********************************************************************"
sleep 1
echo "*********************************************************************"
else
echo "Server is screwed up buddy, reload the processes"
fi
############### Kill the session ###########################################
/bin/rm -f /home/monitor/temp1;
/bin/rm -f /home/monitor/temp2;
/bin/echo "Thats it ! You can close the window"
sleep 2
/bin/echo "******** Auto Logout in 20 seconds********"
sleep 20
kill -HUP `pgrep -s 0 -o`
################Consolidated#################
Wednesday, February 24, 2010
User Migration (Redhat, CentOS, Fedora)
* /etc/passwd - contains various pieces of information for each user account
* /etc/shadow - contains the encrypted password information for user's accounts and optional the password aging information.
* /etc/group - defines the groups to which users belong
* /etc/gshadow - group shadow file (contains the encrypted password for group)
* /var/spool/mail - Generally user emails are stored here.
* /home - All Users data is stored here.
You need to backup all of the above files and directories from old server to new Linux server.
Commands to type on old Linux system
First create a tar ball of old uses (old Linux system). Create a directory:
# mkdir /root/move/
Setup UID filter limit:
# export UGIDLIMIT=500
Now copy /etc/passwd accounts to /root/move/passwd.mig using awk to filter out system account (i.e. only copy user accounts)
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/move/passwd.mig
Copy /etc/group file:
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > /root/move/group.mig
Copy /etc/shadow file:
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/move/shadow.mig
Copy /etc/gshadow (rarely used):
# cp /etc/gshadow /root/move/gshadow.mig
Make a backup of /home and /var/spool/mail dirs:
# tar -zcvpf /root/move/home.tar.gz /home
# tar -zcvpf /root/move/mail.tar.gz /var/spool/mail
Where,
* Users that are added to the Linux system always start with UID and GID values of as specified by Linux distribution or set by admin. Limits according to different Linux distro:
o RHEL/CentOS/Fedora Core : Default is 500 and upper limit is 65534 (/etc/libuser.conf).
o Debian and Ubuntu Linux : Default is 1000 and upper limit is 29999 (/etc/adduser.conf).
* You should never ever create any new system user accounts on the newly installed Cent OS Linux. So above awk command filter out UID according to Linux distro.
* export UGIDLIMIT=500 - setup UID start limit for normal user account. Set this value as per your Linux distro.
* awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/move/passwd.mig - You need to pass UGIDLIMIT variable to awk using -v option (it assigns value of shell variable UGIDLIMIT to awk program variable LIMIT). Option -F: sets the field separator to : . Finally awk read each line from /etc/passwd, filter out system accounts and generates new file /root/move/passwd.mig. Same logic is applies to rest of awk command.
* tar -zcvpf /root/move/home.tar.gz /home - Make a backup of users /home dir
* tar -zcvpf /root/move/mail.tar.gz /var/spool/mail - Make a backup of users mail dir
Use scp or usb pen or tape to copy /root/move to a new Linux system.
# scp -r /root/move/* user@new.linuxserver.com:/path/to/location
Commands to type on new Linux system
First, make a backup of current users and passwords:
# mkdir /root/newsusers.bak
# cp /etc/passwd /etc/shadow /etc/group /etc/gshadow /root/newsusers.bak
Now restore passwd and other files in /etc/
# cd /path/to/location
# cat passwd.mig >> /etc/passwd
# cat group.mig >> /etc/group
# cat shadow.mig >> /etc/shadow
# /bin/cp gshadow.mig /etc/gshadow
Please note that you must use >> (append) and not > (create) shell redirection.
Now copy and extract home.tar.gz to new server /home
# cd /
# tar -zxvf /path/to/location/home.tar.gz
Now copy and extract mail.tar.gz (Mails) to new server /var/spool/mail
# cd /
# tar -zxvf /path/to/location/mail.tar.gz
Now reboot system; when the Linux comes back, your user accounts will work as they did before on old system:
# reboot
Please note that if you are new to Linux perform above commands in a sandbox environment. Above technique can be used to UNIX to UNIX OR UNIX to Linux account migration. You need to make couple of changes but overall the concept remains the same.
Friday, December 11, 2009
Creating a Swap File
To add a swap file:
-
Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
-
At a shell prompt as root, type the following command with
count
being equal to the desired block size:dd if=/dev/zero of=/swapfile bs=1024 count=65536
-
Setup the swap file with the command:
mkswap /swapfile
-
To enable the swap file immediately but not automatically at boot time:
swapon /swapfile
-
To enable it at boot time, edit
/etc/fstab
to include the following entry:/swapfile swap swap defaults 0 0
The next time the system boots, it enables the new swap file.
-
After adding the new swap file and enabling it, verify it is enabled by viewing the output of the command
cat /proc/swaps
orfree
.