Beginning Linux – Chapter 3 02-14-2014, 07:48 AM
#1
What is in this Chapter?
Invoking Programs.
Mounting and Unmounting FileSystem.
Navigation In The FileSystem.
Managing Processes.
1. Invoking a Program by Using Its Path
When you open a terminal, it in turn executes a command interpreter called the shell. In fact, the command prompt that you see in the terminal is generated by this shell program. The shell outputs this command prompt, and then waits for you to type in a command. When you launch a program from the prompt, the shell searches for it within the file system, and then executes it. You can execute programs by typing out their full path name at the terminal prompt, like this:
Alternatively, you can simply use the program’s name like this:
The second command doesn’t specify the exact location of the ls command, so the shell searches for the program among a specific set of locations. Now, how the shell knows where to look for the program. These locations are specified by a special environment variable, known as the search path.An environment variable consists of a name and a value, and is maintained by the shell for use by programs executed with it. The name of the search path environment variable is $PATH, and you can find out its value by executing the following command:
Shell will return something like this:
As you can see, the value of $PATH consists of a number of directories, separated by “:” character. If you launch a program by typing only its file name (rather than its full path name) then the shell will try to find the program among the directories names in your path.
The value of your $PATH variable will depend on what account you’re using. By default, Linux discourages normal users from executing certain programs, such as the various administrative programs located in the directories /sbin and /usr/sbin that are intended for execution only by the superuser (the root account). The way Linux does this is to omit these directories from any normal user’s $PATH (as in the example above) − so that they cannot invoke these programs without specifying the full path name. Note Even if you do try to invoke an administrative program from a non−root account, you will still be challenged to authenticate yourself as a root user before the program will run. This has the added effect of reminding you that you’re accessing an administrative program, and to take the appropriate care when doing so.
1.1 Finding a Program’s Location
Which command allows you to find the location of programs contained within any of the directories listed in the $PATH variable. In the following example, we use which to find the location of the vim text editor:
returns:
The where is command is also useful for locating programs. This command also allows you to find the location of programs that are not contained in directories listed in $PATH. In the example below, you can see how which and whereis behave differently:
returns
returns
Here, which is unable to locate the ifconfig program because ifconfig is not located within any of the locations specified by the $PATH. However, whereis locates two files by the name of ifconfig: one (/sbin/ifconfig) is the program we’re looking for, and the other is the file that contains the pages of the manual that cover ifconfig.
2.Adding Devices to the FileSystem
In Linux, almost every device is treated as a file. This concept might be a bit unusual to you if you’re more familiar with other non−Unix operating systems like the Windows family, but you’ll quickly get used to it. The idea behind this is to provide a uniform interface to all resources − whether that resource is a hard disk, a serial device, a floppy disk, the sound card, or even the TV tuner! The notable exceptions in this concept are the network devices, which are handled differently by the operating system.
2.1 Mounting a Device
For users coming from a Windows background, there are some notable differences here. In Linux, the different hard disks and their partitions are not considered as different “drives.” Instead, when we need to use such a device, it is mounted to a directory within the file system hierarchy (which is called its mount point). Mounting a disk drive causes its own file system to appear as a child of the directory on which the device is mounted. Then, the content of the disk drive can be accessed by navigating the directory hierarchy, just like any other file in the file system.
For example, suppose we have a floppy disk that contains a single directory, /doc, and that this directory contains a single file called readme.txt. We can make the contents of this floppy disk accessible by mounting the floppy disk (say, to the computer directory /mnt/floppy). Then, the file readme.txt will be accessible using the path /mnt/floppy/doc/readme.txt:
![[Image: mount_IMG_1.png]](http://linuxlounge.info/wp-content/uploads/2014/01/mount_IMG_1.png)
Thus, when the floppy is mounted, it appears to be joined seamlessly to the file system − even though it is an entirely different type of media than the hard disk on which the main file system is situated. By convention, we create mount points in the /mnt directory − for example, we might mount a floppy disk onto /mnt/floppy or a CD−ROM onto /mnt/cdrom.
2.1.1 Mount Points
All partitions are attached to the system via a mount point. The mount point defines the place of a particular data set in the file system. Usually, all partitions are connected through the root partition. On this partition, which is indicated with the slash (/), directories are created. These empty directories will be the starting point of the partitions that are attached to them. An example: given a partition that holds the following directories:
videos/ cd-images/ pictures/
We want to attach this partition in the filesystem in a directory called /opt/media. In order to do this, the system administrator has to make sure that the directory /opt/media exists on the system. Preferably, it should be an empty directory. Then, using the mount command, the administrator can attach the partition to the system. When you look at the content of the formerly empty directory /opt/media, it will contain the files and directories that are on the mounted medium (hard disk or partition of a hard disk, CD, DVD, flash card, USB or other storage device). During system startup, all the partitions are thus mounted, as described in the file /etc/fstab. Some partitions are not mounted by default, for instance if they are not constantly connected to the system, such like the storage used by your digital camera.
If well configured, the device will be mounted as soon as the system notices that it is connected, or it can be user-mountable, i.e. you don’t need to be system administrator to attach and detach the device to and from the system.
In Linux, df is the GNU version, and supports the -h or human readable option which greatly improves readability. Note that commercial UNIX machines commonly have their own versions of df and many other commands. Their behavior is usually the same, though GNU versions of common tools often have more and better features. The df command only displays information about active non-swap partitions. These can include partitions from other networked systems as well.
![[Image: df.png]](http://linuxlounge.info/wp-content/uploads/2014/01/df.png)
2.2 Representation of Devices on the System
Every device on the system is represented by a file in the /dev directory. For example:
The first floppy drive in the computer is represented by the device special file, /dev/fd0
The first master IDE hard disk and CD−ROM is represented by the file /dev/sda and /dev/cdrom respectively
The first audio DSP device of the computer is represented by the file /dev/dsp
The first video capture device is represented by the file /dev/video
Similarly, the first serial port of the computer is /dev/ttyS0
Not all devices are mountable. Only devices that can contain a recognized file system are mountable. For example, floppy, CD−ROM, IDE, and SCSI hard disks generally contain file systems to store their data, and can hence be mounted onto the file system of the computer.
2.3.1 Mounting and Unmounting a Device Automatically
Normally, any Linux Distro can detect media in the disk drives and can mount them automatically. Whenever you insert a data CD into the CD−ROM drive, system will:
Detect it and mount it on the directory /mnt/cdrom
Open a GUI FileSystem explorer, with the contents of this directory displayed in it
Create a desktop shortcut button for quick access to the contents of the CD
For unmounting just right click on the CDROM device and click on eject.
2.3.2 Mounting and Unmounting from the Command Line
You can also perform the tasks of mounting and unmounting from the command line. To mount a CD−ROM onto the file system, you can use the mount program. The mount program needs to be told the device that has to be mounted, the file system that is present on it, and the mount point (the point in the existing file system where the device needs to be mounted). For example, the following command takes the CD contained in the CD−ROM drive /dev/cdrom, and mounts it at the mount point /mnt/cdrom:
Some of the parameters for mounting a CD in a Red Hat Linux desktop are pre−configured and stored in the system configuration file /etc/fstab. Therefore, you will need to execute only the following command to mount the CD−ROM on the mount point /mnt/cdrom:
To mount the CD−ROM on a mount point other than /mnt/cdrom, then you will have to give all the information to the mount program. For example, to mount the CD−ROM on the mount point /mnt/cdrom1, you need to execute the following command:
This instructs the mount program to mount the CD−ROM device /dev/cdrom onto the mount point /mnt/cdrom1. It also specifies the FileSystem to be iso9660, which is present on most standard CD−ROM media.
Note: You can find all the parameters that can be passed to the mount program in the corresponding manual page by executing the command man mount at the terminal.
To unmount a devices from the file system, you need to execute the umount program. The umount program takes only one parameter − the mount point of the device. For example, the following command unmounts the CD−ROM disc from its mount point:
Note: For unmounting a device from the FileSystem, it is absolutely necessary that no application is currently using the device FileSystem data. If the device is being used at the time of the unmount process, the process will be unable to unmount the device you specified.
3.Navigating in the File System
This section will help you navigate the file system and all the files in your computer. The file system can be navigated using either a GUI file explorer (such as Nautilus) or the terminal.
3.1 Navigating Using the GUI
For navigating using the GUI, just use the inbuilt file explorer of your system. The file explorer may be under:
System Tools –> File Browser
or something like that.
3.2 Navigating using the Terminal Emulator
Opening up a terminal emulator window places you at the shell prompt, and in your home directory. As we’ve already noted, the shell is the program that interprets each of the commands that you type at the terminal. The directory that you are located in at any particular time is called the current directory (or present working directory). When you first open up the terminal, your current directory is set to be your home directory. At any time, you can find out which directory that is by executing the command pwd:
This example shows the pwd program being used to confirm that the current directory just after opening the terminal application is the user’s home directory.
3.3 Displaying the Contents of a Directory
To display the contents of a given directory, we can use the ls program. If the ls program is executed without any arguments, it displays the files in the present working directory. If you try this in your home directory, just after installation (before you’ve created any new files in your home directory), the output of the ls program will give no output (to reflect the lack of content in the directory). However, you should note that the default behavior of the ls program is to not display the hidden files in a directory. Hidden files are generally used to store user−specific configuration data, and you can usually spot a hidden file because its name begins with the ‘.’ (dot) character. To display these files you need to use the −a parameter with the ls command. The following example shows how the −a parameter affects the output of the ls command:
The lack of output from the first command suggests that the directory is empty. However, the output from the second command reveals the existence of a number of hidden files.
3.4 Changing the Current Directory
To change the present working directory to a different directory, you need to use the cd command. Executing this command without any parameters changes the working directory to be the user’s home directory. Adding a directory name as the parameter causes the working directory to be the one specified. These can be understood from the terminal session shown below:
Here, the first cd command changes the current directory to be /usr/bin (as confirmed by the subsequent pwd command). The second cd command changes the current directory to be this user’s home directory (which happens to be /home/shailendra).
4.Searching for Files
There are two different CLI programs that enable us to search for files − locate and find:
The locate program works by building a daily database of files and their respective locations. When a search is conducted, it simply looks up this database and reports the file locations. (However, this database is updated once a day and may not therefore reflect the actual contents of the file system if changes have happened in the last 24 hours.)
The find program, by contrast, doesn’t use any database and actually looks inside directories and their subdirectories for files matching the given criterion.
The find program is more powerful because of the wealth of search criteria that it allows in searches. You can see both these commands being used to find a file named pam.ps in the example given below:
Searching a file is also possible in GUI mode, inside the file explorer just like in Windows.
5.Managing Processes
To manage a desktop and control its resources, it is very important to grasp the concepts behind processes running in the computer. You can think of a process simply as an independent program entity, executing and using computer resources such as CPU time and memory. Any application that is executed is started as an independent process. An application may start its own child processes − and indeed many applications like web servers and database servers have multiple processes to tend to the needs of different clients at the same time.
5.1 Understanding Processes
Normally, a process is started by a parent process. The newly launched process can in turn launch child processes if it wants to. When you are working at the desktop, any new program is executed as a child process of the program that is receiving and interpreting your commands. For example, when you are at the terminal prompt, you are interacting with the shell interpreter program (by default, this is bash). The shell itself is running as a process in the computer. Any program that you execute at the bash prompt is launched as a child process of the bash program.
In fact, every executing program on the computer is a process that has been launched by some other parent process − and hence, all the running processes in the computer can be represented in the form of a hierarchical tree. There is only one exception − the first process of the computer. This is the init program, which is launched by the operating system itself just after booting. Thereafter init forks off child processes which in turn may fork off other child processes of their own.
Note:
1) Because of the underlying manner in which new processes are launched, the process of launching a child process in the UNIX environment is also frequently referred to as forking.
2) When launching a child process, the parent process makes a copy of itself by forking, and then executes the child program in the memory space of the new copy.
5.2 Understanding Run Levels
When the computer is started up, the operating system loads and starts off the first process − the init process. The init process then starts off any required sub−processes before the computer is ready to be used by the user. The processes that the operating system needs to start off after the booting process are specified using run levels. A run level is a state of the machine, which determines the processes to be run. There are seven run levels − numbered from 0 to 6 − and they are described below:
Run level 0 signifies the halted state of a machine. While changing run levels, if you set the new run level to be 0, it effectively halts the machine.
Run level 1 stands for the single user mode. This brings the machine to a super user mode and disallows external users from using the machine. All networking functions are disabled at this level. This level is also called the system maintenance mode because it is generally used to recover from serious system problems. An important thing to note is that at this level, the root prompt is displayed without any attempt to check the corresponding password. This is necessary to allow for the various maintenance functions that need to be done without the hindrance of an authentication system; however, it also leaves the system open to any malicious users physically present at the computer. Therefore, this run level should be used only when absolutely necessary.
Run level 2 allows multiple users to log in to the machine through virtual terminals and other login devices, but still doesn’t activate any networking functions.
Run level 3 finally allows networking processes to be started, and allows the complete resources of the system to be used effectively. However, until this level the interface to the desktop is through a CLI interface. Since the GUI interface uses the computer resources (such as CPU, memory, and hard disk) intensively, this run level is mainly used by server class workstations where precious computer resources are used to deliver networking and other application services more effectively.
Run level 4 is unused. It can be used to define your own custom run level.
Run level 5 finally allows the X−server process to be started and the accompanying desktop to be loaded to allow users to use the system with a GUI. The desktop that you have been using until now has been working at run level 5.
Run level 6 signifies the rebooting state of a machine and is used to restart the machine.
When a machine is booted, the init program examines the contents of the configuration file /etc/inittab to determine the run level to boot the computer to, and executes all the processes for that run level. The processes to execute for a particular runlevel are specified as special program scripts in the directory /etc/rcx.d/ where x is the run level to boot to.
The present run level of the computer can be found out by using the program runlevel. This outputs two numbers, which represent the previous and current run levels. The current run level can be changed using the program telinit. This program needs to be executed as root, and takes a single argument − the run level to boot to. For example, to reboot the system you can execute the command telinit 6.
5.3 Managing Processes Using Terminal
Top Command:
The easiest way to find out what processes are running on your server is to run the
top command.
![[Image: top-1024x575.png]](http://linuxlounge.info/wp-content/uploads/2014/01/top-1024x575.png)
Htop Command:
Visit this link for how to install Htop – http://wp.me/p4eV36-1q
Now run the htop command:
![[Image: htop-1024x575.png]](http://linuxlounge.info/wp-content/uploads/2014/01/htop-1024x575.png)
5.3.1 How To Use ps to List Processes
Both top and htop provide a nice interface to view running processes similar to a graphical task manager.However, these tools are not always flexible enough to adequately cover all scenarios. A powerful command called ps is often the answer to these problems. When called without arguments, the output can be a bit lack-luster:
![[Image: psnoargs-1024x560.png]](http://linuxlounge.info/wp-content/uploads/2014/01/psnoargs-1024x560.png)
This output shows all of the processes associated with the current user and terminal session. This makes sense because we are only running bash and ps with this terminal currently. To get a more complete picture of the processes on this system, we can run the following:
![[Image: psargs1-1024x613.png]](http://linuxlounge.info/wp-content/uploads/2014/01/psargs1-1024x613.png)
These options tell ps to show processes owned by all users (regardless of their terminal association) in a user-friendly format. To see a tree view, where hierarchal relationships are illustrated, we can run the command with these options:
![[Image: psargs2-1024x613.png]](http://linuxlounge.info/wp-content/uploads/2014/01/psargs2-1024x613.png)
5.3.2 Getting ID Of any Process (PID)
In Linux and Unix-like systems, each process is assigned a process ID, or PID. This is how the operating system identifies and keeps track of processes. A quick way of getting the PID of a process is with the pgrep command:
This will simply query the process ID and return it. The first process spawned at boot, called init, is given the PID of “1″.
returns:
This process is then responsible for spawning every other process on the system. The later processes are given larger PID numbers. A process’s parent is the process that was responsible for spawning it. If a process’s parent is killed, then the child processes also die. The parent process’s PID is referred to as the PPID. You can see PID and PPID in the column headers in many process management applications, including top, htop and ps. Any communication between the user and the operating system about processes involves translating between process names and PIDs at some point during the operation. This is why utilities tell you the PID.
5.4 How To Send Processes Signals in Linux
All processes in Linux respond to signals. Signals are an os-level way of telling programs to terminate or modify their behavior. The most common way of passing signals to a program is with the kill command. As you might expect, the default functionality of this utility is to attempt to kill a process:
This sends the TERM signal to the process. The TERM signal tells the process to please terminate. This allows the program to perform clean-up operations and exit smoothly. If the program is misbehaving and does not exit when given the TERM signal, we can escalate the signal by passing the KILL signal:
This is a special signal that is not sent to the program. Instead, it is given to the operating system kernel, which shuts down the process. This is used to bypass programs that ignore the signals sent to them. Each signal has an associated number that can be passed instead of the name. For instance, You can pass “-15″ instead of “-TERM”, and “-9″ instead of “-KILL”. How To Use Signals For Other Purposes.
Signals are not only used to shut down programs. They can also be used to perform other actions. For instance, many daemons will restart when they are given the HUP, or hang-up signal. Apache is one program that operates like this.
The above command will cause Apache to reload its configuration file and resume serving content. You can list all of the signals that are possible to send with kill by typing:
returns:
5.4.1 How To Send Processes Signals by Name
Although the conventional way of sending signals is through the use of PIDs, there are also methods of doing this with regular process names. The pkill command works in almost exactly the same way as kill, but it operates on a process name instead:
The above command is the equivalent of:
If you would like to send a signal to every instance of a certain process, you can use the killall command:
The above command will send the TERM signal to every instance of firefox running on the computer.
6. Managing Permissions in Linux
In Linux a more strict arrangement of permissions are there, that describe what each user can and cannot do. One user’s processes and data can’t be accessed or modified by other users of the system, unless they are specifically permitted to do so. This concept of user security runs through the heart of the Linux system.
As we discussed earlier in the chapter, the root (or super user) of the computer has complete access over all the resources of the computer. For this reason, various security restrictions are put in place in the system to ensure that this user privilege is not compromised. Even as the administrator of the system, you should attempt to do most of your work as a less privileged user, and assume super user privileges only when they’re needed.
Many new users will also find it unusual that the Linux desktop allows many users to work on the system simultaneously. While only one user can work physically at the desktop, other users can log in through network services like telnet and its more secure equivalent, ssh. Linux has a simple way of organizing users. Every user can be the part of one or more groups. Therefore, as well as securing a resource by applying permissions to individual users, you can also place users into a group and apply permissions to the group instead.
6.1 Understanding Permissions
We have already talked about how privileges are required to control processes of other users; the same applies to files and directories. Every file and directory is associated with a single user (its owner) and also with a group. Then, specific permissions to access a file are given to the three classes of users:
The owner
The group members
Everybody else
Each of the above classes is allowed a combination of three kinds of access permissions:
![[Image: managepermission1.png]](http://linuxlounge.info/wp-content/uploads/2014/01/managepermission1.png)
The permissions of a given file can be found out using the ls program. In the example given below, we use the command ls −l to display a “long” version of the file information:
returns:
The read, write, and executable permissions are represented using the characters r, w, and x respectively. The first part of the file information (in the above example, the expression −r−xr−xr−x) indicates the permission of the file. The first character indicates the file type (directory, character device, and so on), but it is the remaining characters that we’re interested in right now. This sequence consists of three sets of three characters − one set each to describe permissions of the owner, the group, and the others. For example, the sequence r−x indicates that only the read and execution permissions are set, and no write permission is available.
Therefore the permission of this file specifies that all possible users can only read and execute the content of the file but not modify the file contents. You can use the CLI tools chown, chmod, and chgrp to modify the permissions of files and directories in the system which we will see in other chapters.
Invoking Programs.
Mounting and Unmounting FileSystem.
Navigation In The FileSystem.
Managing Processes.
1. Invoking a Program by Using Its Path
When you open a terminal, it in turn executes a command interpreter called the shell. In fact, the command prompt that you see in the terminal is generated by this shell program. The shell outputs this command prompt, and then waits for you to type in a command. When you launch a program from the prompt, the shell searches for it within the file system, and then executes it. You can execute programs by typing out their full path name at the terminal prompt, like this:
Code:
$ /bin/lsAlternatively, you can simply use the program’s name like this:
Code:
$ lsThe second command doesn’t specify the exact location of the ls command, so the shell searches for the program among a specific set of locations. Now, how the shell knows where to look for the program. These locations are specified by a special environment variable, known as the search path.An environment variable consists of a name and a value, and is maintained by the shell for use by programs executed with it. The name of the search path environment variable is $PATH, and you can find out its value by executing the following command:
Code:
$ echo $PATHShell will return something like this:
Code:
/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbinAs you can see, the value of $PATH consists of a number of directories, separated by “:” character. If you launch a program by typing only its file name (rather than its full path name) then the shell will try to find the program among the directories names in your path.
The value of your $PATH variable will depend on what account you’re using. By default, Linux discourages normal users from executing certain programs, such as the various administrative programs located in the directories /sbin and /usr/sbin that are intended for execution only by the superuser (the root account). The way Linux does this is to omit these directories from any normal user’s $PATH (as in the example above) − so that they cannot invoke these programs without specifying the full path name. Note Even if you do try to invoke an administrative program from a non−root account, you will still be challenged to authenticate yourself as a root user before the program will run. This has the added effect of reminding you that you’re accessing an administrative program, and to take the appropriate care when doing so.
1.1 Finding a Program’s Location
Which command allows you to find the location of programs contained within any of the directories listed in the $PATH variable. In the following example, we use which to find the location of the vim text editor:
Code:
$ which vimreturns:
Code:
/usr/bin/vimCode:
$ which ifconfigreturns
Code:
/usr/sbin/which: no ifconfig in /usr/local/bin:/usr/bin:/bin:home/sandipb/binCode:
$ whereis ifconfigreturns
Code:
ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz2.Adding Devices to the FileSystem
In Linux, almost every device is treated as a file. This concept might be a bit unusual to you if you’re more familiar with other non−Unix operating systems like the Windows family, but you’ll quickly get used to it. The idea behind this is to provide a uniform interface to all resources − whether that resource is a hard disk, a serial device, a floppy disk, the sound card, or even the TV tuner! The notable exceptions in this concept are the network devices, which are handled differently by the operating system.
2.1 Mounting a Device
For users coming from a Windows background, there are some notable differences here. In Linux, the different hard disks and their partitions are not considered as different “drives.” Instead, when we need to use such a device, it is mounted to a directory within the file system hierarchy (which is called its mount point). Mounting a disk drive causes its own file system to appear as a child of the directory on which the device is mounted. Then, the content of the disk drive can be accessed by navigating the directory hierarchy, just like any other file in the file system.
For example, suppose we have a floppy disk that contains a single directory, /doc, and that this directory contains a single file called readme.txt. We can make the contents of this floppy disk accessible by mounting the floppy disk (say, to the computer directory /mnt/floppy). Then, the file readme.txt will be accessible using the path /mnt/floppy/doc/readme.txt:
![[Image: mount_IMG_1.png]](http://linuxlounge.info/wp-content/uploads/2014/01/mount_IMG_1.png)
Thus, when the floppy is mounted, it appears to be joined seamlessly to the file system − even though it is an entirely different type of media than the hard disk on which the main file system is situated. By convention, we create mount points in the /mnt directory − for example, we might mount a floppy disk onto /mnt/floppy or a CD−ROM onto /mnt/cdrom.
2.1.1 Mount Points
All partitions are attached to the system via a mount point. The mount point defines the place of a particular data set in the file system. Usually, all partitions are connected through the root partition. On this partition, which is indicated with the slash (/), directories are created. These empty directories will be the starting point of the partitions that are attached to them. An example: given a partition that holds the following directories:
videos/ cd-images/ pictures/
We want to attach this partition in the filesystem in a directory called /opt/media. In order to do this, the system administrator has to make sure that the directory /opt/media exists on the system. Preferably, it should be an empty directory. Then, using the mount command, the administrator can attach the partition to the system. When you look at the content of the formerly empty directory /opt/media, it will contain the files and directories that are on the mounted medium (hard disk or partition of a hard disk, CD, DVD, flash card, USB or other storage device). During system startup, all the partitions are thus mounted, as described in the file /etc/fstab. Some partitions are not mounted by default, for instance if they are not constantly connected to the system, such like the storage used by your digital camera.
If well configured, the device will be mounted as soon as the system notices that it is connected, or it can be user-mountable, i.e. you don’t need to be system administrator to attach and detach the device to and from the system.
In Linux, df is the GNU version, and supports the -h or human readable option which greatly improves readability. Note that commercial UNIX machines commonly have their own versions of df and many other commands. Their behavior is usually the same, though GNU versions of common tools often have more and better features. The df command only displays information about active non-swap partitions. These can include partitions from other networked systems as well.
Code:
$ df -h![[Image: df.png]](http://linuxlounge.info/wp-content/uploads/2014/01/df.png)
2.2 Representation of Devices on the System
Every device on the system is represented by a file in the /dev directory. For example:
The first floppy drive in the computer is represented by the device special file, /dev/fd0
The first master IDE hard disk and CD−ROM is represented by the file /dev/sda and /dev/cdrom respectively
The first audio DSP device of the computer is represented by the file /dev/dsp
The first video capture device is represented by the file /dev/video
Similarly, the first serial port of the computer is /dev/ttyS0
Not all devices are mountable. Only devices that can contain a recognized file system are mountable. For example, floppy, CD−ROM, IDE, and SCSI hard disks generally contain file systems to store their data, and can hence be mounted onto the file system of the computer.
2.3.1 Mounting and Unmounting a Device Automatically
Normally, any Linux Distro can detect media in the disk drives and can mount them automatically. Whenever you insert a data CD into the CD−ROM drive, system will:
Detect it and mount it on the directory /mnt/cdrom
Open a GUI FileSystem explorer, with the contents of this directory displayed in it
Create a desktop shortcut button for quick access to the contents of the CD
For unmounting just right click on the CDROM device and click on eject.
2.3.2 Mounting and Unmounting from the Command Line
You can also perform the tasks of mounting and unmounting from the command line. To mount a CD−ROM onto the file system, you can use the mount program. The mount program needs to be told the device that has to be mounted, the file system that is present on it, and the mount point (the point in the existing file system where the device needs to be mounted). For example, the following command takes the CD contained in the CD−ROM drive /dev/cdrom, and mounts it at the mount point /mnt/cdrom:
Code:
$ mount −t iso9660 /dev/cdrom /mnt/cdromSome of the parameters for mounting a CD in a Red Hat Linux desktop are pre−configured and stored in the system configuration file /etc/fstab. Therefore, you will need to execute only the following command to mount the CD−ROM on the mount point /mnt/cdrom:
Code:
# mount /mnt/cdromTo mount the CD−ROM on a mount point other than /mnt/cdrom, then you will have to give all the information to the mount program. For example, to mount the CD−ROM on the mount point /mnt/cdrom1, you need to execute the following command:
Code:
$ mount −t iso9660 /dev/cdrom /mnt/cdrom1This instructs the mount program to mount the CD−ROM device /dev/cdrom onto the mount point /mnt/cdrom1. It also specifies the FileSystem to be iso9660, which is present on most standard CD−ROM media.
Note: You can find all the parameters that can be passed to the mount program in the corresponding manual page by executing the command man mount at the terminal.
To unmount a devices from the file system, you need to execute the umount program. The umount program takes only one parameter − the mount point of the device. For example, the following command unmounts the CD−ROM disc from its mount point:
Code:
$ umount /mnt/cdromNote: For unmounting a device from the FileSystem, it is absolutely necessary that no application is currently using the device FileSystem data. If the device is being used at the time of the unmount process, the process will be unable to unmount the device you specified.
3.Navigating in the File System
This section will help you navigate the file system and all the files in your computer. The file system can be navigated using either a GUI file explorer (such as Nautilus) or the terminal.
3.1 Navigating Using the GUI
For navigating using the GUI, just use the inbuilt file explorer of your system. The file explorer may be under:
System Tools –> File Browser
or something like that.
3.2 Navigating using the Terminal Emulator
Opening up a terminal emulator window places you at the shell prompt, and in your home directory. As we’ve already noted, the shell is the program that interprets each of the commands that you type at the terminal. The directory that you are located in at any particular time is called the current directory (or present working directory). When you first open up the terminal, your current directory is set to be your home directory. At any time, you can find out which directory that is by executing the command pwd:
Code:
$ pwdCode:
/home/shailendraThis example shows the pwd program being used to confirm that the current directory just after opening the terminal application is the user’s home directory.
3.3 Displaying the Contents of a Directory
To display the contents of a given directory, we can use the ls program. If the ls program is executed without any arguments, it displays the files in the present working directory. If you try this in your home directory, just after installation (before you’ve created any new files in your home directory), the output of the ls program will give no output (to reflect the lack of content in the directory). However, you should note that the default behavior of the ls program is to not display the hidden files in a directory. Hidden files are generally used to store user−specific configuration data, and you can usually spot a hidden file because its name begins with the ‘.’ (dot) character. To display these files you need to use the −a parameter with the ls command. The following example shows how the −a parameter affects the output of the ls command:
Code:
$ lsCode:
$ ls −aCode:
.bash_logout .bash_profile .bashrc .emacs .gtkrc .xauthkIUlWuThe lack of output from the first command suggests that the directory is empty. However, the output from the second command reveals the existence of a number of hidden files.
3.4 Changing the Current Directory
To change the present working directory to a different directory, you need to use the cd command. Executing this command without any parameters changes the working directory to be the user’s home directory. Adding a directory name as the parameter causes the working directory to be the one specified. These can be understood from the terminal session shown below:
Code:
$ cd /usr/binCode:
$ pwdCode:
/usr/binCode:
$ cdCode:
$ pwdCode:
/home/shailendraHere, the first cd command changes the current directory to be /usr/bin (as confirmed by the subsequent pwd command). The second cd command changes the current directory to be this user’s home directory (which happens to be /home/shailendra).
4.Searching for Files
There are two different CLI programs that enable us to search for files − locate and find:
The locate program works by building a daily database of files and their respective locations. When a search is conducted, it simply looks up this database and reports the file locations. (However, this database is updated once a day and may not therefore reflect the actual contents of the file system if changes have happened in the last 24 hours.)
The find program, by contrast, doesn’t use any database and actually looks inside directories and their subdirectories for files matching the given criterion.
The find program is more powerful because of the wealth of search criteria that it allows in searches. You can see both these commands being used to find a file named pam.ps in the example given below:
Code:
$ locate pam.psCode:
/usr/share/doc/pam−0.75/ps/pam.psCode:
$ find /usr/share/doc −name pam.psCode:
/usr/share/doc/pam−0.75/ps/pam.psSearching a file is also possible in GUI mode, inside the file explorer just like in Windows.
5.Managing Processes
To manage a desktop and control its resources, it is very important to grasp the concepts behind processes running in the computer. You can think of a process simply as an independent program entity, executing and using computer resources such as CPU time and memory. Any application that is executed is started as an independent process. An application may start its own child processes − and indeed many applications like web servers and database servers have multiple processes to tend to the needs of different clients at the same time.
5.1 Understanding Processes
Normally, a process is started by a parent process. The newly launched process can in turn launch child processes if it wants to. When you are working at the desktop, any new program is executed as a child process of the program that is receiving and interpreting your commands. For example, when you are at the terminal prompt, you are interacting with the shell interpreter program (by default, this is bash). The shell itself is running as a process in the computer. Any program that you execute at the bash prompt is launched as a child process of the bash program.
In fact, every executing program on the computer is a process that has been launched by some other parent process − and hence, all the running processes in the computer can be represented in the form of a hierarchical tree. There is only one exception − the first process of the computer. This is the init program, which is launched by the operating system itself just after booting. Thereafter init forks off child processes which in turn may fork off other child processes of their own.
Note:
1) Because of the underlying manner in which new processes are launched, the process of launching a child process in the UNIX environment is also frequently referred to as forking.
2) When launching a child process, the parent process makes a copy of itself by forking, and then executes the child program in the memory space of the new copy.
5.2 Understanding Run Levels
When the computer is started up, the operating system loads and starts off the first process − the init process. The init process then starts off any required sub−processes before the computer is ready to be used by the user. The processes that the operating system needs to start off after the booting process are specified using run levels. A run level is a state of the machine, which determines the processes to be run. There are seven run levels − numbered from 0 to 6 − and they are described below:
Run level 0 signifies the halted state of a machine. While changing run levels, if you set the new run level to be 0, it effectively halts the machine.
Run level 1 stands for the single user mode. This brings the machine to a super user mode and disallows external users from using the machine. All networking functions are disabled at this level. This level is also called the system maintenance mode because it is generally used to recover from serious system problems. An important thing to note is that at this level, the root prompt is displayed without any attempt to check the corresponding password. This is necessary to allow for the various maintenance functions that need to be done without the hindrance of an authentication system; however, it also leaves the system open to any malicious users physically present at the computer. Therefore, this run level should be used only when absolutely necessary.
Run level 2 allows multiple users to log in to the machine through virtual terminals and other login devices, but still doesn’t activate any networking functions.
Run level 3 finally allows networking processes to be started, and allows the complete resources of the system to be used effectively. However, until this level the interface to the desktop is through a CLI interface. Since the GUI interface uses the computer resources (such as CPU, memory, and hard disk) intensively, this run level is mainly used by server class workstations where precious computer resources are used to deliver networking and other application services more effectively.
Run level 4 is unused. It can be used to define your own custom run level.
Run level 5 finally allows the X−server process to be started and the accompanying desktop to be loaded to allow users to use the system with a GUI. The desktop that you have been using until now has been working at run level 5.
Run level 6 signifies the rebooting state of a machine and is used to restart the machine.
When a machine is booted, the init program examines the contents of the configuration file /etc/inittab to determine the run level to boot the computer to, and executes all the processes for that run level. The processes to execute for a particular runlevel are specified as special program scripts in the directory /etc/rcx.d/ where x is the run level to boot to.
The present run level of the computer can be found out by using the program runlevel. This outputs two numbers, which represent the previous and current run levels. The current run level can be changed using the program telinit. This program needs to be executed as root, and takes a single argument − the run level to boot to. For example, to reboot the system you can execute the command telinit 6.
5.3 Managing Processes Using Terminal
Top Command:
The easiest way to find out what processes are running on your server is to run the
top command.
Code:
top![[Image: top-1024x575.png]](http://linuxlounge.info/wp-content/uploads/2014/01/top-1024x575.png)
Htop Command:
Visit this link for how to install Htop – http://wp.me/p4eV36-1q
Now run the htop command:
Code:
htop![[Image: htop-1024x575.png]](http://linuxlounge.info/wp-content/uploads/2014/01/htop-1024x575.png)
5.3.1 How To Use ps to List Processes
Both top and htop provide a nice interface to view running processes similar to a graphical task manager.However, these tools are not always flexible enough to adequately cover all scenarios. A powerful command called ps is often the answer to these problems. When called without arguments, the output can be a bit lack-luster:
Code:
ps![[Image: psnoargs-1024x560.png]](http://linuxlounge.info/wp-content/uploads/2014/01/psnoargs-1024x560.png)
This output shows all of the processes associated with the current user and terminal session. This makes sense because we are only running bash and ps with this terminal currently. To get a more complete picture of the processes on this system, we can run the following:
Code:
ps aux![[Image: psargs1-1024x613.png]](http://linuxlounge.info/wp-content/uploads/2014/01/psargs1-1024x613.png)
These options tell ps to show processes owned by all users (regardless of their terminal association) in a user-friendly format. To see a tree view, where hierarchal relationships are illustrated, we can run the command with these options:
Code:
ps axjf![[Image: psargs2-1024x613.png]](http://linuxlounge.info/wp-content/uploads/2014/01/psargs2-1024x613.png)
5.3.2 Getting ID Of any Process (PID)
In Linux and Unix-like systems, each process is assigned a process ID, or PID. This is how the operating system identifies and keeps track of processes. A quick way of getting the PID of a process is with the pgrep command:
Code:
$ pgrep bashCode:
1017This will simply query the process ID and return it. The first process spawned at boot, called init, is given the PID of “1″.
Code:
$ pgrep initreturns:
Code:
1This process is then responsible for spawning every other process on the system. The later processes are given larger PID numbers. A process’s parent is the process that was responsible for spawning it. If a process’s parent is killed, then the child processes also die. The parent process’s PID is referred to as the PPID. You can see PID and PPID in the column headers in many process management applications, including top, htop and ps. Any communication between the user and the operating system about processes involves translating between process names and PIDs at some point during the operation. This is why utilities tell you the PID.
5.4 How To Send Processes Signals in Linux
All processes in Linux respond to signals. Signals are an os-level way of telling programs to terminate or modify their behavior. The most common way of passing signals to a program is with the kill command. As you might expect, the default functionality of this utility is to attempt to kill a process:
Code:
$ kill PID_of_target_processThis sends the TERM signal to the process. The TERM signal tells the process to please terminate. This allows the program to perform clean-up operations and exit smoothly. If the program is misbehaving and does not exit when given the TERM signal, we can escalate the signal by passing the KILL signal:
Code:
$ kill -KILL PID_of_target_processThis is a special signal that is not sent to the program. Instead, it is given to the operating system kernel, which shuts down the process. This is used to bypass programs that ignore the signals sent to them. Each signal has an associated number that can be passed instead of the name. For instance, You can pass “-15″ instead of “-TERM”, and “-9″ instead of “-KILL”. How To Use Signals For Other Purposes.
Signals are not only used to shut down programs. They can also be used to perform other actions. For instance, many daemons will restart when they are given the HUP, or hang-up signal. Apache is one program that operates like this.
Code:
sudo kill -HUP pid_of_apacheThe above command will cause Apache to reload its configuration file and resume serving content. You can list all of the signals that are possible to send with kill by typing:
Code:
kill -lCode:
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM . . .5.4.1 How To Send Processes Signals by Name
Although the conventional way of sending signals is through the use of PIDs, there are also methods of doing this with regular process names. The pkill command works in almost exactly the same way as kill, but it operates on a process name instead:
Code:
$ pkill -9 pingThe above command is the equivalent of:
Code:
$ kill -9 `pgrep ping`If you would like to send a signal to every instance of a certain process, you can use the killall command:
Code:
$ killall firefoxThe above command will send the TERM signal to every instance of firefox running on the computer.
6. Managing Permissions in Linux
In Linux a more strict arrangement of permissions are there, that describe what each user can and cannot do. One user’s processes and data can’t be accessed or modified by other users of the system, unless they are specifically permitted to do so. This concept of user security runs through the heart of the Linux system.
As we discussed earlier in the chapter, the root (or super user) of the computer has complete access over all the resources of the computer. For this reason, various security restrictions are put in place in the system to ensure that this user privilege is not compromised. Even as the administrator of the system, you should attempt to do most of your work as a less privileged user, and assume super user privileges only when they’re needed.
Many new users will also find it unusual that the Linux desktop allows many users to work on the system simultaneously. While only one user can work physically at the desktop, other users can log in through network services like telnet and its more secure equivalent, ssh. Linux has a simple way of organizing users. Every user can be the part of one or more groups. Therefore, as well as securing a resource by applying permissions to individual users, you can also place users into a group and apply permissions to the group instead.
6.1 Understanding Permissions
We have already talked about how privileges are required to control processes of other users; the same applies to files and directories. Every file and directory is associated with a single user (its owner) and also with a group. Then, specific permissions to access a file are given to the three classes of users:
The owner
The group members
Everybody else
Each of the above classes is allowed a combination of three kinds of access permissions:
![[Image: managepermission1.png]](http://linuxlounge.info/wp-content/uploads/2014/01/managepermission1.png)
The permissions of a given file can be found out using the ls program. In the example given below, we use the command ls −l to display a “long” version of the file information:
Code:
$ ls −l /usr/bin/topreturns:
Code:
−r−xr−xr−x 1 root root 56406 Aug 12 15:50 /usr/bin/topThe read, write, and executable permissions are represented using the characters r, w, and x respectively. The first part of the file information (in the above example, the expression −r−xr−xr−x) indicates the permission of the file. The first character indicates the file type (directory, character device, and so on), but it is the remaining characters that we’re interested in right now. This sequence consists of three sets of three characters − one set each to describe permissions of the owner, the group, and the others. For example, the sequence r−x indicates that only the read and execution permissions are set, and no write permission is available.
Therefore the permission of this file specifies that all possible users can only read and execute the content of the file but not modify the file contents. You can use the CLI tools chown, chmod, and chgrp to modify the permissions of files and directories in the system which we will see in other chapters.
Visit LinuxLounge, If You Want To Thank Me For The Tutorial.


![[Image: znALY5V.jpg]](https://i.imgur.com/znALY5V.jpg)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)





![[Image: R5aCcWV.png]](http://i.imgur.com/R5aCcWV.png)















![[Image: dHJ4Beo.gif]](http://i.imgur.com/dHJ4Beo.gif)