Saturday, March 29, 2014

Directory Structure in Linux


Have you wondered why certain programs are located under /bin, or /sbin, or /usr/bin, or /usr/sbin?
For example, less command is located under /usr/bin directory. Why not /bin, or /sbin, or /usr/sbin? What is the different between all these directories?
In this article, let us review the Linux filesystem structures and understand the meaning of individual high-level directories.





1. / – Root


  • Every single file and directory starts from the root directory.


  • Only root user has write privilege under this directory.


  • Please note that /root is root user’s home directory, which is not same as /.



  • 2. /bin – User Binaries


    • Contains binary executables.


    • Common linux commands you need to use in single-user modes are located under this directory.


    • Commands used by all the users of the system are located here.


    • For example: ps, ls, ping, grep, cp.



    • 3. /sbin – System Binaries


      • Just like /bin, /sbin also contains binary executables.


      • But, the linux commands located under this directory are used typically by system aministrator, for system maintenance purpose.


      • For example: iptables, reboot, fdisk, ifconfig, swapon



      • 4. /etc – Configuration Files


        • Contains configuration files required by all programs.


        • This also contains startup and shutdown shell scripts used to start/stop individual programs.


        • For example: /etc/resolv.conf, /etc/logrotate.conf



        • 5. /dev – Device Files


          • Contains device files.


          • These include terminal devices, usb, or any device attached to the system.


          • For example: /dev/tty1, /dev/usbmon0



          • 6. /proc – Process Information


            • Contains information about system process.


            • This is a pseudo filesystem contains information about running process. For example: /proc/{pid} directory contains information about the process with that particular pid.


            • This is a virtual filesystem with text information about system resources. For example: /proc/uptime



            • 7. /var – Variable Files


              • var stands for variable files.


              • Content of the files that are expected to grow can be found under this directory.


              • This includes — system log files (/var/log); packages and database files (/var/lib); emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed across reboots (/var/tmp);



              • 8. /tmp – Temporary Files


                • Directory that contains temporary files created by system and users.


                • Files under this directory are deleted when system is rebooted.



                • 9. /usr – User Programs


                  • Contains binaries, libraries, documentation, and source-code for second level programs.


                  • /usr/bin contains binary files for user programs. If you can’t find a user binary under /bin, look under /usr/bin. For example: at, awk, cc, less, scp


                  • /usr/sbin contains binary files for system administrators. If you can’t find a system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel


                  • /usr/lib contains libraries for /usr/bin and /usr/sbin


                  • /usr/local contains users programs that you install from source. For example, when you install apache from source, it goes under /usr/local/apache2



                  • 10. /home – Home Directories


                    • Home directories for all users to store their personal files.


                    • For example: /home/john, /home/nikita



                    • 11. /boot – Boot Loader Files


                      • Contains boot loader related files.


                      • Kernel initrd, vmlinux, grub files are located under /boot


                      • For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic



                      • 12. /lib – System Libraries


                        • Contains library files that supports the binaries located under /bin and /sbin


                        • Library filenames are either ld* or lib*.so.*


                        • For example: ld-2.11.1.so, libncurses.so.5.7



                        • 13. /opt – Optional add-on Applications


                          • opt stands for optional.


                          • Contains add-on applications from individual vendors.


                          • add-on applications should be installed under either /opt/ or /opt/ sub-directory.



                          • 14. /mnt – Mount Directory


                            • Temporary mount directory where sysadmins can mount filesystems.



                            • 15. /media – Removable Media Devices


                              • Temporary mount directory for removable devices.


                              • For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder for CD writer



                              • 16. /srv – Service Data


                                • srv stands for service.


                                • Contains server specific services related data.


                                • For example, /srv/cvs contains CVS related data.




                                • Run Levels in Linux (inittab):



                                  So
                                  here goes another article for Linux. “ Run Levels in Linux”




                                  Runlevels” as the name suggests is a type of Linux Operating systemyou want to boot when you start a computer like text mode environment
                                  , graphical mode, etc. In linux we can switch to another run level as
                                  per your need from the CLI. Run levels define what processes or services to run automatically while
                                  the system boots up. This is defined in /etc/inittab file.



                                  The init process is the last step in the boot procedure and has pid of
                                  ‘1’.’ “init” is responsible for starting system processes
                                  as per defined in the /etc/inittab file.


                                  init” process checks which default run level is defined in
                                  /etc/inittab andstarts the system in that run level which means all the services
                                  defined for that run level gets executed.







                                  So, in the above screenshot for my CentOS, it shows that the default
                                  init level is 5 which means that whenever I boot my system it will
                                  boot in graphical mode. We can change the default mode as per our
                                  requirement. The file is stored in /etc/inittab and can be changed
                                  using vi editor.


                                  init” process checks which default run level is defined in
                                  /etc/inittab andstarts the system in that run level which means all the services
                                  defined for that run level gets executed.

                                  There are 7 different run levels present (run level 0-6) in Linux system
                                  for different purpose. The descriptions are given below.

                                  0:
                                  Halt System (To shutdown the system)
                                  1: Single user mode
                                  2:
                                  Basic multi user mode without NFS
                                  3: Full multi user mode (text
                                  based)
                                  4: unused
                                  5: Multi user mode with Graphical User
                                  Interface
                                  6: Reboot System

                                  Most desktop Linux distributions boot into run level 5, which starts up
                                  the Graphical Login Prompt. This allows the user to use the system
                                  with X-Windows server enabled. Most servers boot into run level 3,
                                  which starts the text based login prompt as it is advisable not to
                                  install graphical windows in a server as lots of space goes waste and
                                  also it takes lot of resource to run.



                                  Each run level is defined inside its own directory structure. These
                                  directories are located in the /etc/rc.d/ directory, under which you
                                  have rc1.d, rc2.d… rc6.d directories where the number from 0 to 6
                                  corresponds to the specific run level. Inside each directory symbolic
                                  links are defined to a  to master initscripts found in
                                  /etc/init.d or /etc/rc.d/init.d.


                                  Switching between different runlevels:-




                                  Method-1:
                                  Changing run level without reboot (temporarily).

                                  We
                                  can use init command to change rune levels without rebooting the
                                  system.

                                  Ex:-if
                                  we are currently in run level 3 and want to go to run level 1, just
                                  we need to execute

                                  #
                                  init 1

                                  Or
                                  if you want to shutdown a machine you can take help of run level ‘0’
                                  .Just you need to execute

                                  #init
                                  0

                                  Remember
                                  this change is not permanent and on next reboot you will get your
                                  default runlevel.



                                  Method-2:
                                  Changing run level permanently

                                  If
                                  you want to change your default run level then

                                  Open
                                  the file /etc/inittab and edit entry initdefault:

                                  #
                                  vi /etc/inittab

                                  Let’s
                                  set initdefault to 5, so that you can boot to X next time when Linux
                                  comes up:

                                  id:5:initdefault:



                                  Method-3:-Change
                                  run level at boot time

                                  You
                                  can also change the run level at boot time. If your system uses LILO
                                  as the boot manager, you can append the run level to the boot
                                  command:

                                  LILO:
                                  linux 3 or
                                  LILO: linux 5

                                  If
                                  your system uses GRUB, you can change the boot runlevel by pressing
                                  the `e’ key to edit the boot configuration. Append the run level(in
                                  our case 5) to the end of the boot command as shown:


                                  kernel
                                  /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet  5


                                  Understanding the /etc/password file in Linux

                                  /etc/passwd file stores essential information, which is required during login i.e. user account information. /etc/passwd is a text file, that contains a list of the system's accounts, giving for each account some useful information like user ID, group ID, home directory, shell, etc. It should have general read permission as many utilities, like ls use it to map user IDs to user names, but write access only for the superuser (root).
                                  Each line in /etc/passwd represents a single user. The first listed is the root (i.e., administrative) account, which has complete power over every aspect of the system. This is followed by system-defined groups and accounts that are required for proper installation and update of system software. The lines at the end represent real people who use the system.




                                  Understanding fields in /etc/passwd

                                  The /etc/passwd contains one entry per line for each user (or user account) of the system. All fields are separated by a colon (:) symbol. Each line contains seven attributes or fields: name, password, user ID, group ID, gecos, home directory and shell. Each attribute is separated from the adjacent attributes by colons but with no spaces. Colons must not be used in the attributes themselves, in order to avoid confusing the system. If there is no data for an attribute, there is no space, but, rather, two consecutive colons.

                                  Generally, passwd file entry looks as follows (click to enlarge image):




                                  1. Username: It is used when user logs in. It should be between 1 and 32 characters in length.


                                  2. Password: An x character indicates that encrypted password is stored in /etc/shadow file.


                                  3. User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-499 are reserved for other predefined accounts. Further UID 500-999 are reserved by system for administrative and system accounts/groups.


                                  4. Group ID (GID): The primary group ID (stored in /etc/group file)


                                  5. User ID Info: The comment field. It allow you to add extra information about the users such as user's full name, phone number etc. This field use by finger command.


                                  6. Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /


                                  7. Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell.






                                  8. Tasks: To see a user list:  $ cat /etc/passwd





                                    Output: -rw-r--r-- 1 root root 2659 Sep 17 01:46 /etc/passwd





                                    Types of "Users" in Linux


                                    There are different types of  accessibility options provided by Linux, from which one can choose respective access as per requirement. Linux provides 5 different alternatives to choose the accessibility.

                                    They are as listed below.


                                    1. Super User or Root User (Like Administrator in Windows) : A special kind of user account which holds all kind’s of permissions to do any alteration to a programs or services of Linux. Especially this kind of  user account is used for  system administration. He can control and limit the access of other User’s. As  all the powers is vested for Root User, care has to be taken before performing each and every modifications, if anything goes wrong, no option left other than crash of  Kernel. It is very sensitive with unlimited accessibilityTake care before performing any step in root.


                                    2.  System User: It is created by default by the OS. This type of user is similar to normal user but got more privileges and access to secure programs which normal user doesn’t got authentication.


                                    3. Normal User : These are the users which have been created by the Root and has limited access to the resources and need permission from Root to access any secure resources and services.


                                    4. Network User (Remote User) : A user that can remotely access the linux machine from a different location. 


                                    5. Pseudo User: It’s a replica of Root User, is used when a user needs the permissions similar to  Root User. Only  Root User can give the access to this user account for others.



                                    6. Every Users got their own identity and authenticity. When we say Identity of the User,UID (User identifier) comes into picture.  UID  is a unique identity, simply a number given to the user.

                                      Every kind of User type as discussed above are given a particular range of UID’s.

                                      Root User got the UID ‘0‘ . {As root user is prime and supreme authoritative, so he got the UID ” 0 “, This can be altered in the configuration file, which we come across later}

                                      UID for System User lies with in the range of to 499. {Don’t come to a conclusion that Linux OS can only provide 500 System Users. It’s just a default range provided by the OS, it can be altered and can add system users greater than 500 but less than a saturation value which depends on a type Linux OS}

                                      UID for Normal User lies with in the range of > 500 <6000.

                                      UID for Network User lies with in the range of  >6000.

                                      To check which type of user are you logged in, simply do the following.

                                      [root@localhost~]# whoami

                                      root




                                      Permissions in Linux



                                      Basic File Permissions

                                      Permission Groups

                                      Each file and directory has three user based permission groups:


                                      • owner - The Owner permissions apply only the owner of the file or directory, they will not impact the actions of other users.


                                      • group - The Group permissions apply only to the group that has been assigned to the file or directory, they will not effect the actions of other users.


                                      • all users - The All Users permissions apply to all other users on the system, this is the permission group that you want to watch the most.



                                      • Permission Types

                                        Each file or directory has three basic permission types:


                                        • read - The Read permission refers to a user's capability to read the contents of the file.


                                        • write - The Write permissions refer to a user's capability to write or modify a file or directory.


                                        • execute - The Execute permission affects a user's capability to execute a file or view the contents of a directory.



                                        • In a normal file there are three permission attributes: read (r), write (w), and execute (x). There is also a fourth character, "-", that means no permission. These permissions are given to a particular user or group. The read permission lets you read data from the file. The write permission lets you write new data to the file. The execute permission lets you run the file as a program. 

                                          To check the permissions, use the ls -l command.  This command will return a list of all of the filenames and their permissions in the current directory.  The beginning of each line will be a string of letters and dashes that tells you the permissions for that file.

                                          For example, after you run the ls -l command, you will see text that is similar to the following line:





                                          drwxr-xr-x 2 user user 4096 Jan 18 5:58 Desktop




                                          d rwx r-x r-x


                                          At the beginning of every line there is a set of characters that contain permission information; it is found within the red square in the image above. The first character will be a "d" for a directory, or a "-" for a regular file or 'l' for a link. The first set of 3 letters indicates permissions for the owner, which is the first name listed (user). The second set of 3 letters indicates the permissions for the group (user). The third set of letters represents the permissions for everyone else.  The example line above would indicate that user has read, write, and execute access, while the group and everyone else have only read and execute access.

                                          To change the file permissions, you must have privileges to write to the file (as you are making changes to the file). The command to change permissions is chmod. With chmod, you need to specify three items: the user or group you are changing, whether you are adding or removing privileges, and which privileges you are adding/removing. 

                                          First, you specify the user or group with the following letters: user (u), group (g), or all users (a).  Next, you use the operator '+'  or '-' to either add or remove privileges, respectively. Lastly, you use the letters r, w, and x to select the new permissions for the file.

                                          For example, to change the permissions for all users to have read, write, and execute permissions, you would execute: chmod a+rwx FileName.  To remove the write privilege from the owning user, you would execute chmod u-w FileName.

                                          You may alternatively use chmod with numbers instead of letters. You would then use the chmod command with a three digit number. The first digit gives the owner's permissions, the second one the group, and the third everyone else's. The number used is an addition of the three possible permissions; 1 for Execute, 2 for Write, 4 for Read. 

                                          For example, the following command would give the user, the group and everyone else Execute, Write and Read permissions: 

                                          chmod 777 FileName





                                          The next example would give Execute, Write and Read permissions to the user (1+2+4), Read and Write to the group (4+2) and Execute to everyone else (1)

                                          chmod 761 FileName



                                          What is vi?



                                          What is vi?
                                          The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate editors for UNIX environments include pico and emacs, a product of GNU.]
                                          The UNIX vi editor is a full screen editor and has three modes of operation:


                                          1. Command mode commands which cause action to be taken on the file, and


                                          2. Insert mode in which entered text is inserted into the file.


                                          3. Execution mode: 


                                          4. In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the <Esc> (Escape) key turns off the Insert mode.

                                            While there are a number of vi commands, just a handful of these is usually sufficient for beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands. The most basic and useful commands are marked with an asterisk (* or star) in the tables below. With practice, these commands should become automatic.
                                            NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase letter; the results will not be what you expect.





                                            To Get Into and Out Of vi


                                            To Start vi


                                            To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created into which you may enter text.





                                            *
                                            vi filename
                                            edit filename starting at line 1



                                            vi -r filename
                                            recover filename that was being edited when system crashed





                                            To Exit vi

                                            Usually the new or modified file is saved when you leave vi. However, it is also possible to quit vi without saving the file.
                                            Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is completed by hitting the <Return> (or <Enter>) key.





                                            *
                                            :x<Return>
                                            quit vi, writing out modified file to file named in original invocation



                                            :wq<Return>
                                            quit vi, writing out modified file to file named in original invocation



                                            :q<Return>
                                            quit (or exit) vi


                                            *
                                            :q!<Return>
                                            quit vi even though latest changes have not been saved for this vi call








                                            Moving the Cursor


                                            Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor screen (or window). You must use the the key commands listed below. On some UNIX platforms, the arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.
                                            If you go back and forth between a PC environment and a UNIX environment, you may find that this dissimilarity in methods for cursor movement is the most frustrating difference between the two.
                                            In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed.





                                            *
                                            j or <Return>
                                              [or down-arrow]
                                            move cursor down one line


                                            *
                                            k [or up-arrow]
                                            move cursor up one line


                                            *
                                            h or <Backspace>
                                              [or left-arrow]
                                            move cursor left one character


                                            *
                                            l or <Space>
                                              [or right-arrow]
                                            move cursor right one character


                                            *
                                            0 (zero)
                                            move cursor to start of current line (the one with the cursor)


                                            *
                                            $
                                            move cursor to end of current line



                                            w
                                            move cursor to beginning of next word



                                            b
                                            move cursor back to beginning of preceding word



                                            :0<Return> or 1G
                                            move cursor to first line in file



                                            :n<Return> or nG
                                            move cursor to line n



                                            :$<Return> or G
                                            move cursor to last line in file








                                            Screen Manipulation


                                            The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.






                                            ^f
                                            move forward one screen



                                            ^b
                                            move backward one screen



                                            ^d
                                            move down (forward) one half screen



                                            ^u
                                            move up (back) one half screen



                                            ^l
                                            redraws the screen



                                            ^r
                                            redraws the screen, removing deleted lines








                                            Adding, Changing, and Deleting Text


                                            Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the commands in the following tables.
                                            Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.





                                            *
                                            u
                                            UNDO WHATEVER YOU JUST DID; a simple toggle





                                            The main purpose of an editor is to create, add, or modify text for a file.


                                            Inserting or Adding Text


                                            The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put the vi editor back into command mode.




                                            *
                                            i
                                            insert text before cursor, until <Esc> hit



                                            I
                                            insert text at beginning of current line, until <Esc> hit


                                            *
                                            a
                                            append text after cursor, until <Esc> hit



                                            A
                                            append text to end of current line, until <Esc> hit


                                            *
                                            o
                                            open and put text in a new line below current line, until <Esc> hit


                                            *
                                            O
                                            open and put text in a new line above current line, until <Esc> hit





                                            Changing Text


                                            The following commands allow you to modify text.




                                            *
                                            r
                                            replace single character under cursor (no <Esc> needed)



                                            R
                                            replace characters, starting with current cursor position, until <Esc> hit



                                            cw
                                            change the current word with new text,
                                            starting with the character under cursor, until <Esc> hit




                                            cNw
                                            change N words beginning with character under cursor, until <Esc> hit;
                                              e.g., c5w changes 5 words




                                            C
                                            change (replace) the characters in the current line, until <Esc> hit



                                            cc
                                            change (replace) the entire current line, stopping when <Esc> is hit



                                            Ncc or cNc
                                            change (replace) the next N lines, starting with the current line,
                                            stopping when <Esc> is hit






                                            Deleting Text


                                            The following commands allow you to delete text.




                                            *
                                            x
                                            delete single character under cursor



                                            Nx
                                            delete N characters, starting with character under cursor



                                            dw
                                            delete the single word beginning with character under cursor



                                            dNw
                                            delete N words beginning with character under cursor;
                                              e.g., d5w deletes 5 words




                                            D
                                            delete the remainder of the line, starting with current cursor position


                                            *
                                            dd
                                            delete entire current line



                                            Ndd or dNd
                                            delete N lines, beginning with the current line;
                                              e.g., 5dd deletes 5 lines






                                            Cutting and Pasting Text


                                            The following commands allow you to copy and paste text.





                                            yy
                                            copy (yank, cut) the current line into the buffer



                                            Nyy or yNy
                                            copy (yank, cut) the next N lines, including the current line, into the buffer



                                            p
                                            put (paste) the line(s) in the buffer into the text after the current line








                                            Other Commands


                                            Searching Text


                                            A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.





                                            /string
                                            search forward for occurrence of string in text



                                            ?string
                                            search backward for occurrence of string in text



                                            n
                                            move to next occurrence of search string



                                            N
                                            move to next occurrence of search string in opposite direction





                                            Determining Line Numbers


                                            Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful.





                                            :.=
                                            returns line number of current line at bottom of screen



                                            :=
                                            returns the total number of lines at bottom of screen



                                            ^g
                                            provides the current line number, along with the total number of lines,
                                            in the file at the bottom of the screen









                                            Saving and Reading Files
                                            These commands permit you to input and output files other than the named file with which you are currently working.





                                            :r filename<Return>
                                            read file named filename and insert after current line
                                            (the line with cursor)




                                            :w<Return>
                                            write current contents to file named in original vi call



                                            :w newfile<Return>
                                            write current contents to a new file named newfile



                                            :12,35w smallfile<Return>
                                            write the contents of the lines numbered 12 through 35 to a new file named smallfile



                                            :w! prevfile<Return>
                                            write current contents over a pre-existing file named prevfile





                                            How to add a user in Linux?


                                            1) adduser:


                                            Syntax is as follows for useradd command:
                                            useradd username

                                            By default user account is locaked, you need to setup a new password:
                                            passwd password

                                            Eg: 

                                            # adduser tom

                                            # passwd tom

                                            List of common options:


                                            • -c comment: Add a comment for the user


                                            • -D : To view default options when adding a user.


                                            • -d home-directory: Create a new home-directory


                                            • -e yyyy-mm-dd: Date for the account to be disabled


                                            • -f days: Number of days after the password expires until the account is disabled. (If 0 is specified, the account is disabled immediately after the password expires. If -1 is specified, the account is not be disabled after the password expires.)


                                            • -g group-name: Primary Group name or group number for the user's default group (group name must exist)


                                            • -G group-list: List of secondary additional (other than default) group names or group numbers, separated by commas, of which the user is a member (group name must exist)


                                            • -m: Create the home directory


                                            • -M: Do not create the home directory


                                            • -s: User's login shell (default /bin/bash)



                                            • 4 different methods to a user 




                                              Method 1: Linux useradd Command — Create User With Default Configurations





                                              Syntax: # useradd LOGIN-NAME








                                              How do I change user name (rename user name) or UID under Linux operating system?

                                              A. Use the usermod command to change user name under Linux. it modifies the system account files to reflect the changes that are specified on the command line.

                                              Syntax is as follows
                                              usermod -l login-name old-name

                                              Where,

                                              => The name of the user will be changed from old-name to login_name. Nothing else is changed. In particular, the user's home directory name should probably be changed to reflect the new login name.

                                              usermod -u UID username

                                              Where,
                                              => The numerical value of the user's ID (UID) . This value must be unique, unless the -o option is used. The value must be non-negative. Values between 0 and 99 are typically reserved for system accounts. Any files which the user owns and which are located in the directory tree rooted at the user's home directory will have the file user ID changed automatically. Files outside of the user's home directory must be altered manually.


                                              Task: Change username from tom to jerry

                                              Type usermod command as follows:
                                              # id tom
                                              # usermod -l jerry tom
                                              # id jerry
                                              # id tom


                                              Task: Change user tom UID from 5001 to 10000

                                              Type usermod command as follows
                                              # id tom
                                              # usermod -u 10000 tom
                                              # id tom