Friday, February 6, 2009

Setting up a scheduler in Linux (Cron Jobs: Crontab tutorial)

There are cases wherein we have to execute a command or a script say for daily basis for some maintenance purposes. In Linux there is a way that we could do it. It is through crontab (a cron job).

Assuming that we want to execute a command. Say we want to reset and resync our clock time with NTP server pool.

a.) Set up for scheduling command

The first thing to do is to locate the complete path of the command. So for our example let us locate where the complete path for ntpdate command is. Issue this command:

# whereis ntpdate

This will show you the different paths like this:

ntpdate: /usr/sbin/ntpdate /usr/share/man/man8/ntpdate.8.gz

Just take note of the bin or sbin path of the command. So in our case it is /usr/sbin/ntpdate.

b.) for scripts here's the set up

First create your script. If it is a shell script then on the first line of code you must have this. For other scripts like PHP just google out there and surely you will find many.

#!/bin/sh

Save your script. Set the file permission to 775 by issuing:

# chmod 775 (script)

Take note of the path where you saved the script we need this later on.

Next we'll open the crontab file by issuing this command.

# crontab -e

The flag 'e' signifies editing of the crontab file.

Once you're prompted on a file like this

~
~
~
~
"/tmp/crontab.XXXXtkRgpi" 7L, 546C

Press 'i' or insert key then you can start writing your schedules. When you are done. Just press escape key then press colon then 'w' and 'q' (:wq (a vi editor way of saving remember..)).


Before we proceed let me just discuss the format of the syntax that you will be enterring here.

* * * * * /complete/path/to/script/(command / filename of script to execute)

Example

*/1 * * * * /usr/sbin/ntpdate north-america.pool.ntp.org > /dev/null 2>&1

Let me explain...

the first * denotes minute (it can be from 0 - 59) minute
the second * denotes hour (military time 0 - 23) hour
the third is day of month (1 - 31)
4th is month (1 - 12)
5th the last one is day of week (0 - 6) 0 = Sunday

Say you want a schedule everyday at 12 midnight... here is an example

0 0 * * * /complete/path/to/script/(command / filename of script to execute)

If you want to execute per minute, here is how

*/1 * * * * /usr/sbin/ntpdate north-america.pool.ntp.org > /dev/null 2>&1

To list your current active cron jobs, issue:

# crontab -l

I hope you find these stuffs useful. Thanks, Cheers and God Bless!!!

Thursday, February 5, 2009

Fixed PHP compile error: Note that the MySQL client library is not bundled anymore!

Just a note that is worth be reminded for all CentOS version 5 users. One of the issues being encountered on compiling PHP with MySQL is the error:

Note that the MySQL client library is not bundled anymore!

Now how are you going to fix this?? Here's how.

On your configure command just locate or point the mysql lib base dir in my case it is on /usr

so just do ./configure --with-mysql=/usr

That will resolve your problem.

Hope this helps. Thanks, Cheers and God Bless!!

Hardening PHP (Protecting your PHP from Flaws)

There are rumors going on out there that PHP has some pitfalls. This could lead up to some security risk most especially if you have a PHP application dealing with the outside world. To avoid this, some nice guys out there developed a 'guardian angel' for PHP. The guardian angel I am referring into is the suhosin. Yes suhosin is a korean word which mean 'guardian angel'. It is a PHP extension the secures PHP from its pitfalls or vulnerability.

I will discuss here on how to have this cool stuff on your PHP settings.

1. First download the suhosin signature key. Goto /usr/src then issue this command

# wget http://www.hardened-php.net/hardened-php-signature-key.asc

2. Import it into the GNU Privacy Guard

# gpg --import < hardened-php-signature-key.asc

3. Download the suhosin source

# wget http://download.suhosin.org/suhosin-0.9.27.tgz

4. Unpack the package

# tar xvzf suhosin-0.9.27.tgz

5. cd suhosin-0.9.27

6. After getting into the suhosin directory, issue the following

# phpize
# ./configure
# make
# make install

7. The library suhosin.so is most likely installed on /usr/src/suhosin-0.9.27/modules or it can be on /usr/local/lib/php/extensions/no-debug-non-zts-20060613/suhosin.so

8. copy the suhosin.so to /usr/local/lib/php/extensions that is if you have installed php from source. Check other resources if you did not.

# cp /usr/src/suhosin-0.9.27/modules/suhosin.so /usr/local/lib/php/extensions

9. Open the php.ini

# vi /usr/local/lib/php.ini

10. Set the following parameters on php.ini

extension_dir = "/usr/local/lib/php/extensions/"

extension=suhosin.so

11. save it then restart apache

12. You should now see the suhosin among the enabled extension on your PHP. Check on the phpinfo for this.

Hope you find these info useful. Cheers, Thanks and God Bless!!!!

Tuesday, February 3, 2009

Smart Host: Relaying Mails from Sendmail to Gmail

There are cases wherein the mails that are being sent from server does not arrive at the recipient's inbox. One possible cause is that the mail was considered a spam. One solution that can be done, dealing with this issue, is to relay the emails to a trusted SMTP host say like Gmail. How are we going to do it here is how.

First you need to have sendmail a native MTA for Linux. Here are the packages you need to have

- sendmail
- senmail-cf
- sendmail-devel

All of these packages can be installed via yum.

Next enable the smart host feature on sendmail. Open the sendmail configuration file.

vi /etc/mail/sendmail.mc

Change this:

dnl define(`SMART_HOST',`smtp.gmail.com')dnl

to:

define(`SMART_HOST',`smtp.gmail.com')dnl

Next add this entry before the entry of smart host.

FEATURE(`authinfo',`hash /etc/mail/auth/client-info')dnl

If you don't have an /etc/mail/auth directory, as root, create one like this:

# mkdir -p /etc/mail/auth
# chmod 700 /etc/mail/auth

You won't have the next client-info files so you'll have to create it (with you favorite Unix editor). Change the permissions on the client-info file like this:

# chmod 600 client-info

So using my user_id@gmail.com email ID and password I made the following entry in /etc/mail/auth/client-info:

# cat /etc/mail/auth/client-info
AuthInfo:smtp.gmail.com "U:smmsp" "I:user_id" "P:password" "M:PLAIN"
AuthInfo:smtp.gmail.com:587 "U:smmsp" "I:user_id" "P:password" "M:PLAIN"

Yes, password is plain text. It is possible to encode the password but I don't have those instructions here.

Remember to replace user_id with your Gmail email ID (your email addresse without the @gmail.com) and password with your email password. Also make sure that the client-info file has a

How to compile it into a db file:

# cd /etc/mail/auth
# makemap -r hash client-info.db <> sendmail.cf

And lastly restart sendmail:

# /sbin/service sendmail restart

Now you can test it....

Hope you find this useful. Thanks, Cheers and God Bless!!!!

Monday, February 2, 2009

Some Linux Network Configurations and Files

I just wanted to list some of the common files or configurations in Linux for networking.

NETWORK Stuff

1.) /etc/sysconfig/network
-this one contains the hostname configuration of the machine so if you want to change the machine hostname do it here
- Sometimes the GATEWAY is configured here

2.) /etc/sysconfig/network-scripts/ifcfg-eth#

Ex. ifcfg-eth0 means eth0 , ifcfg-eth1 means eth1 or ethernet 1 or NIC 1
- the Network / LAN configuration of the machine where its IP is configured
- ifcfg-eth0:0 when there is a colon it means it is a virtual ethernet

3.) /etc/hosts
-local base bind or name service to resolve hostname

4.) /etc/resolv.conf
- DNS settings of the local machine