Thursday, December 17, 2009

echo colourfully

# for i in `cat num.txt`; do echo -en '\E[3'$i'm'"\033[1mPsycho Tux\033[0m " ; done;
# for i in `cat num.txt`; do echo -en '\E[47;3'$i'm'"\033[1mPsycho Tux\033[0m " ; done;


# cat num.txt
1
2
3
4
5
6
7
8
9
0

Block direct SSH to root, but not to root equivalent

The PermitRootLogin no option of /etc/ssh/sshd_config will block all the users with UID 0. Below is an option to overcome this.

# vi /etc/ssh/sshd_config

###PermitRootLogin no
AllowUsers newuser guest psychotux hari
DenyUsers root

# /etc/init.d/sshd restart


Here users listed along with AllowUsers can be normal user or root equivalent.

Thursday, December 10, 2009

Hardware clock failure in ISA system

In ISA systems /sbin/hwclock will fail to fetch the Hardware clock and will throw an error similar to below.
# hwclock
select() to /dev/rtc to wait for clock tick timed out

# hwclock --show
select() to /dev/rtc to wait for clock tick timed out

But the --directisa option of hwclock will work here.
# /sbin/hwclock --directisa

So as a permanent solution we can rename the existing binary /sbin/hwclock and create a new Wrapper as below

1. Find the version of hwclock
# hwclock --version
2. Rename the binary by suffixing the version number
# cd /sbin
# mv hwclock hwclock-x.y
3. Create a wrapper for the hwclock-x.y named hwclock
# cat > hwclock << HERE
#!/bin/bash
/sbin/hwclock-x.y --directisa \$@
HERE

4. Give necessary execute permission and reboot the server
# chmod +x hwclock
5. Check the hardware clock, System Time, NTP, etc.
To synchronize system time with Hardware clock we can use hwclock --hctosys

And an optional reboot
# reboot

If your system is not ISA the we can try the RTC driver as well. The first thing we have to ensure is the RTC driver is loaded using the below steps.
# lsmod|grep rtc
This will show something like below
rtc 15329 0

If you are not getting any, then the rtc driver has not loaded. You need to load it

# modprobe rtc

Monday, April 27, 2009

Basic Linux Configuration backup

#!/bin/bash
# Title: Linux Primary Configuration Backup
# Version: 1.5
# Last update: 06-08-2012
# Author: Hareesh V V
# E Mail: tux.psycho@gmail.com
# Web: http://www.psychotux.com

DATE=`date +%d%m%y`
BKP=~/`hostname`.BACKUPS_$DATE
/bin/mkdir -p $BKP

tar -cjf $BKP/etc_$DATE.tar.bz2 /etc
/sbin/ifconfig > $BKP/ifconfig
/sbin/route -n > $BKP/route
/sbin/runlevel > $BKP/runlevel
/sbin/chkconfig --list | grep 3:on > $BKP/chkconfig_init_3
/sbin/chkconfig --list | grep 5:on > $BKP/chkconfig_init_5
/bin/hostname > $BKP/hostname
lsmod > $BKP/lsmod
cat /etc/hosts > $BKP/etc_hosts
cat /etc/resolv.conf > $BKP/etc_resolv 
cat /etc/grub.conf > $BKP/grub_conf 
#crontab -l > $BKP/crontab
/sbin/iptables -L  > $BKP/iptables_filter
/sbin/iptables -t nat -L > $BKP/iptables_nat
/sbin/iptables-save > $BKP/iptables
cat /etc/sysconfig/iptables-config > $BKP/iptables-config

/bin/netstat -ntpl > $BKP/netstat
mount > $BKP/mount
fdisk -l > $BKP/fdisk
cat /etc/rc.local > $BKP/rc_local
cat /proc/sys/net/ipv4/ip_forward > $BKP/proc_ip_forward
cat /proc/cpuinfo > $BKP/proc_cpuinfo
getenforce > $BKP/getenforce
cat /etc/fstab > $BKP/fstab
cp -r /etc/sysconfig/network-scripts $BKP/
cat /etc/sysconfig/network > $BKP/network
echo $PATH > $BKP/path

## Hardware
/usr/sbin/hwinfo

/sbin/lspci > $BKP/lspci
/usr/bin/lsb_release > $BKP/lsb_release
/usr/sbin/dmidecode > $BKP/dmidecode
/usr/bin/getconf LONG_BIT > $BKP/getconf
/usr/bin/systool > $BKP/systool
/usr/bin/lshal > $BKP/lshal
/sbin/lsusb -t > $BKP/lsusb
/usr/sbin/biosdecode > $BKP/biosdecode
lshw > $BKP/lshw
cat /proc/version > $BKP/version
cat /etc/printcap > $BKP/printcap
dmesg > $BKP/dmesg
cat /etc/sysconfig/hwconf > $BKP/hwconf


## CRON Backup
mkdir $BKP/`hostname`_crons
cd $BKP/`hostname`_crons
OUT=$BKP/`hostname`_crons
> crons.txt
> $OUT/cronlist
for i in `ls /var/spool/cron/`
do
   grep $i /etc/passwd
   if [ $? = 0 ]
   then
                {
                 crontab -u $i -l >> $OUT/$i.cron
crontab -u $i -l | grep -v "^#" | sort | uniq | awk {'print $6'} >> crons.txt
crontab -u $i -l | grep -v "^#" | sort | uniq | awk {'print $7'} >> crons.txt
                 crontab -u $i -l | grep -v "^#" | sort | uniq | awk {'print $8'} >> crons.txt
                }
   fi
done

for i in `cat crons.txt`
do
  cp $i $OUT 2> /dev/null
done
cd
tar -cjf $OUT.tar.bz2 $OUT
tar -cjf $BKP.tar.bz2 $BKP
cd $BKP/`hostname`_crons
rm -rf crons.txt $OUT
rm -rf $BKP

Sunday, March 8, 2009

Script to find normal users above UID 500

Script to find normal users above UID 500 and their Shell History. This works in Linux. Other NIXes may require modification.


#!/bin/bash
USERS=`grep ":5*:*:" /etc/passwd | grep "/bin/bash" | awk -F: '{print $1}'`
HOME=`grep ":5*:*:" /etc/passwd | grep "/bin/bash" | awk -F: '{print $6}'`
for i in $USERS
do
egrep -i "reboot|init|shutdown|halt|poweroff" `grep $i: /etc/passwd | cut -f6 -d:`/.bash_history
done

Sunday, March 1, 2009

Ever alive SSH session

If you are facing Session timeout issue whenever you are leaving an open session idle for some time you can make use of TCP Keepalive option in putty.

1. Open Putty
2. Go to Connection->Seconds between keepalives(0 to turn off). Give a keepalive value here in seconds, preferably 120 or above.

Advanced
1. If you are using portaputty you can set it in config file .\putty\sessions\Default%20Settings. Set TCPKeepalives=120.
2. Right inside Linux or any other UNIX we can use /etc/ssh/ssh_config. Set the variable ServerAliveInterval 60.
3. We can use screen command also. ssh host -t screen -xRe^oo.
4. Screen can exist with Putty as well. Go to Connections -> SSH -> Remote command. Then specify screen -xRe^oo

Further Readings
HowtoGeek
Metafilter

Tuesday, February 10, 2009

VSFTPD-chrooted user with limited directory access

Create a user with home directory /foo. Otherwise we can create a normal user and then edit /etc/passwd to change the home directory (useradd hari -d /foo).

Here we chose the latter option since it's a sensible directory and we don't wnat to take risk by putting .bash files.

# useradd hari
# grep hari /etc/passwd

hari:x:796:796::/home/hari:/bin/bash

Now change the home directory to /foo

# vi /etc/passwd


# grep hari /etc/passwd
hari:x:796:796::/foo:/sbin/nologin


Note that we have changed the home directory from /home/hari to /foo and the shell from /bin/bash to /sbin/nologin

Added the below two lines in /etc/vsftpd/vsftpd.conf for enabling chroot functionality.
# vi /etc/vsftpd/vsftpd.conf
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list


Now add the user hari to Chroot List file.

# echo hari >> /etc/vsftpd.chroot_list

Now verify the permission of /foo, /foo/tux and /foo/beastie

[root@psycho ~]# ls -ld /foo/
drwxrwx--- 17 root ftpuser 4096 Feb 6 19:58 /foo/

[root@psycho ~]#
[root@psycho ~]# ll /foo/
total 81652
drwxr-xr-- 2 tiger ftpuser 4096 Sep 18 2007 alert
drwxr-xr-x 14 root root 4096 Jan 18 2008 tux
drwxr-xr-x 2 root root 4096 Jan 11 2008 log
drwx------ 2 root root 16384 Sep 6 2007 lost+found
drwxrwxr-x 4 giraffe ftpuser 048000 Feb 9 23:51 beastie
[root@psycho ~]#


/foo/tux and /foo/beastie are having read-access to all. But /foo will not be readable since the permission is 770.

So add the user "hari" to the group "ftpuser", which is the Group for /foo.

# grep ftpuser /etc/group
ftpuser:x:502:

# vi /etc/group
ftpuser:x:502:hari


Now take the list of files/directories under /foo except tux and beastie. These are the only directories user needs access.

# ls /foo | grep -v tux | grep -v beastie
alert
log
lost+found


Add these to /etc/vsftpd_user_conf/hari for restricting access by the FTP user. We have already mentioned the below in /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd_user_conf

# vi /etc/vsftpd_user_conf/hari
deny_file={alert,log,lost+found}
write_enable=NO


write_enable=NO is to restrict the user from changing the files/directories

Restart VSFTPD service.

/etc/init.d/vsftpd restart

That's it. Now the conditions satisfied are as below

1. User will be able to login through FTP protocol.
2. Default login directory will be a "chrooted HOME- /foo". User will not be able to access any other directory other than /foo.
3. User can access "tux" and "beastie" directories right from the home directory.
4. These two directories will be "read-only". User can't write/change any files/directories.
5. User cannot access any other directories under /foo except tux and beastie.
6. User will not be able to login directly to system.

AT&T USA | Internet not working | Fix by custom APN

If the AT&T Mobile internet is not working on your cellphone, it can be fixed easily by adding an APN configuration. You can read this a...