IT Consulting in Richmond, BC, Canada
Minek Systems Inc. - IT Consulting Vancouver, Canada, Linux Consulting
email fax 305-356-7141
250-2639 Viking Way, Richmond, BC, V6V 1N3, Canada

Linux Tips and Tricks

Prevent Hotlinking

If you want to prevent others to link directly to the images or archives on your website, there is a way to do it without using mod_rewrite. Add the following code to the Apache config file:
SetEnvIfNoCase Referer "^" local_ref=1
SetEnvIfNoCase Referer "^" local_ref=1
SetEnvIfNoCase Referer "^$" local_ref=1
<FilesMatch ".(iso|exe|zip|rar|jpg)">
Order Allow,Deny
Allow from env=local_ref

Backing up Master Boot Record (MBR)

The MBR is a 512 byte segment on the very first sector of your hard drive composed of three parts: The core of the backup command is dd, which will be familiar to every system administrator, especially to those who intend to clone an entire hard disk. To see all the options type man dd. As we want to back up only the first 512 bytes we need to append some arguments to it. Here is the full command you need (and remember to run it as the root user, su (and sudo for Ubuntu users):
dd if=/dev/hda of=/home/richmondg/mbr_backup bs=512 count=1

Restoring the MBR
You can use a live CD to access your hard drive and read the backup off any removable media such as a USB stick. Here is the command:
dd if=/dev/sda/mbr_backup of=/dev/hda bs=512 count=1

mkdir -p creates nested directories

By default, mkdir will only create a top level folder. So the following command will fail:
% mkdir some/nested/folder
mkdir: cannot create directory `some/nested/folder': No such file or directory
However, if you pass the -p parameter, mkdir will create all of the nested directories.
% mkdir -p some/nested/folder

lsof shows open files

lsof lists all open files on the system. This is useful for determining what is reading or writing to a certain file. For example, if you canít unmount a drive because it is busy, run the following command to see what is still using the drive:
% lsof | grep /media/usbdisk
lsof can even be used to see what is listening on a given port. For example:
% lsof | grep 3000
ruby      6126       paul    5u     IPv4      25036              TCP *:3000 (LISTEN)
There is a ruby process (with process id 6126) listening on port 3000. Now, we can use ps to see the full process information:
% ps ax | grep 6126
 6126 pts/1    Sl+    0:26 ruby ./script/server

Discovering Who's On Your Network

Even small networks can pick up some surprise guests that slip in when you're not looking. Or maybe, as your network has grown, you haven't been very diligent about keeping track of IP addresses, so you want to make a list. You don't have to run around to every network host, but can perform host discovery from the comfort of your network administrator lair. There are a lot of ways to do this. One way is to use nmap. This example queries DNS for IP addresses and hostnames:
# nmap -sL 192.168.1.* | grep (
Starting Nmap 4.10 ( ) at 2006-12-18 18:37 PST
Host ( not scanned
Host ( not scanned
Host ( not scanned
Nmap finished: 256 IP addresses (0 hosts up) scanned in 8.248 seconds
This command pings the network to see who is up, and reports IP addresses, hostnames, and MAC addresses and names:
# nmap -sP 192.168.1.*
Starting Nmap 4.10 ( ) at 2006-12-18 18:42 PST
Host ( appears to be up.
MAC Address: 00:00:24:C1:1D:F0 (Connect AS)
Host ( appears to be up.
MAC Address: 00:0D:B9:05:25:B4 (PC Engines GmbH)
Nmap finished: 256 IP addresses (5 hosts up) scanned in 4.657 seconds

Sort folders by size

# du -sm /home/* | sort -n

cpio works like tar, only better

It would be nice to combine find and tar. Here is how to do it:
# find / -name "*.mp3" | cpio -o --format=tar -F music.tar
Now extract the file:
# cpio -i -F music.tar

Make a File "immutable" or "unalterable"

It cannot be changed or deleted even by root. Note this works on (ext2/ext3) filesystems. And, yes, root can delete after it's changed back.
As root:
# chattr +i filename
And to change it back:
# chattr -i filename
List attributes
# lsattr filename

What Network Services are Running ?

# netstat -atup

Mounting an ISO Image as a Filesystem

This is great if you don't have the DVD hardware, but, need to get at the data. The following show an example of mounting the Fedora core 2 as a file.
# mkdir /iso0
# mount -o loop -t iso9660 /FC2-i386-DVD.iso /iso0

How fast is your drive ?

# hdparm -tT /dev/sda

Timing buffer-cache reads:   128 MB in  0.41 seconds =315.32 MB/sec
Timing buffered disk reads:  64 MB in  1.19 seconds = 53.65 MB/sec
These are typical results for an average IDE/SATA drive.

Rhinestone Buttons       Web Invoicing      

Our Last Five Customers

  • WCB, Richmond

Technical Guides

Other Links