Archive for the ‘Linux’ Category

in place replace a string in a directory

Wednesday, October 30th, 2013

grep -rl ‘xxxx’ ./ | xargs sed -i ‘s/xxx/yyy/g’

CLI to display all DNS records

Sunday, May 12th, 2013

dig -t AXFR domain_name @authorative_name_server

Use the old method to logrotate in centos 6

Friday, April 26th, 2013

The dateext option for logrotate.conf is enabled by default in centos6

[william.ho@mail10a log]$ ls /var/log
anaconda.ifcfg.log anaconda.xlog btmp-20130401 cron-20130414 ftp.log maillog messages ntpstats secure-20130331 spooler-20130331 wtmp
anaconda.log anaconda.yum.log clamav cron-20130421 httpd maillog-20130331 messages-20130331 openwebmail.log secure-20130407 spooler-20130407 xferlog
anaconda.program.log audit cron dmesg iptraf maillog-20130407 messages-20130407 sa secure-20130414 spooler-20130414 yum.log
anaconda.storage.log boot.log cron-20130331 dmesg.old lastlog maillog-20130414 messages-20130414 sa-update.log secure-20130421 spooler-20130421
anaconda.syslog btmp cron-20130407 dracut.log mail maillog-20130421 messages-20130421 secure spooler tallylog

To disabled it, edit /etc/logrotate.conf and comment the line “dateext”

Installing keepalived from source (For centos 5, rpmbuilder only provide centos 6 rpms)

Monday, April 8th, 2013

1: Get the source and extract

wget?http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

tar xvfzp keepalived-1.2.7.tar.gz

2: Compile and install

cd keepalived-1.2.7
./configure
make
make install

3: init script

cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

vi /etc/init.d/keepalived

After line 19, add

PATH=$PATH:/usr/local/sbin

4: create directories and config file

mkdir -p /etc/keepalived
touch /etc/sysconfig/keepalived

vi /etc/keepalived/keepalived.conf

On master:

vrrp_script chk_ping {
script “ping -c 1 192.168.100.1”
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
}

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 100
priority 101
virtual_ipaddress {
192.168.100.52/24 dev eth0
}
track_script {
chk_ping
}

}

On slave

vrrp_script chk_ping {
script “ping -c 1 192.168.100.1”
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK
}

vrrp_instance VI_1 {
interface eth0
state MASTER
virtual_router_id 100
priority 100
virtual_ipaddress {
192.168.100.52/24 dev eth0
}
track_script {
chk_ping
}

}

5: Start service

chkconfig keepalived on

service keepalived start

?

SFTP only server in centos 5.X

Friday, August 17th, 2012

configuration file:

/etc/ssh/sshd_config-sftponly


# ZYV
PasswordAuthentication yes
PermitRootLogin no
PidFile /var/run/sshd-sftponly.pid
Port 2234
Protocol 2
UsePAM no

Subsystem sftp internal-sftp

ChrootDirectory /srv/sftp
AllowTcpForwarding no
X11Forwarding no
ForceCommand internal-sftp

Init script: /etc/init.d/sshd-sftponly

#!/bin/bash
#
# Init file for SFTP-only OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: SFTP-only OpenSSH server daemon
#
# processname: sshd-sftponly
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub
# config: /etc/ssh/ssh_random_seed
# config: /etc/ssh/sshd_config-sftponly
# pidfile: /var/run/sshd-sftponly.pid

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0
prog=”sshd-sftponly”

# Some functions to make the below more readable
SSHD=/usr/sbin/sshd-sftponly
PID_FILE=/var/run/sshd-sftponly.pid

# ZYV
LOCK_FILE=/var/lock/subsys/sshd-sftponly
OPTIONS=” -f /etc/ssh/sshd_config-sftponly ”

runlevel=$(set — $(runlevel); eval “echo \$$#” )

start()
{
cp -af /etc/localtime /var/empty/sshd/etc

echo -n $”Starting $prog: ”
$SSHD $OPTIONS && success || failure
RETVAL=$?
[ “$RETVAL” = 0 ] && touch $LOCK_FILE
echo
}

stop()
{
echo -n $”Stopping $prog: ”
if [ -n “`pidfileofproc $SSHD`” ] ; then
killproc $SSHD
else
failure $”Stopping $prog”
fi
RETVAL=$?
# if we are in halt or reboot runlevel kill all running sessions
# so the TCP connections are closed cleanly
if [ “x$runlevel” = x0 -o “x$runlevel” = x6 ] ; then
killall $prog 2>/dev/null
fi
[ “$RETVAL” = 0 ] && rm -f $LOCK_FILE
echo
}

reload()
{
echo -n $”Reloading $prog: ”
if [ -n “`pidfileofproc $SSHD`” ] ; then
killproc $SSHD -HUP
else
failure $”Reloading $prog”
fi
RETVAL=$?
echo
}

case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
condrestart)
if [ -f $LOCK_FILE ] ; then
stop
# avoid race
sleep 3
start
fi
;;
status)
status -p $PID_FILE openssh-daemon
RETVAL=$?
;;
*)
echo $”Usage: $0 {start|stop|restart|reload|condrestart|status}”
RETVAL=1
esac
exit $RETVAL

Some directory/lib setup…

mkdir -p /srv/sftp/{home,lib,sbin}
ln /lib/ld-2.5.so /srv/sftp/lib
ln /lib/ld-linux.so.2 /srv/sftp/lib
ln /lib/libc-2.5.so /srv/sftp/lib
ln /lib/libc.so.6 /srv/sftp/lib
ln /sbin/nologin /srv/sftp/sbin
ln -s /usr/sbin/sshd /usr/sbin/sshd-sftponly
chkconfig –add sshd-sftponly
chkconfig sshd-sftponly on
service sshd-sftponly start
groupadd sftponly

Adding a user

useradd sftpuser -s/sbin/nologin
usermod -a -G sftponly sftpuser
mkdir -p /srv/sftp/home/sftpuser
chown -R sftpuser:sftponly /srv/sftp/home/sftpuser

Example usage

sftp -P 2234 sftpuser@<IP or hostname>

Mapping iostat to LVM Volume Name

Wednesday, July 25th, 2012

iostat -x 1 -N

avg-cpu: %user %nice %system %iowait %steal %idle
4.95 0.00 0.99 94.06 0.00 0.00

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
sda 2.00 23.00 127.00 50.00 2960.00 19160.00 124.97 105.46 652.45 5.65 100.00
sdb 0.00 21.00 0.00 9.00 0.00 152.00 16.89 0.31 19.56 15.11 13.60
ddf1_4035305a8680c3272020202020202020c71dbd673a354a45 0.00 0.00 128.00 38.00 2944.00 304.00 19.57 6558.13 35088.59 6.02 100.00
ddf1_4035305a8680c3272020202020202020c71dbd673a354a45p1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
ddf1_4035305a8680c3272020202020202020c71dbd673a354a45p2 0.00 0.00 128.00 38.00 2944.00 304.00 19.57 6558.13 35088.61 6.02 100.00
VolGroup00-LogVol00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.95 0.00 0.00 94.70
VolGroup00-LogVol05 0.00 0.00 1.00 34.00 8.00 272.00 8.00 13.30 658.74 28.57 100.00
VolGroup00-LogVol03 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
VolGroup00-LogVol04 0.00 0.00 11.00 1.00 136.00 8.00 12.00 16.56 4613.42 83.33 100.00
VolGroup00-LogVol02 0.00 0.00 0.00 1.00 0.00 8.00 8.00 6.90 898.00 1000.00 100.00
VolGroup00-LogVol01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
VolGroup00-LogVol06 0.00 0.00 0.00 2.00 0.00 16.00 8.00 0.98 0.00 244.50 48.90
VolGroup00-LogVol07 0.00 0.00 116.00 0.00 2800.00 0.00 24.14 6519.45 49518.97 8.62 100.00

Mirroring a website to HTML

Saturday, February 25th, 2012

if you need to migrate a website from one service provider to another and found you don’t have access to the files or backup on the server, you can try to migrate using the httrack utility.

 

Example

httrack “http://www.exmaple.com/” -O “/home/www/www.exmaple.com/htdocs/” -%v -r 1

 

Setting up Network Bonding

Thursday, August 4th, 2011

Network Bonding can provide resiliency to your server in case if one of the network connection failed.

Loading Bonding Driver

/etc/modprobe.conf
[ADD the following lines to the end of the file]
alias bond0 bonding
options bond0 mode=1 miimon=100

You can set up your bond interface according to your needs. Changing one parameters (mode=X) you can have the following bonding types:

mode=0 (balance-rr) Round-robin policy
mode=1 (active-backup) Active-backup policy:
mode=2 (balance-xor) XOR policy:
mode=3 (broadcast) Broadcast policy:
mode=4 (802.3ad) IEEE 802.3ad Dynamic link aggregation.
mode=5 (balance-tlb) Adaptive transmit load balancing:
mode=6 (balance-alb) Adaptive load balancing:


/etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
BROADCAST=192.168.242.255
IPADDR=192.168.242.50
NETMASK=255.255.255.0
NETWORK=192.168.242.0
USERCTL=no
GATEWAY=192.168.242.1
TYPE=BOND

/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=xx:xx:xx:xx:xx:xx
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no
TYPE=Ethernet

/etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
HWADDR=xx:xx:xx:xx:xx:xx
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
USERCTL=no
TYPE=Ethernet

To Find and Remove Files that has updated x days ago

Thursday, July 28th, 2011

Where X is the number of days

find . -mtime +X -exec ls -l {} \;

to remove these files

find . -mtime +X -exec rm -f {} \;

Splitting a Large Mailbox into multiple files

Wednesday, July 20th, 2011

By using formail – we can split a large mailbox into multiple small one easily.

+skip
Skip the first skip messages while splitting.

-total
Output at most total messages while splitting.

 

The following command will output the first 10 messages into mailbox.1 and the next 10 messages to the mailbox.2, etc etc.

cat mailbox | formail -10 -s > mailbox.1
cat mailbox | formail +10 -10 -s >mailbox.2
cat mailbox | formail +20 -10 -s >mailbox.3