Miscellaneous Linux Tricks

Paul Elliottpelliott@io.com
http://www.FreeBlackPatchPanel.com/pme/linux/MiscLinux.pdf

Miscellaneous Linux Tricks●●●Gnome PartitionEditor (gparted) CDfor partitions
Mount lost partitions
Linux boot floppy(with filesystem)●

Linux boot CD●

fetchmail with openssl

Gparted: Gnome Partition Editor

Gparted: Table of functionality
Are you sure you need to pay money for Partition Magic?

Use the gparted LiveCD

gparted comes with Ubuntu LiveCD and perhaps
others.
The Gparted LiveCD does not lock any existing
partitions.
Find at: http://gparted.sourceforge.net/livecd.php
download the iso and burn with K3B!
Boot the CD to use gparted!

Hey, save your partition setup!

Save your partition table:
sfdisk -d /dev/hda > hda.pt
To restore:
sfdisk /dev/hda < hda.pt

Save your MBR!
Save you MBR (Master boot record)
dd if=/dev/hda of=hda.mbr bs=512 count=1
to restore:
dd if=hda.mbr of=/dev/hda
back these files up in case of disk crash.
To restore the primary partition table without
overwriting the MBR type:dd if=hda.mbr of=/dev/hda bs=1 count=64 \skip=446 seek=446

Mount lost partitions!
●

●

●

●

Suppose yourpartition table iswiped out.
But the partitions
themselves are intact!
And you know wherethe partitions are!
Then you canrecover!

fdisk -l -u /dev/sdb●

Disk /dev/sdb: 73.4 GB, 73407865856 bytes●

255 heads, 63 sectors/track, 8924 cylinders, total 143374738 sectors●

Units = sectors of 1 * 512 = 512 bytes●●

Device Boot

StartEndBlocks

IdSystem●/dev/sdb1

59504760

143364059

41929650

83

Linux

●

/dev/sdb253207280

59504759

3148740

83

Linux

●

/dev/sdb3

6316064

8001

83

Linux

●

/dev/sdb4

16065

53207279

26595607+

83

Linux

●

●

16065*512=8225280 so offset of /dev/sdb4 is 8225280 bytes.

Suppose our partition table is wiped
out!
●

●

●

Of course, getting the offset by doing fdisk -lwould be impossible, that uses the partition
table.
mounting the partition directly, would also beimpossible for the same reason.
suppose we had the offset by other means
such as:●scanning for first bock of partition. (gpart)●we wrote it down before hand.

gpart - guess PC-type hard disk
partitions●●

gpart is a programthat scans a hard disk
looking for blocks thatlook like the start of apartition! It prints theoffset (in 1K blocks)
out. Does not use thepartition table (bydefault).
Read the gpartmanual page!

http://www.stud.uni-hannover.de/user/76201/gpart/losetup to the rescue!
●
●

●
●

●
●

losetup is usually used for loopback encryption.
But it can be used to create a device which is
an offset into another device!
losetup -o 8225280 /dev/loop0 /dev/sdb
You can now mount the partition by mounting
the loop back device.
mount -r /dev/loop0 /mnt
Now you can deal with the files and directories
like any other mounted device.

Windblows loves to overwrite theMBR!
●●●

windoze likes tooverwrite the masterboot record. (note toall you dual bootpeople.)some Distros do not
like to share theMBR! (if you install
multiple distros on
different partitions.
So you need a bootfloppy, or boot CD!

Boot floppy with filesystem is mostflexible.
●●●

Grub manual
describes a different
method.
filesystem method
allows you to easilychange the menu,
device map.
extra space available
for additional info.

Boot floppy with filesystem:
Step 1●●●

make filesystem on
floppy.
# mke2fs /dev/fd0

eliminate filesystem
checks.

●# tune2fs -c 0 /dev/fd0●# tune2fs -c 0 /dev/fd0

Boot floppy with filesystem:
Step 2●

mount the filesystem,
copy the grub files.

●# mount /dev/fd0 /mnt●

# mkdir -p /mnt/boot/grub●# for x in●/usr/lib/grub/*;do
cp $x /mnt/boot/grub;

●

●

\done

Boot floppy with filesystem:
Step 3●

Setup the grub floppy●# grub●grub> root (fd0)●grub> setup (fd0)●grub> quit

Now would be a good time to save
our work as a floppy image!
●●●#umount /mnt
# dd bs=18k if=/dev/fd0 \of=grubFS.flp.img# mount /dev/fd0 /mnt

Boot floppy with filesystem:
Step 4●●●●

Copy the menu, and devicemap.
#cp /boot/grub/menu.lst \ /mnt/boot/grub/menu.lst#cp /boot/grub/device.map \ /mnt/boot/grub/device.map

This step can be redone for adifferent grub setup!

What does a menu look like?
●# Modified by YaST2. Last modification on Sun Jun 18 11:27:22 CDT 2006●●

color white/blue black/light-gray●default●

timeout 8●

gfxmenu (hd0,0)/message●●###Don't change this comment - YaST2 identifier: Original name: linux###●

title SUSE Linux 10.1●

root (hd0,0)●●

kernel /vmlinuz root=/dev/sda3 vga=0x31a resume=/dev/sda2 splash=silent \showopts
initrd /initrd

Boot floppy with filesystem:
Step 5: extra information.
●

●

●

●

There is plenty of
extra space on thefloppy.
Why not save thepartition table, MBR
in case we need itlater?
# sfdisk -d /dev/sda \> /mnt/sda.pt# dd bs=512 count=1 \
if=/dev/sda \of=/mnt/sda.mbr

Boot floppy with filesystem:
Step 6●●

Don't forget tounmount thefilesystem before
removing the floppy.
# umount /mnt

Build a boot CD with K3B.
●

●

●

●

Many newer
computers have CD
player but no floppy!
Grub manual
describes how tobuild boot CD fromthe command line.
http://www.gnu.org/software/grub/manual/

We describe the pointand clicky way: K3b

K3b: New Data CD projectK3b: Right click, New directory
to create /boot directory on the CD.

K3b: click on boot directory to enterit.

as before, create grub subdirectory
within /boot and enter it.

Drag /usr/lib/grub/stage2_eltorito
into /boot/grub on the CD.

In the same way
drag /boot/grub/menu.lst anddevice.map.

At this point we could add any
additional info to the CD.
●●

like the partition table info and the MBR info,
like we did for the floppy.
In the interests of brevity, we will skip this step.

Now click on Project\Edit Boot
Images...

Click on Advanced options and New
and open stage2_eltorito

click on none, boot-info-table andset boot load size to 0x4

Almost done, click burn

Click on “Only Create Image”●●●

For some reason,
direct burning does
not work.
If the local computer
does not have a CDburner, the iso can betransferred to one
that does.
Use tools\Burn CD
Image... to burn iso.

Note to all distro authors.
●

●
●

●

The grub manual describes command line
procedure, to create Boot CDs.
Many new computers do not have floppy.
All distros should have a “point and clicky”
option to create boot a CD, just like the one tocreate a boot floppy.
This code should be fairly easy to write.

use fetchmail with openssl!
●●●

If you connect to yourISP's imap via modem orthe ISP's cable line you
probably don't need anyencryption.
Modem conversations
are almost impossible todecypher.
If cable line is insecure,then ISP's host probably
is insecure too.

But if sometimes you connect toyour ISP via the open Internet....
●

●

●

●

then you can worry.
You don't want to haveyour password moving in
the clear over the openInternet!
Solution: use ssl withfetchmail!
ssl encrypts passwords
and prevents man-in-themiddle attacks!

Step1: run fetchmail -v●

check that you do not already have an ssl
command in .fetchmailrc●

fetchmail -v●

look for the first line that starts like this:●fetchmail: IMAP< * OK [CAPABILITY●●

check for the capability “STARTTLS” in thisline.
If not there, you are out of luck, your imap
server does not support ssl. Otherwise, goto
next step.

Step2: run fetchmail -v --ssl
●
●

●

●

look for errors that look like this:fetchmail: Server certificate verification error:
unable to get local issuer certificate
fetchmail: Server certificate verification error:
certificate not trusted
fetchmail: Server certificate verification error:
unable to verify the first certificate●no errors: done, add ssl to .fetchmailrc●

otherwise continue to step 3

Step3: run fetchmail -v --ssl●

look for a line that looks like this:●fetchmail: Issuer CommonName: Equifax Secure eBusiness CA-1●

Search the net for the CommonName.

step4:Find the place to download
the certificate.

We have found it!

We have found it! Check that it is
the true respectable site.

step5: Download the base64 versionof the certificate.

step 6:Find the place openssl keeps
all its certificates!
●

There will be a lot of files of type “.pem” there.

●

On my system it is: /etc/ssl/certs

●

●

●

logon to root, copy the certificate you
downloaded there, renaming the type to “.pem”.
Then do a “c_rehash” on the directory.
Example:# c_rehash /etc/ssl/certs

step7: fetchmail -v --ssl
again!
●●

This time check that the error messages weencountered before about: Server certificate
verification error: has gone away!
OK, good we have verified that ssl works!

Step8: edit .fetchmailrc●add “ssl” and “sslcertck” to the user line.

●

Mine looks like this:●set postmaster "pelliott"●

set bouncemail●

set no spambounce●

set properties ""●poll mail.io.com with proto IMAP●

user 'pelliott' there with password 'XXXXXX' is \'pelliott' here ssl sslcertckSuccess.
●●

From now onwhenever we use
fetchmail, it will use
openssl to encrypt
our password andprotect us from manin-the-middle attacks!
Done.

Do you know 3 or 4 MiscellaneousLinux Tricks?
●●Perhaps you cancompile your favorite
Linux Tricks into apresentation for ALG?
ALG can use someinternally generated
presentations.