Friday, October 3, 2008
Listing the Last Times a User Logged In
$ last | head
To output a list of last 10 logins of a user (say: mitesh):
$ last mitesh | head
Note: This command is very useful in determining who has
logged in the system at particular moment. Helpful in forensics ;)
Wednesday, October 1, 2008
Viewing PDF Files on Terminal
For example, you can install pdftohtml and elinks in Debian Linux with this command:
# apt-get install pdftohtml elinks
Users of the yum package can get the RPM version with this command:
# yum -y install pdftohtml elinks
Now you can view a PDF document with the following command. This particular command has one drawback. The output will not include frames (PDF files generally have a frame on the left that lets you jump to different pages).
$ pdftohtml -q -noframes -stdout document .pdf | elinks
If you want the left frame of page numbers, you can always use the following command instead:
$ pdftohtml -q document .pdf ; elinks document .html
You can write a script to save you all this typing each time you view a document. Use sudo or log in as root to create the /usr/local/bin/viewpdf script and enter the following code:
#!/bin/bash
pdftohtml -q $1 ~/temp.html
elinks ~/temp.html
#
#end of script
This code assumes it's OK to store the temporary HTML file in your home directory. You can use another location if you prefer. Now save your work and make the file executable:
$ sudo chmod +x /usr/local/bin/viewpdf
Create your own personalized boot splash backgrounds for GRUB
GRUB imposes a number of limitations on the image size and number of colors. It also doesn't let you move the menu. The menu appears in a rectangle near the top of the screen, with some text instructions below the menu. This makes it relatively easy to create a graphical background screen for the GRUB bootloader, because you can focus primarily on making the bottom one-third of the screen interesting. That is not to say you cannot use other areas of the screen, but you should be careful. For example, don't make it difficult to read the GRUB instructions by placing complex graphics behind the text.
Here are the rather strict requirements for the image:
- It must be sized at 640x480.
- It must be a .xpm format image file (gzip compression is optional).
- It must contain no more than 14 colors.Most people will cringe at the 14-color limit, but it is rather amazing what you can do with just 14 colors. Cartoon images are quite suitable for this narrow range of colors, and the narrow range of colors to represent the official Linux penguin (Tux) works fine.Find or create any image you want to try as a background for GRUB.
If you create an image yourself, it's best to create a 640x480 image and use as few colors as possible so that you don't lose the fidelity of the image when you later reduce it to 14 colors. Don't worry about using your graphics editor to limit yourself to 14 colors, however. It is possible to use the Gimp to reduce your image to use 14 colors, which can be a good way of fine-tuning the results you want.
Here is what you need:
A graphics editor, such as the Gimp, if you want to create or modify an image.
You must install ImageMagick if it is not already installed. Nearly all Linux distributions provide this on the install CD, and you can use your preferred package manager to install it.Suppose you have found or created the image myimage.png.
If you have ImageMagick installed, all you need to do to prepare the image is log in as root and issue these commands:
# convert myimage.png -colors 14 -resize 640x480 myimage.xpm
The convert command recognizes the extension png and knows what format it must convert the image from. It also recognizes the extension xpm and knows what format to convert the image to. The -colors 14 switch reduces the number of colors in the image to 14. If the image isn't already sized at 640x480, the switch -resize 640x480 will do that for you.
This is sample myimage.xpm
Changing RunLevel in Linux
The /etc/inittab file is the configuration file for /sbin/init. /etc/inittab identifies the processes that init starts, and it can be customized as desired. Few environment variables are set when a process is started by init.
The inittab lines have four colon-separated fields:
Let's look at the meaning of each.
(i). id The inittab id consists of one to four characters that identify the inittab line. The id must be unique.
(ii). runlevels The runlevels field contains one or more characters, usually numbers identifying the runlevels for which this process is started.
- 0 System Halt
- 1 Single user mode
- 2 Local multiuser without remote network (e.g., NFS)
- 3 Multiuser with network
- 4 Not used
- 5 Multiuser with network and xdm
- 6 System reboot
you want. And reboot the machine.
Wednesday, September 17, 2008
How to find out which process is listening upon a port?
$ lsof -i :6000
Alternatively, you can use following command:
$ fuser -v -n tcp 6000Find Command : A Swiss Army Knife
$ find / -perm -4000 -print
This command will find every file on the system that is suid. This means that when you run it you will be running it as an other user. For example, traceroute is a utility that needs to be run as root. To allow users to run it, systems administrators will set it suid root so it will be run as root even if a user starts it. This can be useful, but can also be a big security risk if the utility has a security hole in it.
Here is another interesting command:
$ find / -atime +10 -print
This command will find all the files accessed more than 10 days ago. Commands like this one can be useful to find old files that need to be backuped or erased.
Find links that point to nothing
$ find / -type l -print | perl -nle '-e || print';
List zero-length files
$ find . -empty -exec ls {} \;
Finding files which contains a given pattern:
$ find . -type f -exec grep "pattern" /dev/null {} \;
Finding large files (>= 100 MB) in your home directory:
$find ~/ -size +100M
Saturday, September 6, 2008
Creating Bootable USB from System Rescue CD
of System Rescue Linux, generally we follow steps given
in this link. After following instructions given, I have created
a small shell script to create bootable rescue USB.
The contents of the script create_rescue_usb.sh are:
$ cat create_rescue_usb.sh
#!/bin/bash
if [[ $# < 4 ]]
echo "Usage: $0 CDROM_Device CDROM_mount USB_Device USB_mount"
exit
fi
CDROM_Device=$1
CDROM_mount=$2
USB_Device=$3
USB_mount=$4
if [[ -e $CDROM_Device ]]
then
echo "$CDROM_Device is present."
else
echo "$0: $CDROM_Device is not present."
exit
fi
if [[ -e $USB_Device ]]
then
echo "$USB_Device is present."
else
echo "$0: $USB_Device is not present."
exit
fi
echo "Mounting..."
mount -o loop $CDROM_Device $CDROM_mount
if [ $? -ne 0 ]
then
echo "$0: cannot mount $CDROM_Device on $CDROM_mount."
exit
fi
mount $USB_Device $USB_mount
if [ $? -ne 0 ]
then
echo "$0: cannot mount $USB_Device on $USB_mount."
umount $CDROM_mount
exit
fi
echo "Copying Files..."
cp $CDROM_mount/syslinux/syslinux.cfg $USB_mount/
cp -r $CDROM_mount/isolinux/* $USB_mount/
cp -r $CDROM_mount/bootdisk/* $USB_mount/
cp $CDROM_mount/sysrcd.dat $USB_mount/
echo "Unmounting..."
umount $USB_mount
umount $CDROM_mount
echo "Making $USB_Device bootable..."
syslinux $USB_Device
sync
echo "$USB_Device is now ready to serve as bootable Linux USB."
#end of script
Now, you can make it executable.
$ chmod u+x create_rescue_usb.sh
Sample execution of above script (Please run as root user):
# ./create_rescue_cd.sh /path/of/system-rescue-cd.iso /mnt/cdrom /dev/sdb /mnt/usb
Where,
a. /path/of/system-rescue-cd.iso -- ISO image of the CD can be created as given in blog Creation of ISO Image of CD/DVD.
b. /mnt/cdrom -- directory, where you want to mount above ISO.
c. /dev/sdb -- or /dev/sdb1, your usb-stick device. May differ
according to your system configuration
d. /mnt/usb -- directory, where you want to mount usb-stick.
Now, your usb stick is ready to serve as bootable rescue USB.
You can test it. At the time of BIOS boot, please select USB device
as first boot device in BIOS. then save and reboot with the usb-stick
plugged in one USB port.
