October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

SUDO on Linux

SUDO on Linux

cat /etc/passwd

test:x:500:500:test:/home/test:/bin/bash

[root@localhost ~]# cp /etc/sudoers
[root@localhost ~]# cp /etc/sudoers /etc/sudoers.org

1) Full Permission to User
# User privilege specification
test ALL=(ALL) ALL

Let restart apache with out sudo

[test@localhost ~]$ /etc/init.d/httpd restart
rm: cannot remove `/var/run/httpd/httpd.pid’: Permission denied

test@localhost ~]$ sudo /etc/init.d/httpd restart
[sudo] password for test:
Stopping httpd: [ OK ]

sudo vi /etc/httpd/conf/httpd.conf

2) Limited Permission to User
Let restrict to apache stop,start restart

test ALL=(ALL) /etc/init.d/httpd

3)Full permission to a user with No password

test ALL=(ALL) NOPASSWD:ALL

[test@localhost ~]$ sudo /etc/init.d/vsftpd restart
Shutting down vsftpd: [FAILED]
Starting vsftpd for vsftpd: [ OK ]

4) All Permission to Group
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

5) Limited Permission to Group
# Members of the admin group may gain root privileges
%admin ALL=(ALL) /etc/init.d/ssh

6) All Permission to Group with No Password
# Members of the admin group may gain root privileges
%admin ALL=(ALL) NOPASSWD:ALL

7)Give Limited Permission to Group with No Password
# Members of the admin group may gain root privileges
%admin ALL=(ALL) NOPASSWD:/etc/init.d/ssh

Balancing Traffic Across Data Centres Using LVS

Source is taken from
http://www.linuxforu.com/2009/05/balancing-traffic-across-data-centres-using-lvs/

SCP

Fast SCP
SCP is a great way to copy files from somewhere to somewhere else, but as we all know it can be slower than (insert slow metaphor here). Here’s a way you can get way speedier(really a word?) transfers using scp.

scp -c arcfour -C sourcefile desthost:
BY This way we can copy the files quickly

Logging on Apache

Apache server behind a proxy server

If you are using an apache server behind a proxy server, you may find that in your log files you will be given the proxy IP as the source IP of the connection. This is true,
however you can adjust the apache log settings to pass the X-Forwarded-For IP onto the apache log files.

My original log settings looked like this (httpd.conf)
1 LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” ” combined

The apache log settings dictate that %h is the source IP, in my instance it was my Proxy server. By changing my apache log format to
1 LogFormat “%{X-Forwarded-For}i %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\” %h” sitename

and then telling the site config to use the ‘sitename’ settings
view source
print?
1 CustomLog logs/rmohan-access_log sitename

Netstat

Netstat

netstat -antpuleo

Will display the Timer running on the service

Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name Timer
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 0 9169 1246/rpcbind off (0.00/0/0)
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 0 22083 14524/sshd off (0.00/0/0)
tcp 0 0 0.0.0.0:36998 0.0.0.0:* LISTEN 29 9641 1310/rpc.statd off (0.00/0/0)
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 497 10406 1483/qpidd off (0.00/0/0)
tcp 0 52 10.4.85.106:22 10.4.85.105:4744 ESTABLISHED 0 10716 1578/sshd on (0.36/0/0)
tcp 0 0 :::111 :::* LISTEN 0 9174 1246/rpcbind off (0.00/0/0)
tcp 0 0 :::80 :::* LISTEN 0 20481 14476/httpd off (0.00/0/0)
tcp 0 0 :::22 :::* LISTEN 0 22085 14524/sshd off (0.00/0/0)
tcp 0 0 :::443 :::* LISTEN 0 20490 14476/httpd off (0.00/0/0)
tcp 0 0 :::8443 :::* LISTEN 0 20485 14476/httpd off (0.00/0/0)

HTTPD process check

HTTPD process check

The apache/httpd config file has a setting which determines how many running processes your apache /httpd daemon will commit to the system. The idea is, that if your web server needs more processes to handle more connections it will spawn a new child and serve the request.
If it is able it will kill the process off after and return it back into the pool.

prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves

StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000

I can spawn 256 additional child processes in the event that the systems needs them.
Any more, and the system will slow down while attempting to deal with the existing requests.
It is safe to assume that a normal web site serving hundreds of connections a minute (visitors) will consume approx 100 child processes.

You can count the number of processes spawned by running this very simple command (I use httpd, not apache2)

[root@localhost log]# pstree -G | grep httpd
??httpd???crlhelper
? ??9*[httpd]
? ??{httpd}

#!/bin/bash
# Apache check process script
HOSTNAME=`hostname`
THRESHOLD=180
ADDRTO=”admin@rmohan.com”
SUBJECT=”${HOSTNAME} – Apache Process Check”
LOCKFILE=”/tmp/apache_process_check.lock”
LOGFILE=”/var/log/apache_processes.log”

NUMHTTPD=`ps aux | grep http | grep -v “\(root\|grep\)” | wc -l`
echo “`date +’%Y-%m-%d %H:%M:%S %Z’` – ${NUMHTTPD}” >> ${LOGFILE}

if [[ ${NUMHTTPD} -gt ${THRESHOLD} ]]; then
if [ ! -e “${LOCKFILE}” ]; then
echo “The number of currently running httpd threads is ${NUMHTTPD}. Web services restarted” | mail -s “${SUBJECT} – Above Threshold” ${ADDRTO}
touch ${LOCKFILE}
service httpd stop && sleep 5 && service httpd start
fi
else
if [ -e “${LOCKFILE}” ]; then
rm -f “${LOCKFILE}”
fi
fi

SSH to your webserver you wish to run this script on
In the directory you wish to run the script, create the file and copy/paste the contents of the above script
Make changes to the ADDRTO and THRESHOLD variables as needed to suit your setup
Save the file then make it executable using chmod +x test.sh
Insert the script into crontab using

view source

1 */2 * * * * root /path/to/test.sh

Eating your memory? Let is find Out

Works on CENTOS AND FEDORA Prints the top 10 memory consuming processes

TR=`free|grep Mem:|awk ‘{print $2}’`;ps axo rss,comm,pid|awk -v tr=$TR ‘{proc_list[$2]+=$1;} END {for (proc in proc_list) {proc_pct=(proc_list[proc]/tr)*100; printf(“%d\t%-16s\t%0.2f%\n”,proc_list[proc],proc,proc_pct);}}’|sort -n |tail -n 10

Pound Loadbalancer

Pound Loadbalancer

Pound is a bit more specific to HTTP/Web scenarios. It functions as a 100% Layer-7 load balancer as it does full HTTP(S) integration and has full access to the HTTP stack. What this means is that you can do some fancy routing based on cookies, url regex, and do this with SSL termination. You can combine the two and have Pound decrypt the traffic and forward to HAProxy but your setting up a scaling issue if you do.

wget http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
rpm –import RPM-GPG-KEY.dag.txt
rm -f RPM-GPG-KEY.dag.txt
vi /etc/yum.repos.d/dag.repo
yum –enablerepo=dag install pound

vi /etc/pound.cfg

ListenHTTP
Address 192.168.1.11
Port 80
Service
BackEnd
Address 192.168.1.14
Port 80
End
BackEnd
Address 192.168.1.15
Port 80
End
End
End

openssl req -x509 -newkey rsa:1024 -keyout local.server.pem -out local.server.pem -days 365 -nodes

ListenHTTP
Address 192.168.1.11
Port 80
END

ListenHTTPS
Address 192.168.1.11
Port 443
Cert “/root/software/local.server.pem”
Client 20
End
Service
BackEnd
Address 192.168.1.14
Port 80
End
BackEnd
Address 192.168.1.15
Port 80
End
End

ListenHTTP
Address 192.168.1.11
CheckURL “(^\/|\.html|\.css|\.jpg|favicon\.ico|robots\.txt|\.png)$”
HeadRemove “X-Forwarded-For”
MaxRequest 1024
Port 80
xHTTP 0
Service
Redirect “https://www.mohan.com”
End
Service
Redirect “https://mohan.com”
End
END

ListenHTTPS
Address 192.168.1.11
Port 443
Cert “/root/software/local.server.pem”
Client 20
End
Service
BackEnd
Address 192.168.1.14
Port 80
End
BackEnd
Address 192.168.1.15
Port 80
End
End

Load balancer HAPROXY STUNNEL

Load balancer HAPROXY STUNNEL

HAProxy Software Load Balancer

HAProxy is a bit more bare metal as it targets a very specific set of scenarios focused on TCPIP more than HTTP. You can use cookie based injection with HAProxy to do round robin and stick users to a specific server. However, you can not do this if your site is running SSL traffic. HAProxy can not decrypt the SSL traffic. This is more of the authors dead-fast belief that SSL should not be terminated because of CPU load on the load balancer preventing scaling as you would need to scale the load balancers at some point (we’re talking millions of requests, facebook style).

on all nodes please copy the files on all server / nodes

lb1

vi /etc/hosts

127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

##### Load balancers

192.168.60.11 lb1.rmohan.com lb1
192.168.60.12 lb2.rmohan.com lb2

### web servers

192.168.60.14 web1.rmohan.com web1
192.168.60.15 web2.rmohan.com web2

### database servers

192.168.60.17 db1.rmohan.com db1
192.168.60.18 db2.rmohan.com db2

##############VIPS

192.168.60.10 load.rmohan.com load
192.168.60.6 db.rmohan.com db

Now generate ssh keys

ssh-keygen -t rsa

ssh-keygen -t dsa

cd /root/.ssh

cat *.pub > authorized_keys

ls

authorized_keys id_das id_dsa.pub id_rsa id_rsa.pub known_hosts

scp -r .ssh/ lb2:root/

scp -r .ssh/ web1:root/

scp -r .ssh/ web2:root/

scp -r .ssh/ db1:root/

scp -r .ssh/ db2:root/

ssh-keyscan -t rsa lb1 lb2 www1 www2 db1 db2

ssh-keyscan -t dsa lb1 lb2 www1 www2 db1 db2

scp -r /etc/hosts lb2:/etc/
scp -r /etc/hosts web1:/etc/
scp -r /etc/hosts web2:/etc/

Stop unwanted services

NTP SETUP ON THE SERVER

LB1

ntp services

rpm -qa | grep ntp

vi /etc/ntp.conf

# restrict default kod nomodify notrap nopeer noquery
#restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1
#restrict -6 ::1

# server 0.centos.pool.ntp.org
# server 1.centos.pool.ntp.org
# server 2.centos.pool.ntp.org

server 127.127.1.0 # local clock

#fudge 127.127.1.0 stratum 10

/etc/init.d/ntpd start

chkconfig ntpd on

watch ntpq -p -n

ntpdate -u 192.168.1.10

Note : It NEED SOME TIME TO SYNC

LB2

ntp services

rpm -qa | grep ntp

vi /etc/ntp.conf

# restrict default kod nomodify notrap nopeer noquery
#restrict -6 default kod nomodify notrap nopeer noquery

#restrict 127.0.0.1
#restrict -6 ::1

server 192.168.1.10

# server 0.centos.pool.ntp.org
# server 1.centos.pool.ntp.org
# server 2.centos.pool.ntp.org

#server 127.127.1.0 # local clock

#fudge 127.127.1.0 stratum 10

/etc/init.d/ntpd start

chkconfig ntpd on

watch ntpq -p -n

ntpdate -u 192.168.1.10

Note : It NEED SOME TIME TO SYNC

yum install mod_ssl

yum install openssl

yum install stunnel

wget http://download.fedora.redhat.com/pub/epel/6/i386/epel-release-6-5.noarch.rpm

yum install haproxy

wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.19.tar.gz

tar -zxvf haproxy-1.4.19.tar.gz

yum install gcc

make TARGET=linux26 ARCH=i386

make TARGET=linux26 CPU=i686

make install

mkdir /etc/haproxy

wget http://layer1.rack911.com/haproxy/haproxy-standard.cfg

wget http://layer1.rack911.com/haproxy/haproxy.init

cp haproxy-standard.cfg /etc/haproxy.cfg

cp haproxy.init /etc/init.d/haproxy

chmod +x /etc/init.d/haproxy

cp haproxy.init /etc/init.d/haproxy

/usr/local/sbin/haproxy location

cp /usr/local/sbin/haproxy /usr/sbin/haproxy

chkconfig –add haproxy

chkconfig haproxy on

useradd haproxy

chown haproxy:haproxy /etc/haproxy.cfg

mkdir haproxy
touch stats

chown -R haproxy:haproxy /var/lib/haproxy

# Global settings
global
log 127.0.0.1 local2

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats

defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000

# round robin balancing between the various backends
listen HTTP-80 192.168.60.11:80
mode http
stats enable
balance roundrobin
cookie SERVERID insert nocache indirect
cookie JSESSIONID prefix
option httpclose
option forwardfor
option dontlognull
option httpchk HEAD /check.txt HTTP/1.0
server web1 192.168.60.14:80 weight 1 maxconn 512 check
server web2 192.168.60.15:80 weight 1 maxconn 512 check
option persist
option redispatch

echo 1 > /proc/sys/net/ipv4/conf/all/forwarding
echo 1 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 1 > /proc/sys/net/ipv4/conf/eth0/send_redirects

change the log format

/etc/httpd/conf/httpd.conf

#LogFormat “%h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined

LogFormat “%{X-Forwarded-For}i %h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined

CustomLog logs/access_log combined env=!dontlog
SetEnvIf Request_URI “^/check\.txt$” dontlog

LogFormat “%h %l %u %t \”%r\” %>s %b” common
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html
SetEnvIf Request_URI “^/check\.txt$” dontlog
CustomLog logs/access_log combined env=!dontlog
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common

vim /etc/sysctl.conf

net.ipv4.ip_nonlocal_bind = 1

sysctl -p

Monitor url

http://192.168.60.11/haproxy?stats

wget ftp://ftp.nluug.nl/pub/networking/stunnel/stunnel-4.50.tar.gz

tar -zxvf stunnel-4.50.tar.gz

/usr/local/etc/stunnel

cd stunnel-4.50

./configure
make
install
make install

/usr/local/etc/stunnel

wget http://download.fedora.redhat.com/pub/epel/6/i386/epel-release-6-5.noarch.rpm

rpm -ivh epel-release-6-5.noarch.rpm

yum install stunnel

cp /etc/pki/tls/private/localhost.key ca.key
cp /etc/pki/tls/certs/localhost.crt stunnel.pem

OR

cd /etc/pki/tls/certs

openssl req -x509 -nodes -newkey rsa:1024 -keyout /etc/pki/tls/certs/pound.pem -out /etc/pki/tls/certs/pound.pem

chmod 600 /etc/pki/tls/certs/pound.pem

openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr
cp server.key server.key.org

openssl rsa -in server.key.org -out server.key

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

[root@lb1 stunnel]# cat stunnel.conf
cert=/etc/stunnel/stunnel.pem
key=/etc/stunnel/ca.key
setuid=root
setgid=root
pid = /var/run/stunnel.pid
output = /var/log/stunnel.log

socket=l:TCP_NODELAY=1
socket=r:TCP_NODELAY=1

# HTTPS
[https]
accept=192.168.60.11:443
connect=192.168.60.11:80
TIMEOUTclose = 0

#cert=/etc/stunnel/stunnel.pem
#key=/etc/stunnel/ca.key
setuid=root
setgid=root
pid = /var/run/stunnel.pid
output = /var/log/stunnel.log

socket=l:TCP_NODELAY=1
socket=r:TCP_NODELAY=1

# HTTPS
[https]
cert=/etc/stunnel/server.crt
key=/etc/stunnel/server.key
accept=192.168.60.11:443
connect=192.168.60.11:80

TIMEOUTclose = 0
~

#!/bin/bash

# VARIAVEIS
GREP=”/bin/grep”
EGREP=”/bin/egrep”
PROG=”stunnel”
KILLALL=”/usr/bin/killall”

# TESTANDO SE EXISTE O ARQUIVO
test -x /usr/bin/stunnel || exit 0
RETVAL=0

########## START ##########
start() {
if [ ! -f /var/lock/subsys/stunnel ]; then
/usr/bin/stunnel
RETVAL=$?
if [ $RETVAL = 0 ]; then
touch /var/lock/subsys/stunnel
echo $”Starting $PROG: OK”
else
exit 1
fi
fi
return $RETVAL
}
stop() {
if [ -e /var/lock/subsys/stunnel ]; then
$KILLALL /usr/bin/stunnel
RETVAL=$?
if [ $RETVAL = 0 ]; then
rm -rf /var/lock/subsys/stunnel
echo $”Stop $PROG: OK”
else
exit 1
fi
fi
return $RETVAL
}
restart(){
if [ -e /var/lock/subsys/stunnel ]; then
$KILLALL /usr/bin/stunnel
RETVAL=$?
if [ $RETVAL = 0 ]; then
rm -rf /var/lock/subsys/stunnel
echo $”Stop $PROG: OK”
else
exit 1
fi
fi

if [ ! -f /var/lock/subsys/stunnel ]; then
/usr/bin/stunnel
RETVAL=$?
if [ $RETVAL = 0 ]; then
touch /var/lock/subsys/stunnel
echo $”Starting $PROG: OK”
else
exit 1
fi
fi

return $RETVAL
}
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $”ESCOLHA UM ITEM AO LADO: $0 {start|stop|restart}”
exit 2
esac

exit $?

JBOSS AUTO RESTART SCRIPT

JBOSS AUTO RESTART SCRIPT

set –xv

Shell script with Debug command inside:

Add set –xv inside the shell script now to debug the output as shown below.

$ cat filesize.sh
#!/bin/bash
set -xv
for filesize in $(ls -l . | grep “^-” | awk ‘{print $5}’)
do
let totalsize=$totalsize+$filesize
done
echo “Total file size in current directory: $totalsize”

JBOSS AUTO RESTART SCRIPT

#!/bin/sh

#define where jboss is – this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-“/apps/appprod/jboss-5.1.0.GA”}

#define the user under which jboss will run, or use ‘RUNASIS’ to run as the current user
JBOSS_USER=${JBOSS_USER:-“prod”}

#make sure java is in your path
JAVAPTH=${JAVAPTH:-“/usr/java/jdk1.6.0_31/bin”}

#configuration to use, usually one of ‘minimal’, ‘default’, ‘all’
JBOSS_CONF=${JBOSS_CONF:-“all”}

#bind jboss services to a specific IP address – added by pmc
JBOSS_HOST=${JBOSS_HOST:-“192.168.1.55″}

#if JBOSS_HOST specified, use -b to bind jboss services to that address
JBOSS_BIND_ADDR=”0.0.0.0″
JBOSS_BIND_ADDR=${JBOSS_HOST:+”-b $JBOSS_HOST”}
#JBOSS_BIND_ADDR=${JBOSS_HOST:+”$JBOSS_HOST”}

#define the classpath for the shutdown class
JBOSSCP=${JBOSSCP:-“$JBOSS_HOME/bin/shutdown.jar:$JBOSS_HOME/client/jnet.jar”}

#define the script to use to start jboss
JBOSSSH=${JBOSSSH:-“$JBOSS_HOME/bin/run.sh -c $JBOSS_CONF $JBOSS_BIND_ADDR -Djboss.service.binding.set=ports-01 -g DocsPartition1”}

if [ “$JBOSS_USER” = “RUNASIS” ]; then
SUBIT=””
else
SUBIT=”su – $JBOSS_USER -c ”
fi

if [ -n “$JBOSS_CONSOLE” -a ! -d “$JBOSS_CONSOLE” ]; then
# ensure the file exists
touch $JBOSS_CONSOLE
if [ ! -z “$SUBIT” ]; then
chown $JBOSS_USER $JBOSS_CONSOLE
fi
fi

if [ -n “$JBOSS_CONSOLE” -a ! -f “$JBOSS_CONSOLE” ]; then
echo “WARNING: location for saving console log invalid: $JBOSS_CONSOLE”
echo “WARNING: ignoring it and using /dev/null”
JBOSS_CONSOLE=”/dev/null”
fi

#define what will be done with the console log
JBOSS_CONSOLE=${JBOSS_CONSOLE:-“/dev/null”}

JBOSS_CMD_START=”cd $JBOSS_HOME/bin; $JBOSSSH”
#JBOSS_CMD_STOP=${JBOSS_CMD_STOP:-“java -classpath $JBOSSCP org.jboss.Shutdown –shutdown”}
#JBOSS_CMD_STOP=”$JAVA_HOME/bin/java -classpath $JBOSSCP org.jboss.Shutdown -s jnp://$JBOSS_BIND_ADDR:1199 –shutdown”
JBOSS_CMD_STOP=”$JAVA_HOME/bin/java -classpath $JBOSSCP org.jboss.Shutdown -s jnp://$JBOSS_HOST:1199 –shutdown”
if [ -z “`echo $PATH | grep $JAVAPTH`” ]; then
export PATH=$PATH:$JAVAPTH
fi

if [ ! -d “$JBOSS_HOME” ]; then
echo JBOSS_HOME does not exist as a valid directory : $JBOSS_HOME
exit 1
fi

echo JBOSS_CMD_START = $JBOSS_CMD_START

case “$1” in
start)
cd $JBOSS_HOME/bin
if [ -z “$SUBIT” ]; then
eval $JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &
else
$SUBIT “$JBOSS_CMD_START >${JBOSS_CONSOLE} 2>&1 &”
fi
;;
stop)
if [ -z “$SUBIT” ]; then
$JBOSS_CMD_STOP
else
$SUBIT “$JBOSS_CMD_STOP”
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo “usage: $0 (start|stop|restart|help)”
esac