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 "^http://www.mrbass.org/" local_ref=1 SetEnvIfNoCase Referer "^http://mrbass.org/" local_ref=1 SetEnvIfNoCase Referer "^$" local_ref=1 <FilesMatch ".(iso|exe|zip|rar|jpg)"> Order Allow,Deny Allow from env=local_ref </FilesMatch>
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 boot code which is 446 bytes long
- the partiton table which is 64 btyes long
- the boot code signature which is 2 bytes long
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 directoryHowever, 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/usbdisklsof 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 ( http://www.insecure.org/nmap/ ) at 2006-12-18 18:37 PST Host phineasfreak.alrac.net (192.168.1.25) not scanned Host freewheelinfranklin.alrac.net (192.168.1.26) not scanned Host fatfreddy.alrac.net (192.168.1.50) not scanned Nmap finished: 256 IP addresses (0 hosts up) scanned in 8.248 secondsThis 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 ( http://www.insecure.org/nmap/ ) at 2006-12-18 18:42 PST Host freewheelinfranklin.alrac.net (192.168.1.26) appears to be up. MAC Address: 00:00:24:C1:1D:F0 (Connect AS) Host fatfreddy.alrac.net (192.168.1.50) 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.tarNow 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 filenameAnd to change it back:
# chattr -i filenameList 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 /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/secThese are typical results for an average IDE/SATA drive. Rhinestone Buttons Web Invoicing
Our Last Five Customers
- www.belgian-telecom.be
- www.Semotus.com
- WCB, Richmond
- www.AustinHamilton.ca
- www.icore.net
Server Software
Technical Guides
- Introduction to Linux
- Bash Beginners Guide
- Perl Programming Basics
- Shell Scripting
- Advanced Shell Scripting
- UNIX Sockets FAQ
- Mastering Regular Expressions
- UNIX Programming Examples
- C Programming (Marshall)
- Data Structures and Algorithms
- Art of UNIX Programming
- Advanced Linux Programming
- Linux Kernel Module Programming
- Linux for Mobile Devices
- Basic Unix Administration
- Free ringtones
