Linux Tutorial (System Administration Workshop at FreeDel 2006 JNU, New Delhi, India)
Date:20060823
Author: Tirveni Yadav
CHAPTER 1 Getting Started
$date -- print or set the system date and time $clear (or <CTRL-l>) -- clear the terminal screen $cal (month year) -- the calendar $who (am i) -- show who is logged on $ls -(l,a,t,F,r,R,S) -- list directory contents $wc -(w|l|c) -- print the number of newlines, words, and bytes in files $echo -- display a line of text $exit (or <CTRL-d>) -- logout from console $uname -(a|s,n,r,v,m,o) -- print system information $man -(k,f) -- an interface to the on-line reference manuals $tty -- print the file name of the terminal connected to standard input $apropos -- search the manual page names and descriptions $tar -cvzf backup.tar.gz directory -[A,r,u,x] -- The GNU version of the tar archiving utility. $`CMD` --help -- Almost all the Linux commands offer the --help option that displays a compact listing of all the options $`CMD` completion -- This enables you to first few letters of the command at the prompt,hit the TAB key and have the system complete the command for you.If there is more than one possible match to what you typed before pressing the TAB key,the system will simply beep.try TAB key again. WHEN THINGS GO WRONG --ENTER <ctrl-j> or <ctrl-m> --BACKSPACE <ctrl-h> --LOGOUT <ctrl-d> --clear <ctrl-l> --To kill the current line <ctrl-u> --To kill from current position to EOL <ctrl-k> --To stop the scrolling of screen output <ctrl-s> --To resume scrolling of screen output <ctrl-q> --If terminal behaves erratically "stty sane"CHAPTER 2 About the System
$history -- GNU History Library.history file contains a list of the commands isssued at the command prompt. $echo $PATH -- the path environment variable is used by the shell to where to look for commands. $echo $HOME -- the home environment variable is used to represent the user's home directory. Managing processes $ps --(group|user|pid|ppid|sid|tty) (a,x,u) -- report a snapshot of the current processes. $top (s,i,k,r,S,c,u,1) -- display Linux tasks in real time (cpu intensive) $kill -(1|9|15|l) -- send a signal to a process.1-stop & restart the process.9-stops a process ungracefully.15-stops a process by allowing it to exit(default). BACKGROUND $<ctrl-z> -- stops a process running in foreground. $jobs -- shows processes running in background. $fg jobnumber -- foreground $bg jobnumber -- background(puts a job in background stopped by <ctrl-z>)CHAPTER 3 Processing Text
Input Stream:default- keyboard(standard Input)-STDIN < $sort +1 < abc.txt Output Stream:default- screen(standard Output)-STDOUT >,>>(append) $sort +1 abc.txt > out.txt $sort +1 abc.txt >> out.txt Error Stream:default - screen(standard Error)-STDERR 2>,>&(stdout or stderr) $sort +1 abc.txt 2> err.txt >out.txt $sort +1 abc.txt >& out.txt Pipe Character(|) Output of first command line to be sent to another command as input. $sort +1 abc.txt | nl $tee -- copying standard input to standard output also to any files given as arguments.stdin appears in both stdout and file. $sort +1 abc.txt |tee out.txt |nl > owl.txt $xargs -- passing large number of arguments to another command.Enabling a command to process more arguments than it could handle.The 'xargs' is used to feed arguments to another command at a rate that it can handle.Using this command where it is not needed won't produce any errors. $find -uid 1001 | xargs du -h Sorting Lines of a file $sort -(d,f,m,n,r) -- sort lines of text files.It sort lines of text and displays them to standard output. $sort -option filename Cutting & Pasting Text $cut -(b,c,d,f) -- Print selected parts of lines from each FILE to standard output.Used to write selected parts of a file to standard output. $ls -l | cut -d ":" -f 2 $paste -(d,s,f) -- merge lines of files $ls -l | cut -d ":" -f 2 | paste -d \| -s - - $ls -l | cut -d ":" -f 2 | paste -d \| - - $uniq -(c,i,w,W) -- remove duplicate lines from a sorted file $split [OPTION] [INPUT [PREFIX]] -- split a file into pieces.It is used to divide one long file into many different files.It creates files all of a certain length,with a default length of 1,000 lines and sequentially names the files. $ls -l > /tmp/filesname $split -l 5 filesnames names $ls names* namesaa namesab $head -(n,c) -- output the first part of files $tail -(f,n) -- output the last part of files $sed -- stream editor.A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. It is used to make simple substitutions and more powerfule changes to file. $sed -option s/REGEXP/replacement/flag filename -A script can also be run on a file by using the -f option.which enables to -store frequently used options and simplifies larger commands.format of the -script. s/REGEXP/replacement/flag $grep -options [-e searchpattern] [-f filename] -- print lines matching a pattern.To search files for the pattern specified. $grep -i tirveni temp.txtCHAPTER 4 Managing Users
$useradd -(u,g,d,s,G,e) -- Create a new user or update default new user information $useradd tirveni -u 100 -d /home/tirveni -g guest $userdel -- Delete a user account and related files $userdel tirveni -r (deleting a user with home directory as well) $usermod -(u,g,d,s,G,e,u,L) -- Modify a user account $usermod tirveni -L (Locks a user passwd disabling it) $usermod tirveni -U (Unlock the passwd ) $groupadd -(g,o) -- Create a new group $groupadd -g 722 linusers $groupdel -- Delete a group $groupdel linusers $groupmod -- Modify a group $groupmod linusers -g 400 (changing the group id number) $passwd -(g,r,R) -- change user password. Group passwords are an inherent security problem since more than one person is permitted to know the password. However, groups are a useful tool for permitting co-operation between different users. $passwd (to change the passwd of self) /etc/passwd -- contains user account information,including one-way hashed passwords or much secure way to store password is /etc/shadow which is readable only by root account.CHAPTER 5 Managing the System
Compiling a new kernel 1 $make menuconfig -- configuration options are saved in /usr/src/linux/.config . 2 $make dep -- dependency setup is configured. 3 $make clean -- To remove temp files for every new compile. 4 $make bzImage -- To make a kernel image which has to be installed manuaaly. 5 $make modules -- Drivers configure to be modules are not compiled with rest of kernel, -- hence compile the modules. 6 $make modules_install -- placing modules in correct directory(: /lib/modules/<new kernel version> 7 $depmod -a -- To create a new module dependency list New kernel should be located in /usr/src/linux/arch/i386/bzImage 8 Copy the new kernel to /boot after renaming it like vmlinuz-2.6.8-test 9 Copy /usr/src/linux/System.map to /boot/ 10 $mkinitrd /boot/vmlinuz-2.6.8-test Make an initrd image whenever a driver is needed to access the root volume is compiled as module.CHAPTER 6 Using vi
There are three modes in 'vi' Input mode:Any key pressed is entered as text. Command mode:Any keys are used as commands to act on text. ex Mode: $vi filename To Open a file. To open a file from vi :e filename insert text to the left of the cursor i begin a new line below the current line of the text o Saving and closing. :wq or :x or ZZ Closing without saving. :q! Undo u To revert back to the last saved copy of the file :e! Deleting a single line dd Copying(Yanking) a single line yy Pasting p Going to line no#n nG Search Pattern Unix forward /Unix Search Pattern Unix backward ?Unix Repeating a search n Repeating a search backward N Search and Replace :%s/tirveni/yadav/g between line 10 and 20, c is for confirmation :3,10s/tirveni/yadav/gcCHAPTER 7 Major services in a Linux System
The single most important service in a UNIX system is provided by init init is started as the first process of every UNIX system, as the last thing the kernel does when it boots. When init starts, it continues the boot process by doing various startup chores (checking and mounting filesystems, starting daemons, etc). RUNLEVELS of init 0 - halt 1 - Single user mode 3 - multi user 5 - multi user with GUI 6 - system reboot /etc/inittab $kill -(s) pid -- send a signal to a process. sigHUP -1 exit restart(after config changes) sigKILL -9 exit this signal may not be blocked sigTERM -15 exit stop the process System Logging $tail -f /var/log/messages $tail -f /var/log/syslog $dmesg -- print or control the kernel ring buffer CONFIG FILE for syslogd -/etc/syslog.conf Scheduling jobs $at -(d,c,f,l,m,v) -- executes commands at a specified time. $at 4pm + 3 days; to run a job at 4:00pm on 3 days from now. $at 10am aug 15; to run a job at 10:00am on aug 15 from now. $cron -(l,r,e) -- daemon to execute scheduled commands. $crontab -e ; to edit the crontab file. minute(0-59) hour(0-23) day-of-month(1-31) month(1-12) day-of-week(0-7,[0,7-sunday|1-monday...]) cmd :- 35 14 * * 6 mv backup.tar.gz oldbackup.tar.gz 0 15 * * sun tar -czf backup.tar.gz /home/tirveni/* ://move backup to oldbackup at 14:35 on each saturday. ://backup /home/tirveni/* at 15:00 every sunday. Backup $tar -cvzf backup.tar.gz /home/tirveni Module Files /lib/modules/kernel-version/ block/ : modules for block devices.tape drives,floppy drives,RAID devices. cdrom/ : modules for cdrom devices.(propietary) fs/ : file system modules net/ : network interface driver modules.(PPP,SLIP,eth-card)CHAPTER 8 Directory Structure
/ The root filesystem is specific for each machine (it is generally stored on a local disk, althoughit could be a ramdisk or network drive as well) and contains the files that are necessary for booting the system up, and to bring it up to such a state that the other filesystems may be mounted. The contents of the root filesystem will therefore be sufficient for the single user state. It will also contain tools for fixing a broken system, and for recovering lost files from backups. /var The /var filesystem contains files that change, such as spool directories (for mail, news, printers, etc), log files, formatted manual pages, and temporary files. /home The /home filesystem contains the users' home directories, i.e., all the real data on the system. Separating home directories to their own directory tree or filesystem makes backups easier; the other parts often do not have to be backed up, or at least not as often as they seldom change. /bin Commands needed during bootup that might be used by normal users (probably after bootup). /sbin Like/bin, but the commands are not intended for normal users, although they may use them if necessary and allowed. /sbin is not usually in the default path of normal users, but will be in root's default path. /etc Configuration files specific to the machine. /root The home directory for user root. This is usually not accessible to other users on the system /lib Shared libraries needed by the programs on the root filesystem. /lib/modules Loadable kernel modules, especially those that are needed to boot the system when recovering from disasters (e.g., network and filesystem drivers). /dev Device files. These are special files that help the user interface with the various devices on the system. /tmp Temporary files. As the name suggests, programs running often store temporary files in here. /boot Files used by the bootstrap loader, e.g., LILO or GRUB. Kernel images are often kept here instead of in the root directory. If there are many kernel images, the directory can easily grow rather big, and it might be better to keep it in a separate filesystem. /mnt Mount point for temporary mounts by the system administrator. Programs aren't supposed to mount on /mnt automatically. /mnt might be divided into subdirectories /etc/rc.d or /etc/rc?.d Scripts or directories of scripts to run at startup or when changing the run level. /etc/passwd The user database, with fields giving the username, real name, home directory, and other information about each user. The format is documented in the passwd manual page. /etc/shadow /etc/shadow is an encrypted file the holds user passwords. /etc/fstab Lists the filesystems mounted automatically at startup by the mount -a command (in /etc/rc or equivalent startup file). Under Linux, also contains information about swap areas used automatically by swapon -a . /etc/group Similar to /etc/passwd, but describes groups instead of users. See the group manual page in section 5 for more information. /etc/inittab Configuration file for init. etc/mtab List of currently mounted filesystems. Initially set up by the bootup scripts, and updated automatically by the mount command. Used when a list of mounted filesystems is needed, e.g., by the df command.CHAPTER 9 Hardware Utilities
$lspci lspci is a utility for displaying information about all PCI buses in the system and all devices connected to them. $lsmod program to show the status of modules in the Linux Kernel. $modprobe program to add and remove modules from the Linux Kernel. $fdisk Partition table manipulator for Linux. $fdisk -l As a safety precaution, it is a good idea to write down the partition table on a piece of paper, so that if it ever corrupts you don't have to lose all your files. $fsck.ext3 check a Linux ext2/ext3 file system. $fsck.ext3 /dev/hdc2CHAPTER 10 System Monitoring
$top $ps $vmstat Report virtual memory statistics. $lsof list open files. $du estimate file space usage. $df report filesystem disk space usage. $who x="tirveni yadav"; echo "$x" | read a b; echo "$a $b"; read a b <<< "$x"; echo "$b $a";The End.