Add swap on Linux

Thursday, October 1st, 2009

[root@host ~]# dd if=/dev/zero of=/swapfile bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
[root@host ~]# sync
[root@host ~]# mkswap /swapfile
Setting up swapspace version 1, size = 1073737 kB
[root@host ~]# swapon /swapfile
[root@host ~]# echo “/swapfile swap swap defaults 0 0″ >> /etc/fstab

Script to generate /etc/network/interfaces file on Ubuntu/Debian

Wednesday, September 30th, 2009

#!/bin/bash
ifile=/etc/network/interfaces
echo -n “Insert IPstart:”
read ipstart
echo -n “Insert IPend:”
read ipend
echo -n “Insert Gateway:”
read gw
#echo -n “Insert Subnet: ”
#read subnet
echo -n “Insert Netmask: ”
read net
echo -n “Insert aliast to start: ”
read ali

firstIp=`echo “${ipstart%.*}”`
lastIpStart=`echo “${ipstart##*.}”`
lastIpEnd=`echo “${ipend##*.}”`
dif=`echo $(($lastIpEnd-$lastIpStart))`

ip=$lastIpStart
for ((i=$ali;i<=$ali+$dif;i++)){
echo "auto eth0:$i" >>$ifile
echo “iface eth0:$i inet static” >>$ifile
echo “address $firstIp.$ip” >>$ifile
echo “network $subnet” >>$ifile
echo “netmask $net” >>$ifile
echo “gateway $gw” >> $ifile
echo ” ” >> $ifile
let ip++
}

Script to generate interface configs on Linux

Wednesday, September 16th, 2009

Generate ifcfg-eth1 307-366 configs.

touch ifcfg-eth1:{307..366}
for f in ifcfg-eth1:{307..366}; do cat origfile > “$f”; done
for f in eth1:{307..366}; do echo “DEVICE=$f” >> “ifcfg-$f”; done
for f in {307..366}; do echo “IPADDR=aaa.bbb.ccc.$((f-239))” >> “ifcfg-eth1:$f”; done

origfile contains:

GATEWAY=eee.fff.ggg.hhh
TYPE=Ethernet
BOOTPROTO=none
NETMASK=255.255.255.192

How do I find out Linux Disk utilization RHEL/Centos?

Wednesday, September 2nd, 2009

iostat syntax for disk utilization report

iostat -d -x interval count

-d : Display the device utilization report (d == disk)
-x : Display extended statistics including disk utilization

interval : It is time period in seconds between two samples . iostat 2 will give data at each 2 seconds interval.

count : It is the number of times the data is needed . iostat 2 5 will give data at 2 seconds interval 5 times

Display 3 reports of extended statistics at 5 second intervals for disk

Type the following command:

# iostat -d -x 5 3

Output:

[root@vari Desktop]# iostat -d -x 5 3

Linux 2.6.18-128.7.1.el5xen (vari.taashee.com) 08/26/2009 _i686_ (2 CPU)

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util

sda 2.41 12.42 3.27 5.37 134.84 142.70 32.13 0.12 14.04 1.83 1.58

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util

sda 0.00 1.60 0.00 0.40 0.00 16.00 40.00 0.00 0.00 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 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

Where,

rrqm/s : The number of read requests merged per second that were queued to the hard disk
wrqm/s : The number of write requests merged per second that were queued to the hard disk
r/s : The number of read requests per second
w/s : The number of write requests per second
rsec/s : The number of sectors read from the hard disk per second
wsec/s : The number of sectors written to the hard disk per second
avgrq-sz : The average size (in sectors) of the requests that were issued to the device.
avgqu-sz : The average queue length of the requests that were issued to the device
await : The average time (in milliseconds) for I/O requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.

svctm : The average service time (in milliseconds) for I/O requests that were issued to the device
%util : Percentage of CPU time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100%.

How do I interpret the output result for optimization?

First you need to note down following values from the iostat output:

1. The average service time (svctm)
2. Percentage of CPU time during which I/O requests were issued (%util)
3. See if a hard disk reports consistently high reads/writes (r/s and w/s)

If any one of these are high, you need to take one of the following action:

Get high speed disk and controller for file system (for example move from SATA I to SAS 15k disk)
Tune software or application or kernel or file system for better disk utilization
Use RAID array to spread the file system

Installing perl audio converter

Wednesday, August 5th, 2009

Download the latest version of pacpl from:

Download PACPL from http://pacpl.sourceforge.net/

install cpan, either with yum,apt..

Install these cpan modules:

cpan install Ogg::Vorbis::Header
cpan install Inline::MakeMaker
cpan install MP3::Tag
cpan install Audio::FLAC::Header
cpan install MP4::Info
cpan install Audio::WMA
cpan install Audio::Musepack
cpan install CDDB_get

Untar the pacpl source and move the binary into /usr/bin and the share/pacpl files into /usr/share/pacpl, also the config file (pacpl.conf) should be in /etc/pacpl/

To convert from .caf to mp3 use:

pacpl –to mp3 file.caf

How to sort folders by size with one command line in Linux

Thursday, July 30th, 2009

du –max-depth=1 $PWD | sort -n -r
du -H –max-depth=1 $PWD

“-H” – produce human readable format sizes (like: 1K 10M 1G)

A good and short one liner using xargs

du -s ./* | sort -n| cut -f 2-|xargs -i du -sh {}

This will sort the folders according to size and display them in human readable format… to reverse the list
use sort -nr option.

How to redirect my website to be opened through HTTPS?

Saturday, July 25th, 2009

In order to redirect your website to be opened through HTTPS, you should add the following rewrite rule in your .htaccess file:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.domain.com/%{REQUEST_URI} [R,L]

This will redirect your domain to https://www.yourdomain.com. If you wish the redirect to work without www, you should remove it from the rewrite rule:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://domain.com/%{REQUEST_URI} [R,L]

Howto configure vsftpd

Saturday, July 11th, 2009

yum install vsftpd

[root@shifu ~]# cat /etc/vsftpd/vsftpd.conf | grep -v ^#
anonymous_enable=NO # disable anonymous account
local_enable=YES # enable local users
write_enable=YES # enable uploading files
local_umask=022 # mask
dirmessage_enable=YES
xferlog_enable=YES # log messages
connect_from_port_20=YES
xferlog_std_format=YES
idle_session_timeout=600
chroot_list_enable=YES # chroot users into their own dir
listen=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

/etc/vsftpd/chroot_list contains a list of all users that will be in chroot

Amazon AWS and RightScale howto

Sunday, July 5th, 2009

These days got my first project regarding AWS. I never used before tools like s3sync and s3cmd. Basically these tools allow you to execute commands on the AWS server or to sync with your aws account.
First issue I got was the fact that I didn`t had enviroment variables setup:

export AWS_ACCESS_KEY_ID=your_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_key

Note : older versions of s3sync had : AWS_ACCESS_KEY ..so dont forget to add : _ID

You can create a script like :

#!/bin/bash
export AWS_ACCESS_KEY_ID=your_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_key

s3cmd listbuckets

This will show all buckets you have on your S3 account.

Redirect subdomains to domain

Thursday, June 25th, 2009


ServerName server.domain.com
ServerAlias server server2.domain.com server2
ServerAlias *.example.com
# …