Showing posts with label Trick. Show all posts
Showing posts with label Trick. Show all posts

Wednesday, 7 December 2011

Swap Partition And Swap File In RHEL 5


[root@server1 ~]#swapoff –a
[root@server1 ~]# fdisk /dev/hda
Create a new partition of 2*RAM and assign the id 82 which is allocated for swap partition.
[root@server1 ~]#mkswap –v1 /dev/hda15
-v1 switch is used for version number.
Add an entry in /etc/fstab file
[root@server1 ~]# vi /etc/fstab
/dev/hda15 swap swap defaults 0 0
:wq Activate the swap partition using
[root@server1 ~]#swapon –a
(which reads /etc/fstab and turns on all the swap entries in the list.
[root@server1 ~]# swapon –s (is used to check the status of swap partition.)
Creating a swap file
[root@server1 ~]#swapoff –a
[root@server1 ~]#dd if=/dev/zero of=swapfile bs=512M count=1
[root@server1 ~]#mkswap –v1 /root/swapfile
Add an entry in the /etc/rc.local
[root@server1 ~]#vi /etc/rc.local
Swapon /root/swapfile

Wednesday, 7 September 2011

Linux Delete / Remove MBR

I've installed an usb image on Pen drive along with MBR. Now, I need to use this USB pen for other purpose. Is there a way in Linux to delete the mbr (just like dos fdisk /mbr command)?

You can delete the mbr (master boot recored) using the dd command itself. The following command will completely delete your MBR, which contains all your partition information. So make sure you use the correct device name:
# dd if=/dev/zero of=/dev/sdc bs=512 count=1
Sample outputs:
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00308483 s, 166 kB/s
/dev/sdc is your USB pen device.

Thursday, 1 September 2011

How-To: Managing services with update-rc.d


Linux services can be started, stopped and reloaded with the use of scripts stocked in /etc/init.d/.
However, during start up or when changing runlevel, those scripts are searched in /etc/rcX.d/ where X is the runlevel number.
This tutorial will explain how one can activate, deactivate or modify a service start up.
When installing a new service under debian, the default is to enable it. So for instance, if you just installed apache2 package, after you installed it, apache service will be started and so will it be upon the next reboots.
If you do not use apache all the time, you might want to disable this service from starting up upon boot up and simply start it manually when you actually need it by running this command:
# /etc/init.d/apache2 start
You could either disable this service on boot up by removing any symbolic links in /etc/rcX.d/SYYapache2 or by using update-rc.d.
The advantage of using update-rc.d is that it will take care of removing/adding any required links to /etc/init.d automatically.
Taking apache2 as an example, let's examine how /etc/rcX.d is looking like:
# ls -l /etc/rc?.d/*apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc0.d/K91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc1.d/K91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc2.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc3.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc4.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc5.d/S91apache2 -> ../init.d/apache2
lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc6.d/K91apache2 -> ../init.d/apache2
As you can see, for runlevels 0, 1 and 6 there is a K at the beginning of the link, for runlevels 2, 3, 4 and 5, there is a S. Those two letters stands for Kill and Start.
On Debian and Ubuntu, runlevels 2, 3, 4 and 5 are multi-users runlevels.
Runlevel 0 is Halt.
Runlevel 1 is single user mode
Runlevel 6 is reboot

1. Removing a Service

If you want to totally disable apache2 service by hand, you would need to delete every single link in /etc/rcX.d/. Using update-rc.d it is as simple as:
# update-rc.d -f apache2 remove
The use of -f is to force the removal of the symlinks even if there is still /etc/init.d/apache2.
Note: This command will only disable the service until next time the service is upgraded. If you want to make sure the service won't be re-enabled upon upgrade, you should also type the following:
# update-rc.d apache2 stop 80 0 1 2 3 4 5 6 .

2. Adding a service

2.1. Default priorities

Now, if you want to re-add this service to be started on boot up, you can simply use:
# update-rc.d apache2 defaults
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K20apache2 -> ../init.d/apache2
/etc/rc1.d/K20apache2 -> ../init.d/apache2
/etc/rc6.d/K20apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2

2.2. Custom priorities

But as you can see, the default value is 20 which is pretty different than 91 ... a S20 link is started before a S91 and and K91 is kill before K20.
To force apache2 to be started with priorities 91 for both Start and Kill, we need to use the following command:
# update-rc.d apache2 defaults 91
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K91apache2 -> ../init.d/apache2
/etc/rc1.d/K91apache2 -> ../init.d/apache2
/etc/rc6.d/K91apache2 -> ../init.d/apache2
/etc/rc2.d/S91apache2 -> ../init.d/apache2
/etc/rc3.d/S91apache2 -> ../init.d/apache2
/etc/rc4.d/S91apache2 -> ../init.d/apache2
/etc/rc5.d/S91apache2 -> ../init.d/apache2

2.3. Different priorities for Start and Kill

Alternatively, if you want to set different priorities for Start than for Kill, let say Start with 20 and Kill with 80, you will need to run:
# update-rc.d apache2 defaults 20 80
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2

3. Specifying custom runlevels

Finally, if you only want to Start and Kill on specific runlevels, like for instance starting apache with priority 20 on runlevels 2, 3, 4 and 5 and Kill with priority 80 on runlevels 0, 1 and 6:
# update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2
Or, to start with priority 20 for runlevel 2, 3 and 4 and priority 30 for runlevel 5 and kill with priority 80 for runlevel 0, 1 and 6:
# update-rc.d apache2 start 20 2 3 4 . start 30 5 . stop 80 0 1 6 .
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S30apache2 -> ../init.d/apache2 

How-To: Find missing packages with apt-file


apt-file is a command line tool for searching packages in Debian/Ubuntu repositories.
Unlike apt-cache search, apt-file can find files in uninstalled or can list the content of uninstalled packages.
This tutorial will show how to install apt-file, update and search or list packages and files.
Have you ever search which package contains a certain file. One could use:
dpkg -S /path/to/file
This will do the job unless the package is not yet installed.
Hopefully, both Debian and Ubuntu have a package repository, http://packages.debian.org/ and http://packages.ubuntu.com.
Command line lover will love apt-file which will do the same job, but you won't have to use your web browser.
To install apt-file, simply run:
$ apt-get install apt-file
Then we need to update apt-file database with:
$ sudo apt-file update
After some times, once the Contents files are downloaded from the repositories, you will be able to query the database for myfile or path/to/myfile with the following commands:
$ apt-file search myfile
or
$ apt-file search path/to/myfile
Listing the content of an uninstalled package can be done with:
$ apt-file list mypackage
Finally, you can delete the cache files with:
$ sudo apt-file purge

Linux Create A Bootable USB Pen


How do I create a bootable USB pen drive to start my CentOS / Fedora Linux? How do I create a bootable USB flash pen for installation of Linux, and for creation of rescue and other special-purpose boot disks using command line options?                                                     

You need to format your USP pen device as Win FAT32. Once formatted type the following commands to make it bootable.


USB Pen Device Name : /dev/sdb1
Our Device Names

  1. DVD Mount : /media/cdrom0
  2. USB Mount Point : /media/usb
  3. USB File System : Win FAT32
  4. ISO or DVD Image : Fedora / CentOS / RHEL
WARNING! These examples may crash your computer or result in data loss if not executed properly. You must understand device names, file systems and required to use fdisk partition table manipulator for Linux. This howto intended to simplify first-time installation of Linux, and for creation of rescue and other special-purpose boot disks.
To find information about your devices and current partitions run:
# dmesg | less
# dmesg | egrep -i 'cd|dvd'
# fdisk -l

Use the first command to identify the USB device name.

Mount CD/DVD ISO or DVD ITSELF

Type the following command to mount Fedora 12 iso image:
# mount Fedora-12-x86_64-netinst.iso -o loop /media/cdrom0/
# DVD=/media/cdrom0
# ls -l $DVD

Sample outputs:
total 6
dr-xr-xr-x 3 root root 2048 2009-11-09 05:37 EFI
drwxr-sr-x 3 root  499 2048 2009-11-09 05:37 images
drwxr-sr-x 2 root  499 2048 2009-11-09 05:36 isolinux
You need to use files stored in isolinux directory to create a bootable usb pen.

Format Usb

Create the fdisk partition:
# fdisk /dev/sdb
You need to create only 1 partition. Next format the partition:
# USB=/media/usb
# mkdosfs /dev/sdb1

Finally mount the partition:
# mkdir -p /media/usb
# mount /dev/sdb1 /media/usb
# USB=/media/usb

Copy Required Files

Type the following commands:
# cp -av $DVD/isolinux/* $USB
# cd $USB
# rm isolinux.bin boot.cat TRANS.TBL
# mv isolinux.cfg syslinux.cfg

Also copy the installer's initial RAM disk $DVD/images/pxeboot/initrd.img (for CentOS / RHEL Linux use $DVD/RedHat/images/pxeboot/initrd.img file) CD/DVD onto the usb drive:
# cp -v $DVD/images/pxeboot/initrd.img $USB

Unmount the USB drive

# umount /dev/sdb1

Make the USB Bootable

Type the following command to make the USB drive bootable
# syslinux /dev/sdb1
# mount /dev/sdb1 $USB

syslinux is a boot loader for the Linux operating system which operates off an MS-DOS/Windows FAT filesystem.

Install Grub

Type the following command to install GRUB on the USB device:
# grub-install --root-directory=$USB /dev/sdb
Create grub.conf:
# cd $USB
# mkdir -p boot/grub

Edit the grub.conf file
default=0
timeout=5
root (hd1,0)
title Fedora Linux
kernel /vmlinuz
initrd /initrd.img
Finally, unmount the USB pen drive, enter:
# umount /dev/sdb1
Your USB pen is ready and should be bootable from the USB device. This can be used to install Fedora or CentOS or RHEL. You can also copy other required tools (such as sniffers or data recovery tools) on this pen. This is left as exercise for the reader.

Unetbootin Installer of Linux/BSD Distributions (GUI Tools)

If command line options are too complicated to follow. Try UNetbootin, which allows you to create bootable Live USB drives for a variety of Linux distributions from Windows or Linux, without requiring you to burn a CD. To install to a partition or USB drive type the following command:
$ sudo apt-get install unetbootin
Sample outputs:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  lsb-desktop m4 aggregate lsb pax lsb-graphics lsb-core ncurses-term lsb-cxx
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  unetbootin-translations
The following NEW packages will be installed:
  unetbootin unetbootin-translations
0 upgraded, 2 newly installed, 0 to remove and 9 not upgraded.
Need to get 428kB of archives.
After this operation, 1,843kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://archive.ubuntu.com karmic/universe unetbootin 356-1 [223kB]
Get:2 http://archive.ubuntu.com karmic/universe unetbootin-translations 356-1 [205kB]
Fetched 428kB in 2s (156kB/s)
Selecting previously deselected package unetbootin.
(Reading database ... 254825 files and directories currently installed.)
Unpacking unetbootin (from .../unetbootin_356-1_amd64.deb) ...
Selecting previously deselected package unetbootin-translations.
Unpacking unetbootin-translations (from .../unetbootin-translations_356-1_all.deb) ...
Processing triggers for desktop-file-utils ...
Processing triggers for man-db ...
Setting up unetbootin (356-1) ...
Setting up unetbootin-translations (356-1) ...
Type the following command to start unetbootin:
$ unetbootin
Fig.01: UNetbootin install Linux/BSD distributions to a partition or USB drive

Make USB pen drive as a bootable device (RedHat)

If you cannot boot from the DVD/CD-ROM drive, but you can boot using a USB device, such as a USB pen drive, the following alternative boot method is available:

To boot using a USB pen drive, use the dd command to copy the diskboot.img image file from the /images/ directory on the DVD or CD-ROM. For example:

dd if=diskboot.img of=/dev/sda 

Your BIOS must support booting from a USB device in order for this boot method to work.

How-To: Recover root password under linux with single user mode


 It happens sometime that you can't remember root password. On Linux, recovering root password can be done by booting Linux under a specific mode: single user mode.
This tutorial will show how to boot Linux in single user mode when using GRUB and finally how to change root password.
During normal usage, a Linux OS runs under runlevels between 2 and 5 which corresponds to various multi-user modes. Booting Linux under runlevel 1 will allow one to enter into a specific mode, single user mode. Under such a level, you directly get a root prompt. From there, changing root password is a piece of cake.

1. Entering runlevel 1

Some Linux distribution, such as Ubuntu for instance, offer a specific boot menu entry where it is stated "Recovery Mode" or "Single-User Mode". If this is your case, selecting this menu entry will boot your machine into single user mode, you can carry on with the next part. If not, you might want to read this part.
Using GRUB, you can manually edit the proposed menu entry at boot time. To do so, when GRUB is presenting the menu list (you might need to press ESC first), follow those instructions:
  • use the arrows to select the boot entry you want to modify.
  • press e to edit the entry
  • use the arrows to go to kernel line
  • press e to edit this entry
  • at the end of the line add the word single
  • press ESC to go back to the parent menu
  • press b to boot this kernel
The kernel should be booting as usual (except for the graphical splash screen you might be used to), and you will finally get a root prompt (sh#).
Here we are, we have gained root access to the filesystem, let's finally change the password.

2. Changing root password

As root, changing password does not ask for your old password, therefore running the command:
# passwd
will prompt you for your new password and will ask you to confirm it to make sure there is no typo.
That's it, you can now reboot your box and gain root access again

Add a line in a specific position with Linux


If you need to add a line to a file in Linux, and you need to add that line in a specific position of the file there is an easy solution, even if you need to do it to hundreds of thousands of files.
Consider this file:
line 1
line 2
line 4
As you can see we missed line 3, so to add it just execute this command:
sed '3iline 3' filename.txt
Parts of the command
  • sed: is the command itself
  • 3: is the line where you want the new line inserted
  • i: is the parameter that says sed to insert the line.
  • line 3: is the text to be added in that position.
  • filename: is the file where sed is going to act.
That will just put the result in the screen but the file will remain the same, you can redirect the output to a new file:
sed '3iline 3' input.txt > output.txt

Shell script add a line to multiple files

Now, if you need to do this with hundreds of files:
for i in *; do sed '3iline 3"' $i > /tmp/$i ; done
You can then copy from /tmp/ to your actual folder and you have been changed all files in current folder.

General error: 1030 Got error 134 from storage engine


If you get this error General error: 1030 Got error 134 from storage engine when trying to access any PHP application that uses MySQL, do not panic as the problem is really easy to solve.
All you have to do is repair your tables, you can use PHPMyAdmin for that:
  1. Login into PHPMyAdmin
  2. Choose the database you want to work with.
  3. Check the boxes of all your tables.
  4. Below the tables, there is a drop down menu, choose "repair table" from it.
You can also use the MySQL command line,
mysql>use [database];

mysql>check table [your_table];
If you see any other thing different than this:
+-----------------+-------+----------+----------+
| Table           | Op    | Msg_type | Msg_text |
+-----------------+-------+----------+----------+
| piwik.piwik_pdf | check | status   | OK       |
+-----------------+-------+----------+----------+
1 row in set (0.00 sec)
Of course your table name will change, but the Msg_text should be OK.
If it is not OK run this command:
mysql>repair table [your_table];
That should solve your problem with General error: 1030 Got error 134 from storage engine error.

How to enable gzip compression in Apache 2.x


If you check your website stats, you will see that most of your visitors are using modern web browsers, those browsers now support gzip (compressed) content. What does that mean?.
Of course you know about zipping a file or group of files to make them use less space, maybe you zipped files to send them via email. Well, today web servers can also compress the content before sending it to your readers, speeding up your site, and lowering the bandwidth requirements of your server.
When a visitor's browser ask for a page of your site, it will let your server know if it can handle compressed content, and if that is the case, it is desirable that your server can send a compressed version of your page.
How to enable web page compression in Apache
First lets be sure your server supports compression, to do that it needs to have the mod_deflate module:
cat httpd.conf | grep deflate
You should see this:
LoadModule deflate_module modules/mod_deflate.so
If it is commented, comment it out.
Now edit the file httpd.conf or Apache2.conf depending on your distribution.
And add these lines, at the end of the file:
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE

# You can't compress what is already compressed

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

# Make proxies work as they should.
<IfModule mod_headers.c>
Header append Vary User-Agent
</IfModule>

</IfModule> 
The mod_deflate module sends a Vary: Accept-Encoding HTTP response header to alert proxies that a cached response should be sent only to clients that send the appropriate Accept-Encoding request header. This prevents compressed content from being sent to a client that will not understand it.
Now simply restart Apache and you are done, you can check your site here