Friday, May 9, 2014

BASIC LINUX COMMANDS GORVAM SADDAR


                                                                Basic Linux Commands

Command
Example
Description
cat
Sends file contents to standard output. This is a way to list the contents of short files to the screen. It works well with piping.
cat .bashrc
Sends the contents of the ".bashrc" file to the screen.
cd
Change directory
cd /home
Change the current working directory to /home. The '/' indicates relative to root, and no matter what directory you are in when you execute this command, the directory will be changed to "/home".
cd httpd
Change the current working directory to httpd, relative to the current location which is "/home". The full path of the new working directory is "/home/httpd".
cd ..

Move to the parent directory of the current directory. This command will make the current working directory "/home.
cd ~
Move to the user's home directory which is "/home/username". The '~' indicates the users home directory.
cp
Copy files
cp myfile yourfile
Copy the files "myfile" to the file "yourfile" in the current working directory. This command will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if it exists.
cp -i myfile yourfile
With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.
cp -i /data/myfile .
Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt before overwriting the file.
cp -dpr srcdir destdir
Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-p option), file attributes (-p option), and copy recursively (-r option). With these options, a directory and all it contents can be copied to another directory.
dd
dd if=/dev/hdb1 of=/backup/
Disk duplicate. The man page says this command is to "Convert and copy a file", but although used by more advanced users, it can be a very handy command. The "if" means input file, "of" means output file.
df
Show the amount of disk space used on each mounted filesystem.
less
less textfile
Similar to the more command, but the user can page up and down through the file. The example displays the contents of textfile.
ln
Creates a symbolic link to a file.
ln -s test symlink
Creates a symbolic link named symlink that points to the file test Typing "ls -i test symlink" will show the two files are different with different inodes. Typing "ls -l test symlink" will show that symlink points to the file test.
locate
A fast database driven file locator.
slocate -u
This command builds the slocate database. It will take several minutes to complete this command. This command must be used before searching for files, however cron runs this command periodically on most systems.
locate whereis
Lists all files whose names contain the string "whereis".
logout
Logs the current user off the system.
ls
List files
ls
List files in the current working directory except those starting with . and only show the file name.
ls -al
List all files in the current working directory in long listing format showing permissions, ownership, size, and time and date stamp
more
Allows file contents or piped output to be sent to the screen one page at a time.
more /etc/profile
Lists the contents of the "/etc/profile" file to the screen one page at a time.
ls -al |more
Performs a directory listing of all files and pipes the output of the listing through more. If the directory listing is longer than a page, it will be listed one page at a time.
mv
Move or rename files
mv -i myfile yourfile
Move the file from "myfile" to "yourfile". This effectively changes the name of "myfile" to "yourfile".
mv -i /data/myfile .
Move the file from "myfile" from the directory "/data" to the current working directory.
pwd
Show the name of the current working directory
more /etc/profile
Lists the contents of the "/etc/profile" file to the screen one page at a time.
shutdown
Shuts the system down.
shutdown -h now
Shuts the system down to halt immediately.
shutdown -r now
Shuts the system down immediately and the system reboots.
whereis
Show where the binary, source and manual page files are for a command
whereis ls
Locates binaries and manual pages for the ls command.
Editors: emacs, vi, pico, jed, vim


Linux Logging in, Logging Out, and Shutting down
Logging in
Once you have completed your system install and booted your system, you should see a login prompt on your monitor. When you did your Linux install you should have set a root password. You may have also created a user with a password. Therefore to log in, you will want to type the name of a user or "root" for the login name and enter the appropriate password. If you logged in as a normal user and know the root password and want to use administration commands, you may use the command "su" to become a "super user". Some systems also support the "sudo" command, which allows administrative privileges on a command by command basis.
Linux Shell levels and the su command
The command, "su" will allow a normal user to enter a new shell level as the root user or as another user if they know the root user's or that user's password respectively. To become the root user, type "su" then you will be prompted for the root password. To become another user, type "su username". You must enter either that user's password to become that user. Every time you use the su command you enter a new shell level which means you have invoked a new running copy of the shell program, such as bash. You can see this change by typing the command "env" and looking at the value of the environment variable "SHLVL". This value increments when you use the su command and decrements when you use the "exit" command to exit that shell environment. You can also see the shell level value by typing "printenv SHLVL".
Logging out
Use the command "logout" to exit a given session. If you have logged in, then typed "su" to become a superuser or another user, you may need to type "exit" until your SHLVL environment value is 1. Then you can type "logout" to exit your session. The "exit" command will take you back to previous shell levels.
Shutting Linux Down
The system is intended to be shutdown by the system administrator using the shutdown command in one of the forms shown below. Many systems are set up to capture the keystroke combination to issue the shutdown command through the init program. This will work on most systems if the root user is logged in. Examples of using the shutdown command are shown below.
shutdown -h now
shutdown -r +10 "Rebooting in 10 minutes"
shutdown -r 13:00
The first command will shutdown and halt the system immediately. The second will reboot the system in 10 minutes and send the message to all users. The third command will shut the system down and do a reboot at 1:00 in the afternoon

Linux Files and File Permission

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:
1
2
3
4
5
6
7
8
9
10
File
User Permissions
Group Permissions
Other Permissions
Type
Read
Write
Execute
Read
Write
Execute
Read
Write
Execute
d
r
w
e
r
w
e
r
w
e
  • 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
drwxrwxrwx
4
george
team1
122
Dec 12 18:02
Projects
-rw-rw-rw-
1
george
team1
1873
Aug 23 08:34
test
-rw-rw-rw-
1
george
team1
1234
Sep 12 11:13
datafile
Which means the following:
Type and
# of
Files's
File's
Size in
Date of last
Filename
Permission field
Links
Owner
Group
Bytes
modification
|
|
|
|
|
|
|
drwxrwxrwx
4
george
team1
122
Dec 12 18:02
Projects
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 myfile
Gives everyone execute permission on myfile.
chmod ugo+x myfile
Same as the above command, but specifically specifies user, group and other.
chmod 400 myfile
Gives 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 myfile
Gives user full access, group read and write access, and other read access.
chmod 751 myfile
Gives user full access, group read and execute permission, and other, execute permission.
chmod +s myfile
Set the setuid bit.
chmod go=rx myfile
Remove read and execute permissions for the group and other.

Below are examples of making changes to owner and group:
chown mark test1
Changes the owner of the file test1 to the user Mark.
chgrp mark test1
Changes 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 Directory Structure
Note: Files are grouped according to purpose. Ex: commands, data files, documentation.

Parts of a Unix directory tree. See the FSSTND standard (Filesystem standard)
/                      Root
|---root               The home directory for the root user
|---home               Contains the user's home directories
|    |----ftp          Users include many services as listed here
|    |----httpd
|    |----samba
|    |----user1
|    |----user2
|---bin                Commands needed during bootup that might be needed by normal users
|---sbin               Like bin but commands are not intended for normal users.  Commands run by LINUX.
|---proc               This filesystem is not on a disk.  Exists in the kernels imagination (virtual).  This directory
|    |                 Holds information about kernel parameters and system configuration.
|    |----1            A directory with info about process number 1.  Each process
|                              has a directory below proc. 
|---usr                Contains all commands, libraries, man pages, games and static files for normal
|    |                 operation.
|    |----bin          Almost all user commands.  some commands are in /bin or /usr/local/bin.
|    |----sbin         System admin commands not needed on the root filesystem.  e.g., most server
|    |                 programs.
|    |----include      Header files for the C programming language.  Should be below /user/lib for
|    |                 consistency.
|    |----lib          Unchanging data files for programs and subsystems
|    |----local        The place for locally installed software and other files.
|    |----man          Manual pages
|    |----info         Info documents
|    |----doc          Documentation for various packages
|    |----tmp
|    |----X11R6        The X windows system files.  There is a directory similar to usr below this
|    |                 directory.
|    |----X386         Like X11R6 but for X11 release 5
|---boot               Files used by the bootstrap loader, LILO.  Kernel images are often kept here.
|---lib                Shared libraries needed by the programs on the root filesystem
|    |----modules      Loadable kernel modules, especially those needed to boot the system after
|                      disasters.
|---dev                Device files for devices such as disk drives, serial ports, etc.
|---etc                Configuration files specific to the machine.
|    |----skel         When a home directory is created it is initialized with files from this directory
|    |----sysconfig    Files that configure the linux system for networking, keyboard, time, and more.
|---var                Contains files that change for mail, news, printers log files, man pages, temp files
|    |----file
|    |----lib          Files that change while the system is running normally
|    |----local        Variable data for programs installed in /usr/local.
|    |----lock         Lock files.  Used by a program to indicate it is using a particular device or file
|    |----log          Log files from programs such as login and syslog which logs all logins,
|    |                 logouts, and other system messages.
|    |----run          Files that contain information about the system that is valid until the system is
|    |                 next booted
|    |----spool        Directories for mail, printer spools, news and other spooled work.
|    |----tmp          Temporary files that are large or need to exist for longer than they should in
|    |                 /tmp.
|    |----catman       A cache for man pages that are formatted on demand
|---mnt                Mount points for temporary mounts by the system administrator.
|---tmp                Temporary files.  Programs running after bootup should use /var/tmp.
Finding Files in Linux
There are three good methods of finding files in linux:
  1. The slocate database
  2. The whereis command
  3. The find command
The slocate database
To use the locate command, you will need to have a slocate database set up on your system. On many systems it is updated periodically by the cron daemon. Try the slocate command to see if it will work on your system:
locate whereis
Will list all files that contain the string "whereis". If that command did not work you will need to run the command:
slocate -u
This command will build the slocate database which will allow you to use the locate command. This command will take a few minutes to run.
The whereis command
This command will locate binary (or executable) programs and their respective man pages. The command:
whereis linuxconf
will find all binaries and manpages with the name linuxconf.

The find command
The following are examples of the find command:
find /home -user mark
Will find every file under the directory /home owned by the user mark.
find /usr -name *spec
Will find every file under the directory /usr ending in ".spec".
find /var/spool -mtime +40
Will find every file under the directory /var/spool that has data older than 40 days.
Find is a very powerful program and very useful for finding files with various characteristics. For more information, read the man page about find by typing "man find".
Locating man pages by subject
There is a keyword option in the man command that can be used to find man pages that have specific words in their descriptions. An example is:
man -k process
to find all man pages that talk about processes. Use the command:
man -k process |grep kernel
to find information on kernel processes. An equivalent command is the apropos command as follows:
apropos process
The which command
The which(1) program is a useful command for finding the full path of the executable program that would be executed if the name of the executable program is entered on the command line. The command:
which startx
Will show the full path of the startx command that will be run if "startx" is entered on the command line when an X session is started.
Linux Help
Sources of help include:
  1. Man pages - Stands for manual pages and are available on your system by invoking the word "man" followed by the subject, you need help for. An example is "man inetd" for information about the inetd internet super daemon program.
  2. Info - Info pages are meant as a replacement for man pages but are harder to use which is why man pages are still popular.
  3. Howtos - Howtos are written about various functional areas and Linux programs. They are likely to be available on your system in the /usr/doc/HOWTO directory. They are also available at various websites which can be accessed from the CTDP Linux Weblinks section.
  4. GNU manuals - Accessible through the GNU website which you can find in the the CTDP Linux Weblinks Documentation section.
  5. Package documentation - Documentation that comes with individual packages, usually found with the package or in the /usr/doc or /usr/local directory.
  6. Other LDP documents - There are many excellent Linux documents written in book form provided by The Linux Documentation Project which are available at their website. Their website may be accessed from the CTDP Linux Weblinks section. Many of these documents include subjects about Linux system administration, the Linux kernel, security and more.
  7. Independent documents and resources - There are many excellent independently written documents. Watch the Linux Weblinks section for information on these types of resources. These resources are added as they are located or suggested by CTDP members or the public.
Man Pages
Man pages general are written to describe programs, configuration files, functions and more areas of the system. They are organized into numbered sections covering various subject areas as follows:
  1. User commands that may be started by everyone.
  2. System calls, functions provided by the kernel.
  3. Subroutines, which are, library functions.
  4. Devices, which are, special files in the /dev directory needed to access items like com ports and disk drives among many other things.
  5. File format descriptions, such as configuration files and files like /etc/passwd.
  6. Games.
  7. Miscellaneous, such as macro packages, standard methods.
  8. System administration tools for the root user.
  9. More kernel routine documentation specifically for linux.
There may be other non numbered sections:
  • n - New documentation, that may be moved to a more appropriate section.
  • o - Old documentation, that may be kept for a grace period.
  • l - Local documentation referring to this particular system.
When referenced in documentation, man pages are many times referred to by section. For example see the man page on inetd(8). You normally don't need to do anything special to see the page in section 8 except type "man inetd". The syntax for using man pages is "man command", where "command" is the name of the command or item you want help for. Sometimes a given name is in more than one section. For instance the name crontab is a configuration file and a utility used to perform configuration. There is a man page for this in sections 1 and 5. If "man crontab" is typed the file in section one will be shown. In this case you will need to type the section number as follows:
man 5 crontab
Once you are in a man page, type [space] to see the next page, and type "Q" anytime to quit

Searching pages:

You can use a keyword function in man. To find commands that deal with DNS, type "man -k dns". A list of all commands system calls and other items that have the word "dns" in their name or short description is given. Entering "man -k dns| grep domain" will search the man pages for occurrences of "dns" and "domain". Another command equivalent to using the "man -k is the apropos command. To search on a particular subject in the man page library type "apropos subject". A list of all man pages with the associated subject in its description will be displayed. This information is kept in the apropos database which is updated weekly by cron. See the section on cron or type "man apropos" or "man whatis" for more information.

To print a man page type "man name | lpr -P". You must have your printer set up for this to work.

Info Pages
Much documentation is included as info pages. Many times the same documentation is included in man pages, but sometimes information is included in infopages that is not in man pages. I don't like info pages since you need to remember many different key commands to navigate through them. There is a tutorial in the info pages that you can use to learn the commands. I recommend that before using info pages, you do at least a quick tour by taking the tutorial. Take notes and keep them handy when using the info pages. You may use the info pages by typing "info command" where command is the name of the command you need information about. To begin the tutorial, type "info", and look for the help section. Some of the basic info commands are listed below.
h
Take the help tutorial
To move down (see more text) on a screen
or Backspace
To page up the screen
b
To move to the top of a screen
n
To move to the next node (text on a particular topic)
p
To move to the previous node
d
To move to the main directory node
l
To move to the last node you were at. Retraces where you were.
L
Refresh the screen
m
Show a menu of nodes you can move to
g
Cancel menu selection
name
Type node name or Ctrl-g to cancel after typing "m" for menu
u
To move back to the menu you were at. Use this command after making a menu selection to get back to the last menu from which you made a selection.
?
Show available commands.

Howto Documents and GNU manuals
Along with the man pages there is much Linux documentation contained in the various howto files and GNU documents written by the Linux community. There are many links on the web to help you find these sources of documentation. The easiest way to find these documents is to link to the websites that have them in the Linux, Weblinks, Documentation section of this website. Most of the how to documents should be on your system in the directory /usr/doc/HOWTO. They are in text format and can be read by the program "less" or any text editor.
Package Documentation
Linux is generally made up of many packages. Each package deals with certain functionality of Linux, but may have several commands. For instance, the etherboot package has a purpose of allowing users to set up remote machines for booting using a Linux server. This package has several binary programs used to perform this function. Most packages contain various text files or html or dvi files that contain further documentation about the package and their various commands. Most package documentation is in the directory "/usr/doc" or "/usr/local" under the specific package name. There are also various important manuals which may have come with your distribution. These manuals may be in postscript format. From an X session if you locate these files, you can double click on them to read them in postscript. The names of some of these manuals are:
  1. Bash Reference Manual
  2. LILO User's Guide
  3. LILO Technical Overview
  4. Name Server Operations Guide for BIND
  5. IP Command Reference
  6. IPv6 Flow Labels in Linux-2.2
  7. Tunnels over IP in Linux-2.2
  8. IP Chains Quick Reference
  9. Sendmail Installation and Operations Guide
  10. Sendmail - An Internetwork Mail Router
  11. mgetty+sendfax
  12. Automated System Monitoring and Notification with SWATCH
  13. SGML-Tools User's Guide
Setting Linux Time
Programs for working with time and date are:
  • clock - This is a soft link to hwclock.
  • date(1) - Print or set the system date and time.
  • hwclock(8) - Sets the hardware clock based on values entered on the command line.
  • setclock(8) - Sets the BIOS hardware clock based on the time and setup of the system clock.
  • timeconfig(8) - A program used to configure the system configuration file "/etc/sysconfig/clock" which includes the type of clock and timezone.
An Example:
While logged in as root do the following:
  1. Type "date".
  2. You should see some variation of"
"Wed Nov 24, 9:29:17 EST 1999"
  1. To change the time type(as an example):
date -s 10:10
  1. The system response will be:
"Wed Nov 24, 10:10:02 EST 1999"
  1. Then if you want to set the hardware(BIOS) clock so the system will keep the time when it reboots type:
clock -w
or
setclock
The program setclock will set your hardware clock based on your system configuration parameters including whether or not your clock is set to universal time.
The "clock -w" command assumes your hardware clock is set to local time. If it is set for universal time you will want to type "clock -wu" rather than the "clock -w" in the line above. If you use the wrong option the time will be set incorrectly and you will need to do it again.
On a Redhat system, you can use the program "linuxconf" as root and page down to the next to the last line in the menu which is "date & time". Select it and see if the box named "universal format(GMT)", next to "Store date in CMOS", is checked. If is is not, you may save your time by typing "clock -w". If it is checked use "clock -wu".
Note: There is a man page for date that you can use to learn more. Type "man date". You do not want to make any more changes to time and especially to the date than necessary, especially while the system is running, since this can trigger the "cron" daemon to perform various time related system tasks.
An alternate method to set time is.
hwclock --set --date "2/24/2000"                If you are using local time
hwclock --set --date "2/24/2000" -utc           If you are using universal time
In the rc.sysinit startup script, this program is used with the options -adjust and -hctosys to adjust the hardware clock for drift, and set the system time to the hardware clock at the time of reboot.
On Redhat systems, there is a configuration program called "timeconfig" which can be used to configure the system configuration file /etc/sysconfig/clock and /etc/localtime. This program will use a graphical interface to let the user set the timezone and whether UTC time is used.
Basic Liinux Devices
The first partition on a IDE hard drive is called partition 1, and is called /dev/hda1 if the drive is the primary IDE master.
/dev/fd0
Floppy disk
/dev/hda1
IDE Hard drive 1, partition 1
/dev/hdb3
IDE Hard drive 2, partition 3
/dev/sda1
First SCSI interface (probably hard drive), device id 1
/dev/sdc3
First SCSI interface, device id 3
/dev/cdrom
CD ROM drive
/dev/mouse
Mouse device, sometimes a pointer to another device such as /dev/psaux, a ps/2 mouse driver.

primary IDE master
/dev/hda
primary IDE slave
/dev/hdb
secondary IDE master
/dev/hdc
secondary IDE slave
/dev/hdd

The first partition on a IDE hard drive is called partition 1, and is called /dev/hda1 if the drive is the primary IDE master.
Linux Tips
Multiple Virtual Terminal access
There are normally 6 virtual terminals in Linux, available by using Alt-F1 through Alt-F6. Each one can be logged in as a different user. There are normally 6 terminals available in X also, F7 through F12. The first X session will be on F7 (if on a local terminal), the second on F8, and so forth. If an X session is started from F1 and you also have an active session on F2, you can type Ctrl-Alt-F2 to go from the X session to the virtual console on F2. Also to get back to your X session, you can type Ctrl-Alt-F7. This example assumes that your terminals are setup in the standard manner with 6 virtual terminals that spawn the getty program available. You can check your setup by checking your /etc/inittab file. You should have lines like the following in your file.
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
See the section on init for further information on this file.
Linux Command line shortcuts
If typing a command on the command line, you can press before the command is complete and if there are enough characters for it to be unique, the system will finish the command for you. If it is not yet unique, and is pressed twice, you will be given choices.
Pasting text in files
There is a cut and paste mouse utility that works with virtual consoles called gpm which runs as a daemon. To use it,
  1. Move your mouse to the text you want to cut or paste
  2. Hold the left mouse button down
  3. Drag the mouse to the end of the selected text
  4. Release the mouse button
  5. If deleting text, just press the "DEL" key for your final step. If pasting text, move the text cursor to the location you want to paste to by switching terminals with function keys, using arrow keys, etc.
  6. If pasting, press the right mouse button.
Viewing previously displayed text
Text that has scrolled off the top of the screen may be viewed again using the key combination. The Keys in the numbers section on the far right of the keypad do not work for this function, only the grey PgUp and PgDn keys just to the right of the key. If you want other keys to perform this function, it would be necessary to map them for bash shell keymapping. Pressing any other key other than or will bring you back to the normal screen location.
Accessing Removable Media
Making a Linux Filesystem on a Floppy
  1. Use fdformat /dev/fd0H1440
  2. To make a filesystem on the disk type "mkfs –t filesystem –c /dev/fd0H1440" where filesystem is the type of filesystem, usually ext2 (linux native).
  3. Mount the filesystem "mount –t ext2 /dev/fd0 /mnt/floppy
Mounting floppy and CD-ROM drives
mount /dev/fd0 /mnt/floppy
To mount a floppy
umount /dev/fd0
To unmount a floppy
mount /dev/cdrom /mnt/cdrom
To mount a cdrom or
mount -t iso9660 -r /dev/cdrom /mnt/cdrom
Another way to mount a CD-ROM with type specified or mount -t auto /dev/cdrom /mnt/cdrom
Note the CDROM may be on the secondary controller as a master (/dev/hdc) or slave (/dev/hdd). In fact, your /dev/cdrom is probably actually a softlink to one of these two devices, if you have an IDE interface. If you use SCSI, you will probably use something like /dev/sda1 or 2, etc.

Mounting a parallel port zip drive:
mount -t vfat /dev/sda4 /mnt/zipdrive

Note: in all these examples, the mount points must exist, be empty, and not already have a device mounted on them. Also you must unmount the device before removing it.
Setting up Removable and External Filesystems to Automatically mount when used
  1. The program "autofs" must be setup to run as a daemon upon system startup. To do this with Redhat Linux, use the program "linuxconf" and select "Control", "Control panel", "Control Service activity". Activate "autofs" using the menu selections.
  2. Edit the file "/etc/auto.master" to the following:
3.      /mnt     /etc/auto.misc   --timeout 20
The above example sets the program to unmount the device after 20 seconds.
  1. Edit the file "/etc/auto.misc" adding lines like:
5.        cd    -fstype=iso9660,ro     :/dev/cdrom
6.        fl    -fstype=auto           :/dev/fd0
This will cause the cd-rom to be mounted when you access the directory "/mnt/cd" and the floppy to be mounted when you access "/mnt/fl". The directories "/mnt/cd" and "mnt/fl" must not exist in order for this to work.
  1. To use automount, put a cd in the CD-ROM drive and type "ls /mnt/cd" or "cd /mnt/cd".
Making and Managing Linux Filesystems
Programs used to manage filesystems
  • badblocks(8) - Search a device for badblocks. The command "badblocks /dev/hda" will search the first partition of the first IDE hard drive for badblocks.
  • cfdisk(8) - A partition table manipulator used to create or delete disk partitions.
  • dosfsck(8) - Used to check a msdos filesystem.
  • dumpe2fs(8) - Lists the superblock and blocks group information on the device listed. Use with a command like "dumpe2fs /dev/hda2". The filesystem on the device must be a Linux filesystem for this to work.
  • fdformat(8) - Performs s lowlevel format on a floppy disk. Ex: "fdformat /dev/fd0H1440".
  • fdisk(8) - Used to add or remove partitions on a disk device. It modifies the partition table entries.
  • fsck(8) - Used to check and/or repair a Linux filesystem. This should only be used on systems that are not mounted.
  • hdparm(8) - Used to get or set the hard disk parameters.
  • mkdosfs(8) - Used to create a msdos filesystem.
  • mke2fs(8) - Create a Linux native filesystem which is called a second extended filesystem. This creates the current version of the Linux filesystem.
  • mkfs(8) - Used to make a Linux filesystem on a device. The command "mkfs /dev/hdb1" will create a Linux filesystem on the first partition of the second IDE drive.
  • mkswap(8) - Creates a Linux swap area on a device.
  • mount(8) - Used to mount a filesystem. It supports many types of filesystems.
  • stat(1u) - Used to print out inode information on a file. Usage: stat filename
  • swapoff(8) - Used to de-activate a swap partition.
  • swapon(8) - Used to activate a swap partition.
  • tune2fs(8) - Used to adjust filesystem parameters that are tunable on a Linux second extended filesystem. The filesystem must not be mounted write when this operation is performed. Can adjust maximum mount counts between filesystem checks, the time between filesystem checks, the amount of reserved blocks, and other parameters.
  • umount(8) - Unmount a filesystem.
Making a Filesystem
Making a swap partition
type "mkswap -c /dev/hda3 10336"
The -c has swap check for bad blocks. The 10336 is the size of the partition in blocks, about 10M. The system enables swap partitions at boot time, but if installing a new system you can type "swapon /dev/hda3" to enable it immediately.
Making an ext2 file system on a floppy
  1. fdformat /dev/fd0H1440
  2. mkfs -t ext2 -c /dev/fd0H1440
Other file systems:
A normal hard drive can have many types of filesystems on it. To create an ext2 file system, type "mke2fs -c /dev/hda2 82080" to create an 82 meg filesystem. Note: mkfs is a front end to many file system types including ext2, minux, and msdos.
Checking a Filesystem
fsck - Used to check and repair a filesystem.
fsck is a front end to a filesystem type specific fsck.ext2, fsck.minix, and fsck.msdos.
Syntax: fsck -t type device
Ex: fsck -t ext2 /dev/hda3
Linux Emergency Filesystems and Procedures
There are several ways to make a boot floppy, but basically there would be two types of boot floppys that make sense. The first is a simple boot floppy with a copy of your kernel that will mount your root device which is usually on your hard drive. This will give you another way to boot to your system and allow you to repair some problems such as a bad re-compile of a kernel or a lilo failure, but will not do much good if you develop a serious problem with your root filesystem. The second is a more elaborate boot floppy including a boot and root floppy and will independently support some tools that will allow repair of a root filesystem, or at least help you rescue files.
Making a simple Emergency boot floppy
How to make a single boot floppy:
  1. Find the kernel. It is usually /vmlinuz or /boot/vmlinuz. The file vmlinuz may be a softlink to the actual kernel executable. Find the executable kernel.
  2. Copy the kernel image to the floppy.
dd if=/vmlinuz of=/dev/fd0
  1. Type the command "df" and examine it's output to determine where your root filesystem is. Your root is "/" and is mounted on something like "/dev/hda2".
  2. Set the kernel image on the floppy to the location of your root system.
rdev /dev/fd0 /dev/hda2
Your root filesystem may be somewhere other than "/dev/hda2".
  1. Test the floppy by rebooting your system and attempting to boot from it.
You can use the command "badblocks /dev/fd01440 1440" to check the floppy for badblocks.
Making a Boot Floppy with a Root Filesystem (not yet complete or accurate)
To make a Linux boot floppy with a root floppy will require two floppy disks. One will be used for the kernel, and the other will hold the root filesystem.
Important functions needed by an emergency boot filesystem:
e2fsck, mount, tar, gzip, insmod, ftape module for kernel if using tape backups

Please note, that it is best to recompile your kernel to do this. This is because most kernel images use modules for support that are stored on your root filesystem and if your root filesystem is corrupt, it may not work properly. The good news is, however, that you will only need a kernel with minimal support, since this is for emergency use only, and you won't need networking support and some other features. The other option is to use a kernel image already compiled that does not depend on modular support if you can find one.
  1. Compile the kernel:
    Most options, you may answer no to with the following exceptions.
    • Make processor type 386 to support all machines with 1Gb of memory.
    • Enable PCI support with access mode=any and quirks=y.
    • CONFIG_SYSVIPC=y
    • CONFIG_BINFMT_ELF=Y
    • CONFIG_PNP=Y
To compile the kernel make sure you provide floppy, CD-ROM and hard disk support for your types of devices such as IDE or SCSI. In my compile, I support IDE only. Set CONFIG_BLK_DEV_IDE=Y and all in the IDE category may be set to Y except for "other IDE chipset support", CONFIG_IDE_CHIPSETS=N. Ramdisk support (CONFIG_BLK_DEV_RAM=Y) with initial support. Virtual terminals (CONFIG_VT=Y). CONFIG_VT_CONSOLE=Y, CONFIG_SERIAL=Y, CONFIG_MOUSE=Y, CONFIG_PS2MOUSE=Y. Under filesystems I supported all but quota, kernel automounter support, and UMSDOS. Include all under partition table support. Include VGA text console and Magic SysRq key.
  1. Copy the kernel image to the first floppy.
dd if=/vmlinuz of=/dev/fd0
  1. Set the image on the floppy to boot to a root filesystem on your floppy device.
rdev /dev/fd0 /dev/fd0
  1. Prepare the root floppy (the second floppy disk)
    1. type "fdformat /dev/fd0H1440" to format the floppy
    2. type "mkfs -t ext2 -c /dev/fd0H1440" to create a filesystem on the floppy
Linux Environment Settings, Shell Selection and Startup Scripts
Linux Environment Variables and Settings
There are many environment variables, some of which are listed below. The command used to add or modify environment variables is the export builtin command provided by the bash shell. See the bash(1) man page for more information on this built in command.
PS1="[\u@h \w]\\$ "
Used to set the command line string to output user,machine and name of directory the user is in. In Redhat, to effectively change this value, it must be changed in the file "/etc/bashrc" and in "/etc/profile".
export PATH=$PATH:/usr/sbin
Adds /usr/sbin to the path in the system environment
MANPATH
Sets the path for man pages
Other environment variables include:
  1. USERNAME
  2. HOSTNAME
  3. LOGNAME
  4. MAIL
  5. EDITOR - Specifies the editor to be used by default for some commands such as edquota. Usually it is set to vi or emacs with a command like "export EDITOR=emacs".
  6. TERM - The type of terminal being used.
  7. PATH - The path the system looks in to find commands that the user has entered.
  8. HOME - The current user's home directory
  9. SHELL - The current shell program that is being executed
  10. USER - The name of the current user.
  11. TMPDIR - Allows programs that use the tempnam(3) function call to use the directory specified by this variable rather than the /tmp directory.
  12. SHLVL - Shows how many shells the user has invoked.
You can type "env" to see a listing of all current environment variables.
SHLVL indicates how many shells deep the user is. If the level is 2, you must type "exit", then logout to exit.
Setting noclobber=1 - keeps files from being clobbered by the shell.
Shell Selection for users
The file "/etc/shells" lists the valid shells and shell paths on your system. An example file is:
/bin/bash
/bin/sh
/bin/tcsh
/bin/csh
/bin/ash
/bin/bsh
/bin/bash2
/bin/ksh
/bin/zsh
The command "chsh" can be used by the user to set his preferred shell. Also the root user can use this command to set any user's shell. This command with the -l option will list the available shells. The -s option will allow the user to change their shell. For example type the following to determine and modify the user's shell.
  1. chsh -l
  2. chsh -s /bin/sh
The first line will show available shells, and the second entry will change it. The full path of the shell must be entered for the command to work.
Startup Scripts
The following scripts are run by bash if the –noprofile option is not used with the shell:
/etc/profile
This script file is run first for all users during the login process
$HOME/.bash_profile
This script file is run for individual user's customized setup
$HOME/.bash_login
If the .bash_profile above does not exist, this file is run
$HOME/.profile
This script file is run if $HOME/.bash_profile and .bash_login don't exist
If bash is invoked as "sh" it will run only "/etc/profile" and "$HOME/.profile", above.
The man page says that "$HOME/.bashrc" is run for non-login interactive shells. I'm not sure if it also runs for login interactive shells, but believe it does. If it does, I'm not sure whether it is run after the scripts above, but believe it is.
$HOME/.bash_logout
Run when the user logs off the system.

Key bindings:
If the shell variable "INPUTRC" is set, probably in "/etc/profile" the keybindings are set by the file declared in the INPUTRC value. Usually this is "/etc/inputrc". Otherwise the keybindings are set in the file "$HOME/.inputrc" for each individual user.
$HOME/.inputrc - User's keybindings definition
/etc/inputrc - Global keybindings definition
Other scripts and files:
/etc/bashrc
On my Redhat system, this file is run by $HOME/.bashrc.
/etc/login.defs
Used with shadow passwords to set initial PATH and other parameters including how often a user must change passwords and what is acceptable as a password..

Put any new script files in /usr/bin or /usr/local/bin. If you put them in /usr/local/bin, they will be easier to keep track of, but you may want to add /usr/local/bin to your path environment to make them more accessible. You can put the command "export PATH=$PATH:/usr/local/bin" in one of the above scripts or add it to the path statement in /etc/profile.
Therefore to modify parameters for all users, edit:
/etc/profile
For users environment setup and other customized items.
/etc/inputrc
For keybindings on some systems if INPUTRC is set to it.

To customize user's setup edit: (Note: In /home/user's name/...)
HOME/.bash_profile - Users environment stuff and startup programs
$HOME/.bashrc - user aliases and functions
$HOME/.bash_logout - User actions to be done at logout
$HOME/.inputrc - Contains keybindings and other bits.
The tcsh shell operates as follows:
/etc/csh.login is run
$HOME/.tcshrc or .cshrc is run
$HOME/.login is run
Other files in the users home directory may be:
.emacs - For the emacs editor. Consists of LISP functions.
.exrc - For the vi editor
.fvwmrc - For the fvwm window manager
.twmrc - For the twm window manager
.newsrc - For newsreaders. Contains a list of newsgroups offered at this site.
.Xdefaults - For programs using the X window system
.xinitrc - Shell commands that run when the user logs into an X session.
Linux Kernel
The Kernel and its Modules
Most kernels are compiled so modular support is required except many used on floppy boot disks.
The package modules.tar.gz contains all the programs needed to manage modules. This should already be installed on most distributions. The kernel modules are usually in a directory pertinent to the kernel version in /lib/modules. Modules can be found in "lib/modules/2.2.12-20" for kernel version 2.2.12-20. They are loadable modules ending in ".o" that are used to support the kernel.
To load a module type "insmod module" where "module" is the name of the module to load. Ex: insmod /lib/modules/2.2.12-20/misc/ftape.o

Programs used to manage modules are:
  • lsmod - Lists all the currently loaded kernel modules
  • rmmod - Unloads modules, Ex: rmmod ftape
  • depmod - Creates a dependency file, "modules.dep", later used by modprobe to automatically load the relevant modules.
  • modprobe - Used to load a module or set of modules. Loads all modules specified in the file "modules.dep".
Modules are loaded from startup script files using "modprobe" to handle loadable modules automatically.

modprobe -l |more
Lists all the modules available for your kernel
rmmod module_name
Remove a module from the kernel
Finding the Kernel
Locate the kernel image on your hard disk. It should be in the file /vmlinuz, or /vmlinux. In some installations, /vmlinuz is a soft link to the actual kernel, so you may need to track down the kernel by following the links. On Redhat 6.1 it is in "/boot/vmlinuz". To find the kernel being used look in "/etc/lilo.conf".

Type "uname -a" to see the kernel version.
Compiling the Kernel
  1. Backup the present kernel which is in "/boot" for most systems. You can tell where it is by looking at "/etc/lilo.conf" . sometimes lilo.conf will point to a link file. Trace the link file and backup the file being pointed to.
  2. Have an alternate way to boot such as another linux system on your computer or an emergency boot disk that you can access your filesystem from in case your new kernel crashes.
  3. Back up your kernel modules in the directory "/modules". There is probably a directory full of modules so you should be able to back it up using "cp –dpr dir1 dir2" where dir1 is the name of the directory where your modules are and dir2 is where you want to put them.
  4. Go to "/usr/src/linux". There is a "/usr/src/linux/.config" file used to do the compile. It is modified by typing "make config" and the values in it are used to determine defaults while doing the "make config". Back this file up to keep your original settings if you want.
  5. Type "make config" and answer the hundreds of questions asked. On another terminal, open the file "/usr/src/linux/Documentation/Configure.help" to determine what each configuration is. Also you can type "make menuconfig" or from an X session "make xconfig".
  6. Type "make dep"
  7. Type "make clean"
  8. Type "make bzImage" or "make zImage" if the kernel is small.
  9. Type "make modules"
  10. Type "make modules_install"
  11. Copy the file "/usr/src/linux/arch/i386/boot/bzImage" to "/boot/vmlinuz".
  12. Copy the file "System.map" from "/usr/src/linux" to the "/boot" directory. Rename it "System.map=w.x.y" and make sure there is a link file called "System.map" pointing to it. This step will keep you from getting warning messages when you boot. It seems to be a step they forgot to include in the kernel-howto.
    • cp /usr/src/System.map /boot/System.map-2.2.14
    • cd /boot
    • rm System.map
    • ln –s System.map-2.2.14 System.map
  13. Run rdev on the new kernel image to verify the root filesystem device. "rdev /boot/vmlinuz /dev/hda2". Please note that this step may be optionally dependent on whether you want to use the boot loader, lilo, to point to the root device.
  14. Make sure the /etc/lilo.conf file is correct (image=/boot/vmlinuz)
  15. If your kernel has a feature supported by a module that is required to boot, you will need to make a RAM disk boot image or your system won't boot.
    • Type "lsmod" and look to see if the "loop" module is loaded. If it is skip the next step.
    • Type "insmod /lib/modules/2.2.14/block/loop.o"
      Use this command if the loopback module is not installed. This assumes you compiled the support into your kernel. In my example, I used kernel version 2.2.14, but your kernel version may have a different number. Substitute the appropriate values.
    • Type "mkinitrd /boot/initrd-2.2.14.img 2.2.14"
      Again, this example is for kernel version 2.2.14. This command will create a RAM image module for your kernel to load into.
    • Add an entry similar to "initrd=/boot/initrd-2.2.14.img" to the "/etc/lilo.conf" file for the stanza that is used to boot this kernel. An example of the stanza follows.
o   image=/boot/vmlinuz
o         label=rhl
o         initrd=/boot/initrd-2.2.14.img
o         read-only
o         root=/dev/hda2
            
Read the section about LILO for more information or read the lilo and lilo.conf man pages.
  1. Run lilo by typing "lilo". If you didn't set the root device in step 12, you will need a line like "root=/dev/hda2" in /etc/lilo.conf in the group of commands for your kernel. This will tell the kernel where your root filesystem is.
This step may be necessary if you are using a kernel previous to the 2.2 series. In the directory "/usr/include" may be several link files such as "asm", "linux", and "scsi". These are normally softlinks through "/usr/src/linux". If they are, you can direct the softlink file "usr/src/linux" to your new kernel file which may be in the form "linux-2.2.14". If you don't have the "linux" softlink file and don't want to create it, you will need to remove and redirect the softlink files in "/usr/include" to the proper location of your new kernel source.
The sysctl tool
This tool is worth mentioning in this section since it is used to manipulate kernel parameters. If you type "sysctl -a |more" you will see a long list of kernel parameters. You can use this sysctl program to modify these parameters. However, I have been unable to add new parameters.




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.