Wednesday, December 15, 2010

How to on and off services for different run-levels?

This is very common and easy thing in Unix.

Many services run on OS if we want to check whether specific service is running or not we will use the command:

chkconfig --list which list the services running in all run-levels

to turn on\off the service we take the help of above command:

chkconfig --levels 35 service on\off.

Installing and removing packages in RHEL

Tutorial on installing software packages in RHEL.

RPM stands for Red Hat Package Manager.With RPM, you can install, upgrade and uninstall software on Linux, as well as keep track of already installed RPM packages on your system. This can be done because RPM keeps a database of all software that was installed with it.

Installing a package:

rpm –ivh software-2.3.4.rpm

Upgrading a package:

rpm -Uvh software-2.3.4.rpm

Removing a package:
rpm -e software-2.3.4
Note that when installing software, you have to type the name of the RPM package. But when removing software, you don't have to type the whole name of the package that contained the software. You don't have to type the .rpm extension when removing software. Probably you don't have to type the version number, either, so this would do exactly the same as the above:
rpm -e software

Checking the packages installed:
The RPM database contains a list of all installed RPM packages on your system. You can query this database to get info of the packages on your Linux system. To query a single package, you use the -q option. For example, to query a package whose name is "software":
# rpm -q software
After issuing this command, rpm either tells you the version of the package, or that the package isn't installed.
If you want a list of all packages installed on your system, you'll have to query all with -qa:

rpm -qa

check the installed package from all list we use grep command

rpm -qa| grep -i software

Source:

Tuesday, December 14, 2010

Cron Tab use

Crontab is mainly used to solve the some of the sysadmin jobs which can be done by running crontab entry.

A usual crontab entry can be checked by command:

crontab -l.

Entry for crontab:

crontab -e

A typical crontab entry:

30 08 10 06 * reboot
| | | | | |
| | | | | |_ command or script which we want to run.
| | | | |
| | | | |_____DAY OF THE WEEK(0-SUNDAY OR 7-SUNDAY)
| | | |_______MONTH
| | |__________ DATE
| |______________ HOURS
|________________MINUTES


To remove crontab entries we use the command crontab -r

To view your current crontab file, use the -l (for ``list'') option:

crontab -l
To delete your file, use:
crontab -d
If you are superuser, you can delete any user's crontab file with:
crontab -d username
where username is the user's login name.
To run a crontab, crond daemon should be running.

Monday, December 13, 2010

Port Blocking in Linux

For example i am blocking FTP port which is 21.
We can block ports using GUI and command line in Linux.

Command line:

Rejecting:

iptables -A INPUT -p tcp --dport 21 -j REJECT
Enabling:

iptables -A INPUT -p tcp --dport 21 -j ACCEPT

Before Enabling or blocking the port flush the iptables using the command

iptables --flush

Using GUI:
go to the system configuration and open firewall and uncheck the service which is running on the port which u want 2 disable.

For further Info See the below links:


Sunday, December 12, 2010

Super block

Reference for Super blocks in Unix:

http://www.angelfire.com/myband/binusoman/Unix.html#inodelist



Unix File System

The arrangement of disk blocks in Unix is as shown in the figure below.


Figure 1. Disk arrangement

The boot block contains the code to bootstrap the OS. The super block contains information about the entire disk. The I-node list a list of inodes, and the data blocks contains the actual data in the form of directories and files. Now let us dissect each block one by one.

The super block contains the following information, to keep track of the entire file system.

Size of the file system

Number of free blocks on the system

A list of free blocks

Index to next free block on the list

Size of the inode list

Number of free the inodes

A list of free inodes

Index to next free inode on the list

Lock fields for free block and free inode lists

Flag to indicate modification of super block

Size of the file system represents the actual no of blocks (used + unused) present in the file system.

The super block contains an array of free disk block numbers, one of which points to the next entry in the list. That entry in turn will be a data block, which contains an array of some other free blocks and a next pointer. When process requests for a block, it searches the free block list returns the available disk block from the array of free blocks in the super block. If the super block contains only one entry which is a pointer to a data block, which contains a list of other free blocks, all the entries from that block will be copied to the super block free list and returns that block to the process. Freeing of a block is reverse process of allocation. If the list of free blocks in super block has enough space for the entry then, this block address will be marked in the list. If the list is full, all the entries from the super block will be copied to the freed block and mark an entry for this block in the super block. Now the list in super block contains only this entry.

Index indexes to the next free disk block in the free disk block list.



Figure 2 Allocation of Data Blocks

The super block also contains an array to represent free inodes. The size of this array need not be the actual number of free inodes. During assignment of an inode to a new file, the kernel searches the free inode list. If one free inode is found, that one is returned. If the free inode list is empty, then it searches the inode list for free inodes. Each inode will contain a type field, which if 0, means the inode is free. It then fills the free inode list of super block as much as it can, with number of free inodes from inode list. It then returns one of these ones. It then remembers the highest inode number. Next time it has to scan the inode list for free ones, it starts from this remembered one. Hence it doesn't have to scan already scanned ones. This improves the efficiency. While freeing an inode, if the free list in super block has space enough, the freed one is put there. If there is not enough space, and the freed inode number is less than the remembered inode, then the remembered inode is updated with the freed inode. If the freed inode number is greater than the remembered one, then it doesn't have to update, because it will be scanned from the remembered node and the freed one will be covered later.

Index indexes to the next free inode in the free disk block list.



Figure 3 Assignment of new inodes

During all these allocations, in a multi-tasking environment, there is a chance of the inodes getting corrupted. Like if one process was trying to allocate an inode and was preempted by the scheduler, and a second process does the same for same inode, it will be a critical problem. Hence lock flags are introduced. While accessing inodes, that inode will be locked. One more flag to indicate that the super block has been modified, is present in the super block.

The I-node list (which server the purpose as FAT+ directory entries in DOS) is a list of inodes, which contains the following entries.

Owner

Type

Last modified time

Last accessed time

Last inode modified time

Access Permissions

No of links to the file

Size of the file

Data blocks owned

Owner indicates who owns the file(s) corresponding to this inode.

Type indicates whether inode represents a file, a directory, a FIFO, a character device or a block device. If the type value is 0 then the inode is free.

The times represent when, the file has been modified, when it was last accessed, or when the inode has been modified last. Whenever the contents of the file is changed, the "inode modified time" also changes. Moreover it changes when there are changes for the inode like permission change, creating a link etc.

Each file will be having nine access permissions for read, write and execute, for the owner, group and others in rwx rwx rwx format.

In Unix we can create links to some files or directories. So we need to have a count of how many links are pointing to the same inode, so that if we delete one of the links the actual data is not gone.

Size of the file represents the actual size of the file.

In Unix, we have a kind of indexing to access the actual data blocks that contains data. We have an array of which (in each inode) first ten elements indicate direct indexing. The next entry is single indirect, then comes double indirect and then triple indirect. By direct indexing we mean that, the value in the array represents the actual data block. If the file needs more than 10 blocks, it uses single indirect indexing, means this is an index to the a block which contains an array of disk block numbers which in turn represent the actual disk block. If all these are exhausted, then double indirect indexing is used and then triple indirect. For further clarifications, refer figure.



Figure 4. Inode DataBlock representation

The data blocks contain the actual data contained in the files or directories. In Unix, a directory is a special file. A directory file contains names of the subdirectories and files present in that directory and its corresponding inode number.

Reference:

The Design of the Unix Operating System By Maurice J. Bach

Advanced Programming in the Unix Environment By W. Richard Stevens

Sunday, December 5, 2010

Adding a printer in Linux

In Linux to add a Network printer we use CUPS which need to be installed to add a printer.

To add a printer from command line we use the command

lpadmin -p LaserJet -E -v socket://ip address


to make the above printer default we use d option with lpadmin

lpadmin -d printername

To add the printer using GUI

we type the command system-config-printer which opens a GUI for the user which is very easy to configure.

Thursday, October 21, 2010

Linux files and permissions

Linux files are setup so access to them is controlled. There are three types of access:
  1. read
  2. write
  3. execute
Each file belongs to a specific user and group. Access to the files is controlled by user, group, and what is called other. The term, other, is used to refer to someone who is not the user (owner) of the file, nor is the person a member of the group the file belongs to. When talking about setting permissions for "other" users to use, it is commonly referred to as setting the world execute, read, or write bit since anyone in the world will be able to perform the operation if the permission is set in the other category.

File names and permission characters

File names can be up to 256 characters long with "-", "_", and "." characters along with letters and numbers.
When a long file listing is done, there are 10 characters that are shown on the left that indicate type and permissions of the file. File permissions are shown according to the following syntax example: drwerwerwe
There are a total of 10 characters in this example, as in all Linux files. The first character indicates the type of file, and the next three indicate read, write, and execute permission for each of the three user types, user, group and other. Since there are three types of permission for three users, there are a total of nine permission bits. The table below shows the syntax:
12345678910
FileUser PermissionsGroup PermissionsOther Permissions
TypeReadWriteExecuteReadWriteExecuteReadWriteExecute
drwerwerwe
  • Character 1 is the type of file: - is ordinary, d is directory, l is link.
  • Characters 2-4 show owner permissions. Character 2 indicates read permission, character 3 indicates write permission, and character 4 indicates execute permission.
  • Characters 5-7 show group permissions. Character 5=read, 6=write, 7=execute
  • Characters 8-10 show permissions for all other users. Character 8=read, 9=write, 10=execute
There are 5 possible characters in the permission fields. They are:
  • r = read - This is only found in the read field.
  • w = write - This is only found in the write field.
  • x = execute - This is only found in the execute field.
  • s = setuid - This is only found in the execute field.
  • If there is a "-" in a particular location, there is no permission. This may be found in any field whether read, write, or execute field.

Examples

Type "ls -l" and a listing like the following is displayed:
total 10





drwxrwxrwx4georgeteam1122Dec 12 18:02Projects
-rw-rw-rw-1george team11873Aug 23 08:34test
-rw-rw-rw-1georgeteam11234Sep 12 11:13 datafile
Which means the following:
Type and# ofFiles'sFile'sSize in Date of lastFilename
Permission fieldLinksOwnerGroupBytesmodification
|||||||
drwxrwxrwx 4georgeteam1122Dec 12 18:02Projects
The fields are as follows:
  1. Type field: The first character in the field indicates a file type of one of the following:
    • d = directory
    • l = symbolic link
    • s = socket
    • p = named pipe
    • - = regular file
    • c= character (unbuffered) device file special
    • b=block (buffered) device file special
  2. Permissions are explained above.
  3. Links: The number of directory entries that refer to the file. In our example, there are four.
  4. The file's owner in our example is George.
  5. The group the file belongs to. In our example, the group is team1.
  6. The size of the file in bytes
  7. The last modification date. If the file is recent, the date and time is shown. If the file is not in the current year, the year is shown rather than time.
  8. The name of the file.

Set User Identification Attribute

The file permissions bits include an execute permission bit for file owner, group and other. When the execute bit for the owner is set to "s" the set user ID bit is set. This causes any persons or processes that run the file to have access to system resources as though they are the owner of the file. When the execute bit for the group is set to "s", the set group ID bit is set and the user running the program is given access based on access permission for the group the file belongs to. The following command:
chmod +s myfile
sets the user ID bit on the file "myfile". The command:
chmod g+s myfile
sets the group ID bit on the file "myfile".
The listing below shows a listing of two files that have the group or user ID bit set.
-rws--x--x   1 root    root    14024 Sep  9 1999 chfn
-rwxr-sr-x   1 root   mail    12072 Aug 16 1999 lockfile
The files chfn and lockfile are located in the directory "/usr/bin". The "s" takes the place of the normal location of the execute bit in the file listings above. This special permission mode has no meaning unless the file has execute permission set for either the group or other as well. This means that in the case of the lockfile, if the other users (world execute) bit is not set with permission to execute, then the user ID bit set would be meaningless since only that same group could run the program anyhow. In both files, everyone can execute the binary. The first program, when run is executed as though the program is the root user. The second program is run as though the group "mail" is the user's group.

For system security reasons it is not a good idea to set many program's set user or group ID bits any more than necessary, since this can allow an unauthorized user privileges in sensitive system areas. If the program has a flaw that allows the user to break out of the intended use of the program, then the system can be compromised.

Directory Permissions

There are two special bits in the permissions field of directories. They are:
  • s - Set group ID
  • t - Save text attribute (sticky bit) - The user may delete or modify only those files in the directory that they own or have write permission for.

Save text attribute

The /tmp directory is typically world-writable and looks like this in a listing:
drwxrwxrwt   13 root     root         4096 Apr 15 08:05 tmp
Everyone can read, write, and access the directory. The "t'' indicates that only the user (and root, of course) that created a file in this directory can delete that file.

To set the sticky bit in a directory, do the following:
chmod +t data
This option should be used carefully. A possible alternative to this is
  1. Create a directory in the user's home directory to which he or she can write temporary files.
  2. Set the TMPDIR environment variable using each user's login script.
  3. Programs using the tempnam(3) function will look for the TMPDIR variable and use it, instead of writing to the /tmp directory.

Directory Set Group ID

If the setgid bit on a directory entry is set, files in that directory will have the group ownership as the directory, instead of than the group of the user that created the file.

This attribute is helpful when several users need access to certain files. If the users work in a directory with the setgid attribute set then any files created in the directory by any of the users will have the permission of the group. For example, the administrator can create a group called spcprj and add the users Kathy and Mark to the group spcprj. The directory spcprjdir can be created with the set GID bit set and Kathy and Mark although in different primary groups can work in the directory and have full access to all files in that directory, but still not be able to access files in each other's primary group.

The following command will set the GID bit on a directory:
chmod g+s spcprjdir
The directory listing of the directory "spcprjdir":
drwxrwsr-x 2 kathy spcprj 1674 Sep 17 1999 spcprjdir
The "s'' in place of the execute bit in the group permissions causes all files written to the directory "spcprjdir" to belong to the group "spcprj" .

Examples

Below are examples of making changes to permissions:

chmod u+x myfile
Gives the user execute permission on myfile.
chmod +x myfileGives everyone execute permission on myfile.
chmod ugo+x myfileSame as the above command, but specifically specifies user, group and other.
chmod 400 myfileGives the user read permission, and removes all other permission. These permissions are specified in octal, the first char is for the user, second for the group and the third is for other. The high bit (4) is for read access, the middle bit (2) os for write access, and the low bit (1) is for execute access.
chmod 764 myfileGives user full access, group read and write access, and other read access.
chmod 751 myfileGives user full access, group read and execute permission, and other, execute permission.
chmod +s myfileSet the setuid bit.
chmod go=rx myfileRemove read and execute permissions for the group and other.

Below are examples of making changes to owner and group:
chown mark test1Changes the owner of the file test1 to the user Mark.
chgrp mark test1Changes the file test1 to belong to the group "mark".
Note: Linux files were displayed with a default tab value of 8 in older Linux versions. That means that file names longer than 8 may not be displayed fully if you are using an old Linux distribution. There is an option associated with the ls command that solves this problem. It is "-T". Ex: "ls al -T 30" to make the tab length 30.

Umask Settings

The umask command is used to set and determine the default file creation permissions on the system. It is the octal complement of the desired file mode for the specific file type. Default permissions are:
  • 777 - Executable files
  • 666 - Text files
These defaults are set allowing all users to execute an executable file and not to execute a text file. The defaults allow all users can read and write the file.

The permission for the creation of new executable files is calculated by subtracting the umask value from the default permission value for the file type being created. An example for a text file is shown below with a umask value of 022:
666 Default Permission for text file
       -022 Minus the umask value
      -----
        644 Allowed Permissions
Therefore the umask value is an expression of the permissions the user, group and world will not have as a default with regard to reading, writing, or executing the file. The umask value here means the group the file belongs to and users other than the owner will not be able to write to the file. In this case, when a new text file is created it will have a file permission value of 644, which means the owner can read and write the file, but members of the group the file belongs to, and all others can only read the file. A long directory listing of a file with these permissions set is shown below.
-rw-r--r--   1 root     workgrp          14233 Apr  24 10:32 textfile.txt
A example command to set the umask is:
umask 022
The most common umask setting is 022. The /etc/profile script is where the umask command is usually set for all users.

Red Hat Linux has a user and group ID creation scheme where there is a group for each user and only that user belongs to that group. If you use this scheme consistently you only need to use 002 for your umask value with normal users.

Linux command reference

CommandDescription
apropos whatisShow commands pertinent to string. See also threadsafe
man -t man | ps2pdf - > man.pdfmake a pdf of a manual page
 which commandShow full path name of command
 time commandSee how long a command takes
time catStart stopwatch. Ctrl-d to stop. See also sw
dir navigation
cd -Go to previous directory
cdGo to $HOME directory
 (cd dir && command)Go to dir, execute command and return to current dir
pushd .Put current dir on stack so you can popd back to it
alias l='ls -l --color=auto'quick dir listing
ls -lrtList files by date. See also newest and find_mm_yyyy
ls /usr/bin | pr -T9 -W$COLUMNSPrint in 9 columns to width of terminal
 find -name '*.[ch]' | xargs grep -E 'expr'Search 'expr' in this dir and below. See also findrepo
 find -type f -print0 | xargs -r0 grep -F 'example'Search all regular files for 'example' in this dir and below
 find -maxdepth 1 -type f | xargs grep -F 'example'Search all regular files for 'example' in this dir
 find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; doneProcess each item with multiple commands (in while loop)
find -type f ! -perm -444Find files not readable by all (useful for web site)
find -type d ! -perm -111Find dirs not accessible by all (useful for web site)
locate -r 'file[^/]*\.txt'Search cached index for names. This re is like glob *file*.txt
look referenceQuickly search (sorted) dictionary for prefix
grep --color reference /usr/share/dict/wordsHighlight occurances of regular expression in dictionary
archives and compression
 gpg -c fileEncrypt file
 gpg file.gpgDecrypt file
 tar -c dir/ | bzip2 > dir.tar.bz2Make compressed archive of dir/
 bzip2 -dc dir.tar.bz2 | tar -xExtract archive (use gzip instead of bzip2 for tar.gz files)
 tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'Make encrypted archive of dir/ on remote machine
 find dir/ -name '*.txt' | tar -c --files-from=- | bzip2 > dir_txt.tar.bz2Make archive of subset of dir/ and below
 find dir/ -name '*.txt' | xargs cp -a --target-directory=dir_txt/ --parentsMake copy of subset of dir/ and below
 ( tar -c /dir/to/copy ) | ( cd /where/to/ && tar -x -p )Copy (with permissions) copy/ dir to /where/to/ dir
 ( cd /dir/to/copy && tar -c . ) | ( cd /where/to/ && tar -x -p )Copy (with permissions) contents of copy/ dir to /where/to/
 ( tar -c /dir/to/copy ) | ssh -C user@remote 'cd /where/to/ && tar -x -p' Copy (with permissions) copy/ dir to remote:/where/to/ dir
 dd bs=1M if=/dev/sda | gzip | ssh user@remote 'dd of=sda.gz'Backup harddisk to remote machine
rsync (Network efficient file copier: Use the --dry-run option for testing)
 rsync -P rsync://rsync.server.com/path/to/file fileOnly get diffs. Do multiple times for troublesome downloads
 rsync --bwlimit=1000 fromfile tofileLocally copy with rate limit. It's like nice for I/O
 rsync -az -e ssh --delete ~/public_html/ remote.com:'~/public_html'Mirror web site (using compression and encryption)
 rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh . remote:/dir/Synchronize current directory with remote one
ssh (Secure SHell)
 ssh $USER@$HOST commandRun command on $HOST as $USER (default command=shell)
ssh -f -Y $USER@$HOSTNAME xeyesRun GUI command on $HOSTNAME as $USER
 scp -p -r $USER@$HOST: file dir/Copy with permissions to $USER's home directory on $HOST
 ssh -g -L 8080:localhost:80 root@$HOSTForward connections to $HOSTNAME:8080 out to $HOST:80
 ssh -R 1434:imap:143 root@$HOSTForward connections from $HOST:1434 in to imap:143
 ssh-copy-id $USER@$HOST Install $USER's public key on $HOST for password-less log in
wget (multi purpose download tool)
(cd dir/ && wget -nd -pHEKk http://www.pixelbeat.org/cmdline.html)Store local browsable version of a page to the current dir
 wget -c http://www.example.com/large.fileContinue downloading a partially downloaded file
 wget -r -nd -np -l1 -A '*.jpg' http://www.example.com/dir/Download a set of files to the current directory
 wget ftp://remote/file[1-9].iso/FTP supports globbing directly
wget -q -O- http://www.pixelbeat.org/timeline.html | grep 'a href' | headProcess output directly
 echo 'wget url' | at 01:00Download url at 1AM to current dir
 wget --limit-rate=20k urlDo a low priority download (limit to 20KB/s in this case)
 wget -nv --spider --force-html -i bookmarks.htmlCheck links in a file
 wget --mirror http://www.example.com/Efficiently update a local copy of a site (handy from cron)
networking (Note ifconfig, route, mii-tool, nslookup commands are obsolete)
 ethtool eth0Show status of ethernet interface eth0
 ethtool --change eth0 autoneg off speed 100 duplex fullManually set ethernet interface speed
 iwconfig eth1Show status of wireless interface eth1
 iwconfig eth1 rate 1Mb/s fixedManually set wireless interface speed
iwlist scanList wireless networks in range
ip link showList network interfaces
 ip link set dev eth0 name wanRename interface eth0 to wan
 ip link set dev eth0 upBring interface eth0 up (or down)
ip addr showList addresses for interfaces
 ip addr add 1.2.3.4/24 brd + dev eth0Add (or del) ip and mask (255.255.255.0)
ip route showList routing table
 ip route add default via 1.2.3.254Set default gateway to 1.2.3.254
host pixelbeat.orgLookup DNS ip address for name or vice versa
hostname -iLookup local ip address (equivalent to host `hostname`)
whois pixelbeat.orgLookup whois info for hostname or ip address
netstat -tuplList internet services on a system
netstat -tupList active connections to/from system
windows networking (Note samba is the package that provides all this windows specific networking support)
smbtreeFind windows machines. See also findsmb
 nmblookup -A 1.2.3.4Find the windows (netbios) name associated with ip address
 smbclient -L windows_boxList shares on windows machine or samba server
 mount -t smbfs -o fmask=666,guest //windows_box/share /mnt/shareMount a windows share
 echo 'message' | smbclient -M windows_boxSend popup to windows machine (off by default in XP sp2)
text manipulation (Note sed uses stdin and stdout. Newer versions support inplace editing with the -i option)
 sed 's/string1/string2/g'Replace string1 with string2
 sed 's/\(.*\)1/\12/g'Modify anystring1 to anystring2
 sed '/ *#/d; /^ *$/d'Remove comments and blank lines
 sed ':a; /\\$/N; s/\\\n//; ta'Concatenate lines with trailing \
 sed 's/[ \t]*$//'Remove trailing spaces from lines
 sed 's/\([`"$\]\)/\\\1/g'Escape shell metacharacters active within double quotes
seq 10 | sed "s/^/      /; s/ *\(.\{7,\}\)/\1/"Right align numbers
 sed -n '1000{p;q}'Print 1000th line
 sed -n '10,20p;20q'Print lines 10 to 20
 sed -n 's/.*<title>\(.*\)<\/title>.*/\1/ip;T;q'Extract title from HTML web page
 sed -i 42d ~/.ssh/known_hostsDelete a particular line
 sort -t. -k1,1n -k2,2n -k3,3n -k4,4nSort IPV4 ip addresses
echo 'Test' | tr '[:lower:]' '[:upper:]'Case conversion
tr -dc '[:print:]' < /dev/urandomFilter non printable characters
tr -s '[:blank:]' '\t' </proc/diskstats | cut -f4cut fields separated by blanks
history | wc -lCount lines
set operations (Note you can export LANG=C for speed. Also these assume no duplicate lines within a file)
 sort file1 file2 | uniqUnion of unsorted files
 sort file1 file2 | uniq -dIntersection of unsorted files
 sort file1 file1 file2 | uniq -uDifference of unsorted files
 sort file1 file2 | uniq -uSymmetric Difference of unsorted files
 join -t'\0' -a1 -a2 file1 file2Union of sorted files
 join -t'\0' file1 file2Intersection of sorted files
 join -t'\0' -v2 file1 file2Difference of sorted files
 join -t'\0' -v1 -v2 file1 file2Symmetric Difference of sorted files
math
echo '(1 + sqrt(5))/2' | bc -lQuick math (Calculate φ). See also bc
echo 'pad=20; min=64; (100*10^6)/((pad+min)*8)' | bcMore complex (int) e.g. This shows max FastE packet rate
echo 'pad=20; min=64; print (100E6)/((pad+min)*8)' | pythonPython handles scientific notation
echo 'pad=20; plot [64:1518] (100*10**6)/((pad+x)*8)' | gnuplot -persistPlot FastE packet rate vs packet size
echo 'obase=16; ibase=10; 64206' | bcBase conversion (decimal to hexadecimal)
echo $((0x2dec))Base conversion (hex to dec) ((shell arithmetic expansion))
units -t '100m/9.58s' 'miles/hour'Unit conversion (metric to imperial)
units -t '500GB' 'GiB'Unit conversion (SI to IEC prefixes)
units -t '1 googol'Definition lookup
seq 100 | (tr '\n' +; echo 0) | bcAdd a column of numbers. See also add and funcpy
calendar
cal -3Display a calendar
cal 9 1752Display a calendar for a particular month year
date -d friWhat date is it this friday. See also day
[ $(date -d "tomorrow" +%d) = "01" ] || exitexit a script unless it's the last day of the month
date --date='25 Dec' +%AWhat day does xmas fall on, this year
date --date='@2147483647'Convert seconds since the epoch (1970-01-01 UTC) to date
TZ='America/Los_Angeles' dateWhat time is it on west coast of US (use tzselect to find TZ)
date --date='TZ="America/Los_Angeles" 09:00 next Fri'What's the local time for 9AM next Friday on west coast US
locales
printf "%'d\n" 1234Print number with thousands grouping appropriate to locale
BLOCK_SIZE=\'1 ls -lUse locale thousands grouping in ls. See also l
echo "I live in `locale territory`"Extract info from locale database
LANG=en_IE.utf8 locale int_prefixLookup locale info for specific country. See also ccodes
locale | cut -d= -f1 | xargs locale -kc | lessList fields available in locale database
recode (Obsoletes iconv, dos2unix, unix2dos)
recode -l | lessShow available conversions (aliases on each line)
 recode windows-1252.. file_to_change.txtWindows "ansi" to local charset (auto does CRLF conversion)
 recode utf-8/CRLF.. file_to_change.txtWindows utf8 to local charset
 recode iso-8859-15..utf8 file_to_change.txtLatin9 (western europe) to utf8
 recode ../b64 < file.txt > file.b64Base64 encode
 recode /qp.. < file.qp > file.txtQuoted printable decode
 recode ..HTML < file.txt > file.htmlText to HTML
recode -lf windows-1252 | grep euroLookup table of characters
echo -n 0x80 | recode latin-9/x1..dumpShow what a code represents in latin-9 charmap
echo -n 0x20AC | recode ucs-2/x2..latin-9/xShow latin-9 encoding
echo -n 0x20AC | recode ucs-2/x2..utf-8/xShow utf-8 encoding
CDs
 gzip < /dev/cdrom > cdrom.iso.gzSave copy of data cdrom
 mkisofs -V LABEL -r dir | gzip > cdrom.iso.gzCreate cdrom image from contents of dir
 mount -o loop cdrom.iso /mnt/dirMount the cdrom image at /mnt/dir (read only)
 cdrecord -v dev=/dev/cdrom blank=fastClear a CDRW
 gzip -dc cdrom.iso.gz | cdrecord -v dev=/dev/cdrom -Burn cdrom image (use dev=ATAPI -scanbus to confirm dev)
 cdparanoia -BRip audio tracks from CD to wav files in current dir
 cdrecord -v dev=/dev/cdrom -audio -pad *.wavMake audio CD from all wavs in current dir (see also cdrdao)
 oggenc --tracknum='track' track.cdda.wav -o 'track.ogg'Make ogg file from wav file
disk space (See also FSlint)
ls -lSrShow files by size, biggest last
du -s * | sort -k1,1rn | headShow top disk users in current dir. See also dutop
du -hs /home/* | sort -k1,1hSort paths by easy to interpret disk usage
df -hShow free space on mounted filesystems
df -iShow free inodes on mounted filesystems
fdisk -lShow disks partitions sizes and types (run as root)
rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1nList all packages by installed size (Bytes) on rpm distros
dpkg-query -W -f='${Installed-Size;10}\t${Package}\n' | sort -k1,1nList all packages by installed size (KBytes) on deb distros
dd bs=1 seek=2TB if=/dev/null of=ext3.testCreate a large test file (taking no space). See also truncate
> filetruncate data of file or create an empty file
monitoring/debugging
tail -f /var/log/messagesMonitor messages in a log file
strace -c ls >/dev/nullSummarise/profile system calls made by command
strace -f -e open ls >/dev/nullList system calls made by command
ltrace -f -e getenv ls >/dev/nullList library calls made by command
lsof -p $$List paths that process id has open
lsof ~List processes that have specified path open
tcpdump not port 22Show network traffic except ssh. See also tcpdump_not_me
ps -e -o pid,args --forestList processes in a hierarchy
ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'List processes by % cpu usage
ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNSList processes by mem (KB) usage. See also ps_mem.py
ps -C firefox-bin -L -o pid,tid,pcpu,stateList all threads for a particular process
ps -p 1,2List info for particular process IDs
last rebootShow system reboot history
free -mShow amount of (remaining) RAM (-m displays in MB)
watch -n.1 'cat /proc/interrupts'Watch changeable data continuously
udevadm monitorMonitor udev events to help configure rules
system information (see also sysinfo) ('#' means root access is required)
uname -aShow kernel version and system architecture
head -n1 /etc/issueShow name and version of distribution
cat /proc/partitionsShow all partitions registered on the system
grep MemTotal /proc/meminfoShow RAM total seen by the system
grep "model name" /proc/cpuinfoShow CPU(s) info
lspci -tvShow PCI info
lsusb -tvShow USB info
mount | column -tList mounted filesystems on the system (and align output)
grep -F capacity: /proc/acpi/battery/BAT0/infoShow state of cells in laptop battery
#dmidecode -q | lessDisplay SMBIOS/DMI information
#smartctl -A /dev/sda | grep Power_On_HoursHow long has this disk (system) been powered on in total
#hdparm -i /dev/sdaShow info about disk sda
#hdparm -tT /dev/sdaDo a read speed test on disk sda
#badblocks -s /dev/sdaTest for unreadable blocks on disk sda
interactive (see also linux keyboard shortcuts)
readlineLine editor used by bash, python, bc, gnuplot, ...
screenVirtual terminals with detach capability, ...
mcPowerful file manager that can browse rpm, tar, ftp, ssh, ...
gnuplotInteractive/scriptable graphing
linksWeb browser
xdg-open .open a file or url with the registered desktop application

Converting ext2 to ext3

Log in as root
Make sure /etc/fstab has /dev/hda10 mounted to /test as ext2, read write
umount /dev/hda10
If you can't unmount it, then remount it read only (mount -o remount,ro /dev/hda10)
tune2fs -j /dev/hda10
Edit /etc/fstab, and for /dev/hda10, change ext2 to ext3
mount /dev/hda10
/sbin/shutdown -h now
mount | grep /dev/hda10
If it's not shown as ext3, reboot, if still not, troubleshoot
Otherwise, you're done.

Lvm reducing and extending

Resizing ,extending adding a disk in lvm
 lvextend -L12G /dev/myvg/homevol (or)  lvextend -L+1G /dev/myvg/homevol
I am adding a new hard drive to increase the size of my logical volume "home1" in logical volume group "home" with LVM2 under Ubuntu 6.10.1 LTS.  The instructions in the LVM Howto are pretty straightforward; the only part that spooks me is the resizing of the ext3 filesystem.  This is what I have done so far:
(installed new hardware & partitioned with fdisk; shows up as /dev/sdb1)
(backed up the existing logical volume in case something bad happens)

# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created 

# vgextend home /dev/sdb1
  Volume group "home" successfully extended 

# lvextend -l 357700 /dev/home/home1
  Extending logical volume home1 to 1.36 TB
  Logical volume home1 successfully resized

From what I googled, the next steps are a bit more involved for LVM2 than the Howto lets on.  Does the following look o.k. to the experts:

' use a root login, not su or sudo, because /home will be unmounted

# umount /dev/home/home1 ' the next steps should be done off-line

# e2fsck -C 0 -v dev/home/home1 ' make sure the file system is ok

# tune2fs -O ^has_journal /dev/home/home1 'disable journaling on the logical volume

# resize2fs /dev/home/home1 'do the resizing

# e2fsck -C 0 -v dev/home/home1 ' make sure the file system is still ok

# tune2fs -O has_journal /dev/home/home1 're-enable journaling
# e2fsck -C 0 -v dev/home/home1 ' yes, I am a first-timer and somewhat paranoid

# mount /dev/home/home1 /home ' back on-line
 -------------------------------
VGREDUCE

vgreduce <volume group> <device name>
mount -o remount,rw /

LV REMOVE

lvremove /dev/volumegroup/logical volume

Creating LVM

How to create or add new Linux EXT3 file system in Logical Volume Manager (LVM) with a new disk partition, which involves creating new logical volume (LV)?
How to create new Linux EXT3 file system in LVM with new hard disk by creating new logical volume?

Assuming a new IDE hard disk hooked up to Secondary-Slave IDE bus for the new logical volume / file system. Hence, the Linux device code will be /dev/hdd for this IDE hard disk, according to LANANA Linux Device List or the offline copy in /usr/src/linux-2.x/Documentation/devices.txt
  1. Using the whole secondary-slave IDE hard disk for existing LVM volume group (called vg0 in my case) by creating the physical volume (PV):
    pvcreate /dev/hdd

    A similar message of this will be shown upon successful execution of pvcreate command:

    pvcreate — physical volume “/dev/hdd” successfully created
     
  2. Adding the new physical volume (PV) to volume group vg0, i.e. to extend the existing volume group size with new physical volume:
    vgextend vg0 /dev/hdd

    If no errors encounter while executing vgextend, a similar message of this will be seen:

    vgextend — INFO: maximum logical volume size is 1023.97 Gigabyte
    vgextend — doing automatic backup of volume group “vg0″
    vgextend — volume group “vg0″ successfully extended

     
  3. Create the new logical volume (LV) at 400MB (not fully utilize the whole IDE hard disk) to host the new EXT3 file system in question:
    lvcreate -L 400M -n lvol1 vg0

    Suppose lvcreate completed successfully, this similar message will be seen:

    lvcreate — doing automatic backup of “vg0″
    lvcreate — logical volume “/dev/vg0/lvol1″ successfully created

     
  4. Now, create the new EXT3 file system on the new logical volume (LV) with 1% file system reserved block count:
    mkfs -t ext3 -m 1 -v /dev/vg0/lvol1

    Once the new EXT3 file system creation completed, you can examine the file system by executing

    tune2fs -l /dev/vg0/lvol1
     
  5. Create a mount point directory for the new EXT3 file system:
    mkdir /mnt/newfs
     
  6. It’s now ready to mount the new EXT3 file system:
    mount -t ext3 /dev/vg0/lvol1 /mnt/newfs

    To confirm that the new EXT3 file system has been mounted successful, type df -h                           

 Courtesy: http://www.walkernews.net/2007/07/11/create-new-linux-ext3-file-system-in-lvm/

Formatting a hard drive

How to Format a Hard Drive in Linux

Instructions

    Preparation

  1. 1
    When you format a computer hard drive, you will lose everything that is on the drive. Therefore, it is very important to back up anything you might later want.
  2. 2
    To format a secondary drive, you will need root access. All commands used in this tutorial are run from a command line (terminal) as root. You may also want to make note of the size (total capacity in MB) of the drive.
  3. 3
    Linux refers to hard drives as either "hdx" or "sdx," where x is a letter, starting with a, which represents the order in which the drive was added to or detected by the computer. The "hd" prefix is used for IDE and PATA (formerly just ATA), and the "sd" prefix is used for SCSI, SATA and USB drives. Usually, a number is also put at the end of "hdx" or "sdx" to denote different partitions on the same physical drive, but for the purpose of formatting, you only need to know which letter the drive you want to format is.
  4. 4
    The examples given in this how-to are for a computer with two IDE hard drives attached as a master and slave. In this, the drives are "hda" and "hdb." You will need to determine the letter of the drive you want to format for your own setup. We will be formatting the drive hdb. For all examples, replace "hdb" with whatever your drive designation is.
  5. 5
    You can see all the drives attached to your system by typing the command "ls /dev/hd*" or "ls /dev/sd*", depending on which type (IDE, SATA and so forth) the drives are. On the example system, the result of this command looks like "/dev/hda /dev/hda1 /dev/hda2 /dev/hdb /dev/hdb1". The operating system is installed on hda, which has two partitions (hda1 and hda2), and there is one partition on hdb and hdb1.
  6. Using fdisk

  7. 1
    First, you will use the fdisk command to erase any old partitions on the drive and create a new one. Any changes you make using fdisk are only made permanent if you then issue the "w" command before quitting, so feel free to play around a little if you like. If at any time you find yourself stuck, you can quit the program without saving changes by holding the "Ctrl" key and pressing "c."
  8. 2
    At the command prompt, type "fdisk /dev/hdb", replacing the "hdb" with the letters for your drive. Upon opening, fdisk may give you a couple of warnings, all of which can be ignored. It then gives you a prompt that looks like this: Command (m for help):
  9. 3
    Enter "p" to see the partition table of the drive. The first line of output from the "p" command will also tell you the size of the drive. This is a good way to double-check that you are working with the correct drive.
  10. 4
    If there are any partitions already on the drive, they will be listed as the last lines of the "p" command. On our example, this looks like "/dev/hdb1", followed by some information about the partition's size and filesystem.
  11. 5
    To delete any existing partitions, press "d" and then "Enter." It will ask you which partition number you wish to delete. The number of the partition is the number that follows hdb, so on our example system, we enter 1. If there are multiple partitions, repeat the "d" command for each one. You can always view the partition table again with the "p" command.
  12. 6
    Once you have deleted all existing partitions on the drive, you are ready to make a new one. Type "n" and hit "Enter." Then press "p" to create a primary partition. It asks you for a partition number; enter "1." Now you are asked which cylinder the partition should start at. The beginning of the drive is the default, so just hit "Enter." Then, you are asked for the last cylinder. The end of the drive is the default, so you can just press "Enter" again.
  13. 7
    Now you are back at fdisk's command prompt. Use the "p" command to check the partition table. You should now see your new partition at the bottom of the output. In the example, it lists as "/dev/hdb1."
  14. 8
    You now need to set the filesystem type for your new partition with the "t" command. You are asked for the Hex code of the filesystem you wish to use. We will use the standard Linux ext2 filesystem, which is "83." If you are doing something special and know of a particular filesystem that you need to use, you can press "L" to see all the codes, which are one or two characters made up of the numbers 0 to 9 and the letters a to f.
  15. 9
    Now just issue the "w" command to write your new partition table and exit fdisk.
  16. Creating the New Filesystem With mkfs

  17. 1
    Now you need to create the filesystem on the drive. This is done with the "mkfs" command.
  18. 2
    At the command prompt, enter "mkfs -t ext2 /dev/hdb1", while remembering to change the hdb1 to whatever the letters are for the partition you just created.
  19. 3
    If you are using a different filesystem than ext2, you will have to specify that where "ext2" is in the above command.
  20. Finalizing the Format With fsck

  21. 1
    All that is left is to run a check on the drive and enter it into your fstab so that the drive mounts each time you start your computer. This can be done with a single fsck command.
  22. 2
    At the command prompt, type "fsck -f -y /dev/hdb1", again replacing hdb1 with the letters and number for your partition.
  23. 3
    After fsck runs, your new drive is formatted. Restart your system before using it.
  24. 4
    If you reformatted your system drive, you will now need to boot off an installation disk to install an operating system.


Read more: How to Format a Hard Drive in Linux | eHow.com http://www.ehow.com/how_1000631_hard-drive-linux.html#ixzz133zH1sSH

Understanding mounts

What are mounting points? Basically, mounting points are where you mount your file systems or devices. Example: If I wanted to mount my CD-ROM/DVD, I would login as root or super-user and type: mount /dev/sdx /mnt/cdrom”x”, where “x” is the device number. And /mnt/cdrom is a directory where the files will be exported.
Look at the following table for different mounted partition.

MountpointDefinitionWhat it holds
/rootYou can boot the system here or create a separate /boot partition
SwapSwapTemporary virtual memory
/usrUserStore binaries, compiler libraries and user applications
/varVariableThis partition must be large. It keeps the log, spools etc.
/homeHomeHome users' directory
/tmpTempThe system keeps temp files
/mntMountTemporary mounting points
Table 1.2
In Linux, mount points can be mounted permanently or temporarily. For security purposes, you should only Mount your devices under root's privileges.

Swap Partition: Use or Not to Use

In many cases you will appreciate the swap partition and other times the system won't need it at all. This only depends on the type of system you are setting up. Let say that you have plenty of RAM, say, 128MB+ and you are the sole user of the machine, you may not need it then. On the contrary if you are setting up a server machine then you definitely will need swap. Swap is where the virtual memory takes place. Check the system logs frequently to see if you ran out of virtual memory at any point. Even if you know that you don't need swap, I recommend you create one about double the size of your physical ram.


Courtesy: http://www.comptechdoc.org/os/linux/manual1/mountpoints.html