Saturday, May 5, 2012

How to run a unix command as a USER on startup?

To run a unix command on startup you may use the command below (add it to the /etc/rc.local file):
su USER -c 'your_command &'

How to make stable $PATH when using sudo command?

To make your sudo commands works with the $PATH of your logged-in user you just type the following command and then close all your terminals and login again:

echo "alias sudo='sudo env PATH=$PATH'" >> ~/.bashrc

Wednesday, January 25, 2012

securing memcache

Simply iptables rules can be added to secure memcached server from the out sources.

/sbin/iptables -A INPUT -m tcp -p tcp -s 127.0.0.1 --dport 11221 -j ACCEPT
/sbin/iptables -A INPUT -m tcp -p tcp -s CLIENT_IP_2 --dport 11221 -j ACCEPT
/sbin/iptables -A INPUT -m tcp -p tcp -s CLIENT_IP_1 --dport 11221 -j ACCEPT
/sbin/iptables -A INPUT -m tcp -p tcp --dport 11221 -j DROP
To make it run when system restarts just add it to the /etc/rc.local file.

Sunday, October 16, 2011

Learn path and shell of current user on unix

To find the shell type of current user:
echo $SHELL

To find the paths of current user:
echo $PATH

Friday, July 9, 2010

Simple SH script to monitor basic connection counts on *nix systems

I wrote near a year ago a sh script to see my traffic information which can be used to see basic connection counts like unix connection count, tcp connection count, http inbound and outgoing request counts and ftp connection count, ssh connection count.
Save as mycons.sh then run the command
chmod +x mycons.sh
For short access run
ln mycons.sh /bin/mycons
Sample usage after shortcut
mycons -hl
Here is the code for mycons.sh: http://pastebin.com/dBpPMGeY
#HEADER SECTION STARTS
#------------------------------------------------------------------------
#This script is written by Mustafa TURAN (HOME http://mustafaturan.net/
#BLOG http://vpslife.blogspot.com/) to monitor some basic network actions
#and display some user specific information like hd usage...
#you are feel free to distribute re-use in any of your application
#without deleting HEADER section. Also, it licensed with CC 3.0
#Also see: http://creativecommons.org/licenses/by/3.0/
#-------------------------------------------------------------------------
#HEADER SECTION ENDS


#
# Function to print help
#
print_help()
{
        echo "Usage: $0 -c -h -l -i";
        echo "Where -c clear screen";
        echo "      -h shows hd usage size";
        echo "      -l count of connections";
        echo "      -i show ip list";
        return
}

#
# Function to clear the screen
#
cls()
{
        clear
        return
}

#
# Function to show hd space
#
print_hdspace()
{
        echo -n "Total Space Used: "
        /usr/bin/du -hs /home/www-data/mustafat #write here your home path
        echo "-------------------------------------------------------------------------"
        return
}

#
# Function to connection counts
#
print_connection_count()
{
        unix_connections=`/bin/netstat -an | grep unix | grep CONNECTED | wc -l` #unix connection count
        tcp_connections=`/bin/netstat -an | grep ESTABLISHED | wc -l` #tcp connection count
        ftp_connections=`/bin/netstat -an | grep :21\  | grep ESTABLISHED | wc -l` #fcp connection count
        ssh_connections=`/bin/netstat -an | grep :22\  | grep ESTABLISHED | wc -l` #ssh connection count
        http_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED | wc -l` #http connection count
        h_out_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED |  awk '{print $5}' | grep :80 | wc -l` #http requests count (downloading file from somewhere else)
        h_in_connections=`/bin/netstat -an | grep :80\  | grep ESTABLISHED |  awk '{print $4}' | grep :80 | wc -l` #http response count (someone display a web page from this server)

        echo "CONNECTIONS"
        echo "-------------------------------------------------------------------------"
        echo -e "UNIX\tTCP\tFTP\tSSH\tHTTP\tH-Input\tH-Output"
        echo -e "$unix_connections\t$tcp_connections\t$ftp_connections\t$ssh_connections\t$http_connections\t$h_in_connections\t$h_out_connections\n"
        return
}

#
# Function to ip list http connections
#
print_iplist()
{
        echo "List of ip addresses:"
        echo "-------------------------------------------------------------------------"
        /bin/netstat -an | grep :80 | grep ESTABLISHED | awk '{print $5}'
        return
}

#
# Main procedure start here
#
# Check for sufficent args
#

if [ $# -eq 0 ] ; then
    print_help
    exit 1
fi

# Now parse command line arguments
#
while getopts chlik: opt
do
    case "$opt" in
        c) cls;;
        h) print_hdspace;;
        l) print_connection_count;;
        i) print_iplist;;
        \?) print_help; exit 1;;
    esac
done

VNSTAT: Network Traffic Monitor for *nix Systems

Vnstat is a network stats software, an opensource project for *nix systems and can be downloaded from http://humdi.net/vnstat/. For more than a year I am using Vnstat and it works silently without using too much resources and gives required stats when it is needed.
In details, it is possible to see your network adapters' traffic data based on years, months, weeks, days, hours and even real time data flow rates, running vnstat at the background with a small system resource like 1mb ram.

Setup

cd /usr/local/src
wget http://humdi.net/vnstat/vnstat-1.10.tar.gz
tar zxvf vnstat-1.10.tar.gz
cd vnstat-1.10
make
make all
make install
vnstat -u -i eth0 #eth0 should be replaced by your network adapter
vi /etc/vnstat.conf #change eth0 in conf file with your network adapter
OK configuration fineshed, now it is time to setup startup script which can be downloaded from
/etc/init.d scripts for vnStat daemon
Some Commands
vnstat --help # I think this will be enough :)

Friday, February 26, 2010

Anti-DDOS NGINX Server Configuration includes PHP config

Here is my NGINX configuration for DDOS Attacks to PHP and my lovely NGINX server:
nginx.conf
user www-data www-data;
worker_processes 10;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;

sendfile on;

#keepalive_timeout 0;
keepalive_timeout 5;
tcp_nodelay on;

gzip on;
limit_req_zone $binary_remote_addr zone=antiddosphp:10m rate=1r/s;
limit_req_zone $binary_remote_addr zone=antiddos:10m rate=10r/s;

include /etc/nginx/mustafat/*;

}
virtualhost.conf
server {
root /home/www-data/vpswatch.com;
listen 80;
server_name vpswatch.com;

access_log off;

location / {
index index.php index.html index.htm;
limit_req zone=antiddos burst=10;

}
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 1800;
fastcgi_param SERVER_NAME $http_host;
fastcgi_ignore_client_abort on;
limit_req zone=antiddosphp burst=2;
}
}

Monday, February 1, 2010

Findout Linux Distrubution Name of your Linux

lsb_release -a

Change the timezone, date time on linux

ln -sf /usr/share/zoneinfo/Europe/Berlin /etc/localtime
In this command important part is giving the path of your timezone(bold one in example).

Monday, January 18, 2010

Change Default Editor on Linux

#set | grep EDITOR
EDITOR=/bin/pico
# which vi
/usr/bin/vi
# export EDITOR=/usr/bin/vi
# set | grep EDITOR
EDITOR=/usr/bin/vi
_=EDITOR

Wednesday, October 21, 2009

Deleting Files Based on Date Time

Today I needed to delete files based on date and time:
Here is the command I used for deleting file last modified on 21 Oct 2009 16:30:
ls -lh | sed -n '/Oct\ 21\ 16:30/p' | awk '{print $9}' >> 1.txt
rm -f `cat 1.txt`
Explanations: 
ls -lh (lists all files)
sed -n  '/Oct\ 21\ 16:30/p' (find files at given date from list)
awk '{print $9}' (print only file names on list) and >> 1.txt put list of files to 1.txt
rm -f (force to delete)

Wednesday, October 14, 2009

Recursively Download FTP

Sometimes, you may need to move one server to another. At that time, you may use wget command to move your files without any pain.
Here is the command for recursive download on ftp via wget:

wget -r ftp://username:password@ftp.example.com/
Yes, thats all folks!

Thursday, October 8, 2009

List of users on Linux

To list the users in linux you may use following command:

cat /etc/passwd | cut -d ":" -f1

Wednesday, September 16, 2009

NGINX, PHP-CGI, SPAWN-FCI, FTP, VHOSTS, MYSQL | centos-fedora-redhat

Setup is done under a clean Centos 5.2 32bit server

yum -y update
yum -y upgrade

PART 0
#####################################################
# start with php-5 setup and mysql setup #
#####################################################

yum -y install mysql-server mysql-client
yum -y install php-cgi php-mysql php-curl php-gd php-idn php-pear php-imagick php-imap php-mcrypt php-memcache php-mhash php-ming php-pspell php-recode php-snmp php-sqlite php-tidy php-xmlrpc php-xsl

Note: add following line to end of php ini file
cgi.fix_pathinfo = 1

PART 1
#####################################################
# NGINX SETUP START #
#####################################################

# first creating users for nginx
adduser www-data --home-dir /home/www-data
passwd www-data
--------------->enter a password

# Creating a client user for some virtual hosting
adduser mustafat --home-dir /home/www-data/mustafat -G www-data
passwd mustafat
--------------->enter a password
chown mustafat:www-data /home/www-data/mustafat
#after adding users run chmod command
chmod 0755 /home/www-data/ -R
# adding some directories for user www-data actually for nginx setup :)
mkdir /var/lib/nginx/
mkdir /var/lib/nginx/body
mkdir /var/lib/nginx/proxy
mkdir /var/lib/nginx/fastcgi
chown www-data:root /var/lib/nginx/ -R
chmod 0700 /var/lib/nginx -R


yum -y install wget which
wget http://sysoev.ru/nginx/nginx-0.8.15.tar.gz
tar zxvf nginx-0.8.15.tar.gz
cd nginx-0.8.15

yum -y install gcc make
yum -y install pcre pcre-devel
yum -y install openssl openssl-devel

./configure --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-

path=/var/lock/nginx.lock --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-

path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_stub_status_module --with-http_flv_module --with-

http_ssl_module --with-http_dav_module

make
make install


#nginx setup finished
#####################################################
#####################################################

PART 2
#####################################################
# start spawn-fcgi setup #
#####################################################
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz
tar zxvf spawn-fcgi-1.6.2.tar.gz
cd spawn-fcgi-1.6.2
./configure
make
make install

which spawn-fcgi
#returned: /usr/local/bin/spawn-fcgi
which php-cgi
#returned: /usr/bin/php-cgi

#for help run for spawning
/usr/local/bin/spawn-fcgi --help

#so lets SPAWN it:
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid -C 10

#add it to the start up
vi /etc/rc.local
#add the following line to the end of file
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid -C 10


# end spawn-fcgi setup
#####################################################
#####################################################

PART 3
#####################################################
#editing default nginx conf
vi /etc/nginx/nginx.conf
#change file content with following one
user www-data;
worker_processes 5;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;
server_names_hash_bucket_size 128;
sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 2;
tcp_nodelay on;

gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;
gzip_disable "MSIE [1-6]\.";


limit_req_zone $binary_remote_addr zone=antiddos:20m rate=3r/s;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}


#####################################################
mkdir /etc/nginx/conf.d/
mkdir /etc/nginx/sites-enabled
#add following lines for default config you may change it whatever you like
vi /etc/nginx/sites-enabled/default
# sample config file
server {
listen 80;
server_name _; # underdash means for all hosts

access_log off;

location / {
root /home/www-data;
index index.html index.htm;
}
location /nginx_status {
stub_status on;
access_log off;
}

}
#some test
vi /home/www-data/index.html
hello world from vpslife.blogspot.com and vpswatch.com



#adding a virtual host
vi /etc/nginx/sites-enabled/vpswatch.com.conf
#sample config for vpswatch.com (virtual host) which is running now
server {
listen 80;
server_name vpswatch.com www.vpswatch.com;

access_log off;

location / {
root /home/www-data/mustafat/vpswatch.com;
index index.php index.html index.htm;

# to forward www to non-www address
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent;
}
# I added a some write for the testing purpose
rewrite ^/vps-provider/([^/]*)/$ /myserviceproviders.php?vps_provider=$1 last;

}
location ~ \.php$ {
limit_req zone=antiddos burst=5 nodelay;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www-data/mustafat/vpswatch.com$fastcgi_script_name;
fastcgi_read_timeout 180;
include fastcgi_params;
}

}

PART 4
#####################################################
# NGINX STARTUP SCRIPT FOR CENTOS/FEDORA/REDHAT #
#####################################################
vi /etc/init.d/nginx
# copy paste following script for redhat/centos/fedora


#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac


#####################################################

#make it executable
chmod +x /etc/init.d/nginx
# lets start nginx
/etc/init.d/nginx start
# now try at is it working with a standard browser
# like firefox, ie, safari, opera...
# open http://yourserveripaddress/
# or try your virtual hosting in example : http://vpswatch.com/


# add it to the start up
vi /etc/rc.local
# add following line
/etc/init.d/nginx start
PART 5
#####################################################
# FTP Server with same users #
#####################################################
http://vpslife.blogspot.com/2009/09/setting-up-secure-fast-ftp-server-for.html

Reference : http://wiki.nginx.org/NginxInstall

Setting Up a secure fast ftp server for linux | VSFTP

#################################################
# setting up a ftp server #
# http://vpslife.blogspot.com/ & http://vpswatch.com/
#################################################

wget ftp://vsftpd.beasts.org/users/cevans/vsftpd-2.2.0.tar.gz
tar zxvf vsftpd-2.2.0.tar.gz
cd vsftpd-2.2.0
make
adduser nobody
adduser: user nobody exists
mkdir /var/ftp/
useradd -d /var/ftp ftp
(the next two are useful to run even if the user "ftp" already exists).
chown root.root /var/ftp
chmod og-w /var/ftp
Install vsftpd config file, executable, man page, etc.
make install
cp vsftpd.conf /etc
# open conf file and edit
vi /etc/vsftpd.conf
# set anonym ftp to no!
anonymous_enable=NO
# set local users can login and uncomment
local_enable=YES
# set write enable to yes and uncomment
write_enable=YES
#other settings
local_umask=022
connect_from_port_20=NO

chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list

##save file

#add users that you want enable ftp
vi /etc/vsftpd.chroot_list
#I added two user
www-data
mustafat
#add following lines to pam file
vi /etc/pam.d/vsftpd

# Standard behaviour for ftpd(8).

auth required pam_listfile.so item=user sense=allow file=/etc/vsftpd.chroot_list onerr=fail


# Note: vsftpd handles anonymous logins on its own. Do not enable
# pam_ftp.so.

# Standard blurb.
@include common-account
@include common-session

@include common-auth
auth required pam_shells.so
account required pam_unix.so
session required pam_unix.so
# to add to start up
vi /etc/rc.local
# add following line
/usr/local/sbin/vsftpd &

Reference : ftp://vsftpd.beasts.org/users/cevans/untar/vsftpd-2.2.0/INSTALL
Why Vsftp: Its fast and secure!

Saturday, April 4, 2009

PHP Upload Progress installation-setup

This article covers installation of PHP Upload Progress extension.
#SETUP guide is done by Mustafa TURAN(http://vpslife.blogspot.com/)#
#SETUP Software:#
#--PHP 5.2 or more#
#--Any OS#

Download
wget http://pecl.php.net/get/uploadprogress-*.tgz
Untar
tar zxvf uploadprogress-*.tgz
Go to dir
cd uploadprogress-*
Setup
phpize
./configure
make
make install
Learn php ini path
php --ini
;to ini file add these two lines:
[uploadprogress]
extension=uploadprogress.so

How to uninstall Webmin?

Just run the command
/etc/webmin/uninstall.sh

If you have installed the RPM version of Webmin, you can also use
rpm -e webmin
Or if you have installed the Solaris package you can use
pkgrm WSwebmin
Webmin FAQ: http://www.webmin.com/faq.html

How to uninstall LAMP?

Sometimes, we need to uninstall current Apache Mysql PHP collections and re install them. In this article I will show how to remove a LAMP system(for RHEL,Fedora, CENTOS):
  1. Check the existence of LAMP system:
    rpm -qa | grep -i mysql
    rpm -qa | grep -i apache
    rpm -qa | grep -i httpd
    rpm -qa | grep -i php
  2. If you get list then remove via rpm
    rpm -e "file name"
  3. or with "yum -y uninstall"
    yum remove "file name"
Thats all :))

Tuesday, March 17, 2009

Mysql Nested Query Tweak

Do not use mysql nested queries for performance. Here I will share a little database table and two queries and their response times:
#Experiment is done by Mustafa TURAN(http://vpslife.blogspot.com/)#
#Experiment purpose to see how a nested query affects MYSQL Performance #
#Experiment Hardware:#
#--RAM: 1.5GB#
#--CPU: AMD Athlon(tm) 64 X2 Dual Core Processor 4000#
#Experiment Software:#
#--MYSQL 5(MYISAM)#
#--Centos5.2#
#Notes: 1k=1000#
Experiment tables
Table #1: links

database table : links(l_id, l_source, l_status, l_kb)

total rows: 220k

Table #2: filelinks

database table : filelinks(f_id, l_id)

total rows: 1500k

Nested query

Query: SELECT l_id, l_source, l_status, l_kb FROM links WHERE links.l_id IN (SELECT l_id FROM filelinks WHERE filelinks.f_id = 1);

Response: Showing rows 0 - 0 (1 total, Query took 2.9740 sec)


Inner Join(same query with inner join method)

Query: SELECT distinct(l_id), l_source, l_status, l_kb FROM links INNER JOIN (SELECT l_id as xL_id FROM filelinks WHERE filelinks.f_id = 1) xLinks ON links.l_id =
xLinks.xL_id;

Response: Showing rows 0 - 0 (1 total, Query took 0.0003 sec)

This example make you to see how dangerous using nested queries. I hope this experiment will save your CPU usage by MYSQL!

My detailed /etc/my.cnf file:

#Mustafa Turan (http://vpslife.blogspot.com/)#

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
port = 3306
skip-locking
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 2M
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 128
query_cache_size= 64M
thread_concurrency = 4
thread_stack = 1M
max_connections = 250
skip-bdb
skip-innodb


[mysqldump]
quick
max_allowed_packet = 32M


[mysql]
no-auto-rehash
#safe-updates


[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 16M
write_buffer = 16M


[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 16M
write_buffer = 16M


[mysqlhotcopy]
interactive-timeout


[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Monday, March 16, 2009

How to learn linux system(hardware) properties?

To learn system(hardware,sofware) properties linux has such commands:
Operating System Version:
uname -r
Memory Information:
cat /proc/meminfo
CPU Information:
cat /proc/cpuinfo
Easy Way(smart info):
uname -rs;less /proc/meminfo grep MemTotal;less /proc/cpuinfo grep
"model name"

Aktive System Resource Usages:
(all users and all activity)
top -s

(specified user)
top -u username