Sunday, 31 March 2013

Restrict users access to only their home directory in vsftp

If you want to restrict FTP users to have access only their home directory but not outside of their own directory. You have to setup chroot.

1. open vsftpd configuration file
vi /etc/vsftpd/vsftpd.conf

2. Uncomment the below line
$ chroot_local_user=YES

3. Save and close the file. Restart vsftpd.
$ /etc/init.d/vsftpd restart

As a result FTP users can't access directories other than their home.

ftp> cd /home
550 Failed to change directory.

Thats it.

Friday, 29 March 2013

copy/move/delete files using xargs


How to move/copy/delete largest 15 files to a particular directory

Go to the user directory
$ cd /home/user

If you want to move them, use this below command
$ ls -s|sort -n|tail -15|awk '{print $2}'|while read f;do mv "$f" /backup;done


Explanation:
ls -s - prints size of each file
sort -n - sort files
tail -15 - 15 largest files
awk '{print $2}' - cut the filename
while loop moves files to /backup directory




If you want to delete them, use this below command
$ ls -s|sort -n|tail -15|awk '{print $2}'|while read f;do rm "$f";done

If you want to copy them, use this below command
$ ls -s|sort -n|tail -15|awk '{print $2}'|while read f;do cp "$f" /backup;done


Fix files and folder permission on linux


For files:
find . -type f -exec chmod 644 {} \;

For dirs:

find . -type d -exec chmod 755 {} \;

Monday, 25 March 2013

wget recursive download using ftp


wget recursive download using ftp 

$ wget -r ftp://username:password@ip.of.old.host

If you get error like:

$ wget -r ftp://ftp:M8cf#5GP@xx.xx.xx.xx
  ftp://ftp//:M8cf#5GP@xx.xx.xx.xx: Bad port number.

however if you remove special characters like #,$ and ? from the password than the same will work fine.


wget recursive ftp with mirroring option

wget -m ftp://username:Password@ftp.example.com


Saturday, 23 March 2013

assign multiple shared IPs in WHM


You can add more shared IPs by following the steps shown below.
cd /var/cpanel/mainips

If there is no such directory, create it.

mkdir -p /var/cpanel/mainips

vi /var/cpanel/mainips/root

Add the current main shared IP and the secondary ip.


cat /var/cpanel/mainips/root
11.22.33.44
44.33.22.11

Go to WHM » IP Functions » Show/Edit Reserved IPs


You can see the status of new IPs as 'Main/shared IP for root'.

That's it :)

Friday, 8 March 2013

stop and continue a linux process


In many occasion we may need to stop and start a process temporarily for some reasons(for example stopping rsync process for some time if the system loads goes up). This can be done using the KILL command with the -STOP option and the same process can be started again using -CONT option. For example :
   # kill -STOP 10007 (10007 is the pid)

The above command will sends a signal requesting to block/stop the pid 10007. And the below command will sends a signal request to continue/restart the suspended process.
   # kill -CONT 10007 (10007 is the pid)

Installing multiple python environments without breaking yum


Please use altinstall to have multiple python
# cd /usr/src/
# wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
# tar -xzvf Python-2.7.2.tgz
# cd Python-2.7.2
# ./configure
# make altinstall

To verify the newly installed python, run this below command.
# python2.7 -V
Python 2.7.2

Typically cpanel server server uses the Python 2.6.6 by default. But we can use newer version of python as well.

# which python2.7
/usr/local/bin/python2.7

That's it. We're done.

Use ethtool or mii-tool to detect problems with ethernet card


[root@s2 adserver]# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
                     100baseT/Half 100baseT/Full
                     1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Full
Advertised auto-negotiation: Yes
Speed: Unknown! (0)
Duplex: Half
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000033 (51)
Link detected: yes

You can also change the interface settings with ethtool.
[root@s2 adserver]# mii-tool
eth0: negotiated 10baseT-FD, link ok

Too many open files: /.htaccess


Forbidden You don’t have permission to access /vb/forumdisplay.php on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request i did tail -f /usr/local/apache/logs/error_log and find lot of lines like this:
[Fri May 9 17:13:33 2008] [crit] [client] (24)Too many open files: /.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable

Sol:
It seems the limit for open files is reaching. Type ulimit -a in shell. It should be 1024 by default.

You can add this limit for apache by adding this line to the apache init script and restarting apache.
# ulimit -n 4096

Wordpress version finder bash script


This script can be used on cpanel servers.

#!/bin/bash
email=user@gmail.com # email where the reports are sent
hostname=`/bin/hostname`
find /home/*/public_html -iname version.php > /root/version.txt
grep "wp-includes" /root/version.txt > /root/version.txt1
for i in `cat /root/version.txt1`; do echo "Installed path=`echo $i|dirname $i|awk -Fwp {'print $1'}`" ; echo "Latest version = 3.3.1" ;echo
"Installed version = `grep "wp_version =" $i |cut -d '=' -f2`"; echo " "; done > /root/out.txt
mail $email -s"Wp-version report - $hostname" < /root/out.txt
rm -f /root/version.txt*
rm -f /root/out.txt


Reset WP-admin password from shell


1. Open a bash shell on the linux box. Using SSH is advised.

2. Generate a password MD5 hash using php.
# php -r "print( md5('YourPassWordHere')); print(\"\r\n\");"

3. Copy the 32 bit string that is the result.

4. Connect to mysql
mysql

5. Connect to the correct database;
show databases;
use databasename;

6. make a copy/paste backup of the admin user data.
select * from wp_users where user_login = 'admin';

7. Update the table
update wp_users set user_pass='the_md5_hash_generated_in_step_2/3' where user_login = 'admin';

8. Log into wordpress using the username “admin” and the password you have used.

9. Reset you password using the “wordpress dashboard > Users > Your profile > Change password” option.

Script to change the passwords of all cPanel accounts


You may need to change passwords of all the cpanel accounts sometimes because of the security concerns. Following script will allow you to change the passwords of all the accounts.

1. Create and open a file “changepass.sh” in your favorite editor and add following details to it.
#! /bin/bash
ls -1 /var/cpanel/users | while read user; do
pass=`</dev/urandom tr -dc “A-Za-z0-9*-/+.*=_\|\\#” | head -c16
echo “$user $pass” >> new-pass.txt
/scripts/realchpass $user  $pass
/scripts/ftpupdate
done

2. Give executable permission to our script.
# chmod +x  changepass.sh

3. Execute the script.
# sh changepass.sh

The script will change all the passwords of cPanel and ftp accounts and create text file with the name ” new-pass.txt” which contains all the new passwords.

A note about dmesg command


What is dmesg?

 The main purpose of dmesg is to display kernel messages. dmesg can provide helpful information in case of hardware problems or problems with   loading a module into the kernel. In addition, with dmesg, you can determine what hardware is installed on your server. During every boot,

 Linux checks your hardware and logs information about it. You can view these logs using the command /bin/dmesg.

Clearing the kernel ring buffer
If you want you can backup the logs using dmesg > filename before clearing it. Just execute the following command to clear and frest start the ring buffer loggin (make sure you have logged in as root).

# dmesg -c

Execute the command dmesg to make sure the logs are cleared. Check man dmesg for more help.

Change /tmp file system from ext3 to ext4


Change /tmp file system from ext3 to ext4

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvde1             48G  7.3G   38G  17% /
/securetmp            962M   18M  896M   2% /tmp

Check /securetmp file type to make sure whether its ext3 or not.
# file /securetmp
/securetmp: Linux rev 1.0 ext3 filesystem data (needs journal recovery) (large files)

Then unmount /tmp
# umount /tmp

Next format /securetmp to use ext4 file system
# mkfs.ext4 /securetmp

You can verify it by running file command.
# file /securetmp
/securetmp: Linux rev 1.0 ext4 filesystem data (extents) (large files) (huge files)

Edit ext3 to ext4 for /securetmp in /etc/fstab:
# /securetmp  /tmp ext3 loop,noexec,nosuid,rw 0 0 ====>  /securetmp  /tmp ext4 loop,noexec,nosuid,rw 0 0

Run mount -a to get fstab updated
# mount -a

Thats it. We're done.

Mount: /dev/sdb1 already mounted or /mnt busy


mount: /dev/sdb1 already mounted or /mnt busy

[root@]# mount -t ext3  /dev/sdb1 /mnt
mount: /dev/sdb1 already mounted or /mnt busy

lsof didn’t provide any open files that might be linked to this problem or there was any “famd” running. Finally doing the following steps to remove the logical devices from the device-mapper driver helped us fix the problem.
[root@]# dmsetup ls
ddf1_44656c6c202020201028001510281f033832b7a2f6678dab   (253, 0)
ddf1_44656c6c202020201028001510281f033832b7a2f6678dab1  (253, 1)

[root@]# dmsetup remove ddf1_44656c6c202020201028001510281f033832b7a2f6678dab1

[root@]# dmsetup ls
ddf1_44656c6c202020201028001510281f033832b7a2f6678dab   (253, 0)

[root@]# dmsetup remove ddf1_44656c6c202020201028001510281f033832b7a2f6678dab

[root@]# dmsetup ls
No devices found

Tracking the scripts that send mails on exim


currently running:
# ps -C exim -fH ewww|awk '{for(i=1;i<=40;i++){print $i}}'|sort|uniq -c|grep PWD|sort -n

Few times ago:
# grep "cwd=" /var/log/exim_mainlog|awk '{for(i=1;i<=10;i++){print $i}}'|sort|uniq -c|grep cwd|sort -n

Tracking direct Spammers.(apache):
# netstat -plan |grep :25 | awk '{print $5}' |cut -d: -f1 |sort |uniq -c |sort -n

See the IP addresses accessing php files. This will work only in servers running php as CGI.
# ps aeuxf | grep php|awk -F'REMOTE_ADDR=' '{ print $2 }' |cut -d\  -f 1 | uniq -c | sed 's/^[ ]*//'

To get the active php processes running on the server.
# ps aeuxf | grep php | awk -F'SCRIPT_FILENAME=' '{ print $2 }'  |cut -d\  -f 1 | uniq -c |  sed 's/^[ ]*//'

To list the php processes and the time they have been running on the server.
# ps -eo pid,cmd,etime,args --sort:etime | grep php


Kernel: Neighbour table overflow


Nearly any linux 2.6.* kernel. Typically occurs in situations where there are large, flat networks (e.g. when the subnet mask is /16 and there are more than 1000 active nodes). Solution

1. Check current values:
# grep . /proc/sys/net/ipv4/neigh/default/gc_thresh*

2. Echo new (higher) values into each of the above /proc entries:
# echo 512 > /proc/sys/net/ipv4/neigh/default/gc_thresh1
# echo 2048 > /proc/sys/net/ipv4/neigh/default/gc_thresh2
# echo 4096 > /proc/sys/net/ipv4/neigh/default/gc_thresh3

3. Add the same values to /etc/sysctl.conf:
# net.ipv4.neigh.default.gc_thresh1 = 512
# net.ipv4.neigh.default.gc_thresh2 = 2048
# net.ipv4.neigh.default.gc_thresh3 = 4096

Refer this link - https://supportcenter.checkpoint.com/supportcenter/portal?eventSubmit_doGoviewsolutiondetails=&solutionid=sk43772

Linux Drop Cache


The biggest advantage of Linux OS is maintaining cache for file-systems, memory pages etc to speedup the cpu process reducing the hard disk i/o. From 2.6.16 kernel a new mechanism has been introduced to have the kernel drop the pagecache, dentries or inodes cache which helps the administrator to throw away that script that allocated a ton of memory just to get rid of the cache.

NOTE : Its a good practice to run ” sync ” command before using the below options which will sync the
RAM cache back to the hard drives.

To free pagecache
# echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes
# echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes
# echo 3 > /proc/sys/vm/drop_caches

You will see a sudden drop in the ram memory usage after running this commands. Use either top or free -m command to check the memory usage.

Install ProFTPD on CentOS 6


Before we do anything else, we need to download the EPEL repository which will allow us to install ProFTPD on our virtual private server with yum.

# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm



# yum install proftpd

Once ProFTPD is installed, you can make the needed adjustments in the configuration. Unlike some other ftp configurations, ProFTPD disables anonymous login from the outset and we only need to address a small change in the config file. Open up the file:
# vi /etc/proftpd.conf

Go ahead and change the Server Name to your host name.
ServerName                      "example.com"

Save and Exit from that file.

Then, to prevent any issues, add your droplet name and IP address to the hosts file:
# vi /etc/hosts

The line can look something like this:
12.34.56.789 servername

Restart after you have made all of your changes:
# service proftpd restart

That's it.

Install ImageMagick and Imagick on Cpanel Server


ImageMagick

Check first if it’s installed:
# /scripts/checkimagemagick

Proceed with Installation:
# /scripts/installimagemagick

Installation will take a couple minutes as it will install other packages needed by ImageMagick. After the installation, you can check your ImageMagick version:
# /usr/bin/convert --version

It will give you something like:
Version: ImageMagick 6.4.8 2009-05-11 Q16 OpenMP http://www.imagemagick.org Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC

Imagick

WHM -> Software -> Module Installers -> PHP Pecl (manage). On the box below Install a PHP Pecl enter imagick and click Install Now button

Restart Apache and check your phpinfo page to see the details of Imagick and ImageMagick as well.

Uninstall

ImageMagick:
# /scripts/cleanimagemagick

Imagick:
WHM -> Software -> Module Installers -> PHP Pecl (manage). Click on Uninstall button for Imagick.

That's all