Skip to content


Email Alert on SSH Login

The following method can be used when a user login to a linux machine, an email will be sent with IP information. For this to work, you need to have the program mutt installed. Then add the following to the user’s .bashrc file, which is located in /home/$user/.bashrc echo `who` | /usr/bin/mutt -s "SSH Alert" For example, if you want an email sent everytime the user root login, edit the file /home/root/.bashrc and add the above code.

Daily MySQL Database Backup & Email

The following two scripts on crontab will automatically back up and email the database on a timely manner. This script will work on daily backup of Blogs such as WordPress / Drupal etc In this example, I will be using the directory /home/backup/database It is recommended to create two different shell script named and accordingly and set them as executable. chmod +x Database Backup The following script will backup the database using MySQLdump, then it will bzip2 the Database with the following filename database_DATE.sql.bz2 #!/bin/bash BACKUP="/home/backup/database/database_`date +%d-%m-%Y`.sql" /usr/bin/mysqldump -uUSERNAME -pPASSWORD --opt DATABASE $BACKUP /usr/bin/bzip2 $BACKUP Database Email The mail script will email the database as an attachment using mutt or mail, to a given email address. You can pick whichever suit your needs. Mutt #!/bin/bash /bin/echo "Backup Database for `date +%d-%m-%Y`" | /usr/bin/mutt -s "Backup Database for `date +%d-%m-%Y`" -a /home/backup/database/database_`date +%d-%m-%Y`.sql.bz2 Mail Read more

Passwordless SSH Authentication on Linux

There are two ways of achieving passwordless authentication on a Linux Box.

On Debian/Ubuntu you can just type:

$ cd $HOME
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key:

Press Enter each question and you will have a pair of keys ready to use.

You will be asked for a passphrase but if you do not want to insert anything just leave it blank.

This procedure will create one private and one public key.

$ ls .ssh/
id_rsa  known_hosts
The private key must be secured on your box whereas the public key can be copied across
$ ssh-copy-id -i .ssh/

At this time you will be asked for the password but once the key has been copied you will be able to ssh just perfectly.

Read more

Quick tip: change default text editor on Linux shell

Doing visudo you get nano instead of your favorite text editor? Mine is vim therefore I issue: DEBIAN/UBUNTU way # update-alternatives --config editor There are 4 alternatives which provide `editor'. Selection Alternative ----------------------------------------------- 1 /usr/bin/vim.tiny 2 /bin/ed *+ 3 /bin/nano 4 /usr/bin/vim.basic Press enter to keep the default[*], or type selection number: Selecting 4 I’m ready to use my full syntax colors when I edit any file. OTHER DISTROS Edit your .bashrc file and add the following: EDITOR=vim export EDITOR Next login you will have your VIM working. If you want to have it immediately and only for this session just type # export EDITOR=vim and press Enter.

How to install Debian from USB drive

I’ve come across multiple ways of doing it but so far the best way is the following (I assume you are on i386 platform and you have access to the net): 1. Download boot.img.gz from Debian website 2. Download the net-install image choosing i386. 3. Plug your USB drive into a Linux PC, open the shell and type $ dmesg Last few lines should be like the following: sd 7:0:0:0: [sdb] Attached SCSI removable disk sd 8:0:0:0: [sdb] 4030464 512-byte logical blocks: (2.06 GB/1.92 GiB) sd 8:0:0:0: [sdb] Write Protect is off Now we know it has been mapped as sdb Use zcat to load the boot.img.gz onto your USB drive # zcat boot.img.gz /dev/sdb CAUTION!!! this will destroy the entire data on the USB drive, make sure you have done the backup. If you get an error ensure the following: – You are root (don’t use sudo) Read more

How to change the file timestamp on Linux

This tip might comes handy when you do a system check and you want to make sure you don’t check the same file twice. Let’s pretend that our “file1” is a conf file that needs review. As you can see the output of the command issued below shows that the file was last edited in June. Today I want to check the file without editing it and make sure next time I won’t check it again: $ ls -l total 0 -rw-r--r-- 1 luca luca 290 2009-06-29 16:33 file1 Touch is an excellent tool in this case: $ touch file1 $ ls -l total 0 -rw-r--r-- 1 luca luca 0 2009-08-29 19:43 file1 The Modification Time has changed and so has the access time. If you want to change just the modification time leaving the access time untouched try with the -m option $ touch -m file1 $ ls -l total 0 -rw-r--r-- 1 luca luca 0 2009-08-29 19:46 file1 $ stat file1 [. Read more

How to Zip on Linux

This is fairly straight forward yet people find it somewhat confusing. Follow these easy steps below or if you need more help, use the Linux manpage. How to Zip a file(s) _The Examples shows how to zip a single file and multiple files into a single .zip zip photo.jpg zip photo1.jpg photo2.jpg photo3.jpg How to Zip an entire Directory The Example shows how to zip a directory called photos into a zip file called zip -r photos How to Unzip a .Zip File unzip How to list contents of a .Zip file without Unzipping unzip -l