Saturday, November 15, 2008

LINUX INTRODUCTION

Convention: Words, such as filenames, commands etc, are put within
square brackets to separate them from lines of text. The square
brackets have no other meaning.

Experiments in this laboratory are to be carried out in Debian Linux
environment. An account for user [sumit] is provided for your initial
login. Password of [sumit] is [ sumit ]. You have to create an account
for yourself for future logins.

When [ sumit ] logs in, the operating system ( henceforth called OS )
creates a process called [ shell ], that acts as an interface between
[sumit] and the OS. There can be different types of [shell] processes.
The file [/etc/shells] lists the different [shell] processes available
in your PC. Bourne-Again SHell ( BASH ) is the default. The default
[shell] is configurable. [sumit] may change to another type of [shell]
if required, during a login session.

In [ /etc/passwd ] file there is an entry for [sumit]. This entry has
seven fields separated by colons. The last field specifies the default
shell of [sumit].

The [shell] creates a prompt for [sumit] and waits for a command from
[sumit]. When [ sumit ] enters a command, OS executes the command and
sends back the output ( if any ) to the shell.

Execute a command

$ cal


OS returns an error message if the command is invalid

$ aboltabol

As the [shell] accepts commands from user and returns the result, the
[shell] is also called command interpreter.

Linux commands and filenames are case sensitive. [cal], [Cal], [caL]
are all different.

[ sumit ] may use a large number of commands while within the [shell].
Manuals for almost all these commands are available in your Linux PC,
in "UNIX Programmer's Manual" . The manual is organised in several
sections. Executable programs or shell commands are grouped together
in section 1 of the manual. You may read the manual of a command using
[man] [shell] command.

$ man write

NOTE: Press q to exit [man] command. PLEASE DON'T PRESS CTRL-Z.

Examples:
Copy the file [/etc/passwd] on standard output ( CRT screen )
and number each line of the file
$ cat -n /etc/passwd

Copy the file [/etc/passwd] one screen-full at a time
$ cat /etc/passwd | more

In the above, the output of [ cat ] command is passed to one end of a
pipe. The other end of the pipe is connected to the input of [ more ]
command.

A better way to examine a file, a screen-full at a time, is to use
[less] command.
$ less /etc/passwd

NOTE: Press q to exit [less] command. PLEASE DON'T PRESS CTRL-Z.

Copy the file [/etc/passwd] to one end of a pipe. The other end of the
pipe is connected to input of [ grep ] command. A pattern [ sumit ] is
given to the [ grep ] command. The intention is to pick out the lines
that contain the pattern sumit .

$ cat /etc/passwd | grep sumit

A typical output might be

sumit:x:1001:1001:Sumit Kumar Sarkar,,,:/home/sumit:/bin/bash


Record the actual output




After login, [sumit] is put into the [home] directory of Sumit.

[pwd] command shows the present/working/current directory
$ pwd

Executing [cd] command without argument puts [sumit] in Sumit's [home]
directory.
$ cd

The [shell] also makes available some environmental variables, such as
HOME, SHELL, PATH, PS1 etc etc to [sumit].

Look at the values of some of them

$ echo $PATH
$ echo $SHELL
$ echo $HOME

[sumit] can change some of these variables.

Change the primary prompt

$ PS1="osl> "

Note: There should be no space on either side of the equality sign.
This change of prompt is temporary and remains in effect only
during this login.

When [sumit] runs a [shell] command, [shell] looks for the command in
the locations specified by the PATH variable. The actual location of
an executable program may be found with [whereis] command.

osl> whereis -b gzip

osl> whereis -b sort

osl> whereis -b cat

Clear the PATH environmental variable
osl> PATH=

Print the value of the PATH variable
osl> echo $PATH

The output should be an empty string.

The following command should not work as [ shell ] does not know where
to look for [ cat ] command
osl> cat /etc/passwd

The following command should work as complete path-name of the command
is used
osl> /bin/cat /etc/passwd

The [shell] has some built-in commands, such as cd, echo, fg, history,
logout etc etc. Even when PATH variable is cleared, these built-in
commands continue to work.

osl> history

Logout and login again to restore the PATH variable. Then try to find
out the location of the [cd] command.

osl> whereis -b cd


The [shell] is also programmable. A simple [shell] program example is
shown in the next example. [shell] programs are also called [shell]
scripts.

Create the following text file using any text editor ( vi, nvi, vim,
nano, pico etc etc ).

osl> nano sc-1

---- snip -----
#!/bin/bash
echo Hello CEMK
---- snip -----

The first line

#!/bin/bash

is the directive to the [shell] to run the shell script using [bash].

The next line

echo Hello CEMK

echoes Hello CEMK on standard output ( CRT screen ).

A shell script does not need compilation. The [ shell ] program can be
run as:
osl> sh ./sc-1

Another method is to make the shell script executable.

The jobs that need long time to complete ( compilation of a very large
C program ? ) can be carried out in background.

Save the following shell script in a file [sc-2].
------- snip --------
#!/bin/bash
while :
do
sleep 1
done
------- snip --------
Run it in foreground..
osl> sh ./sc-2

Go to another terminal. Check that the program is running with..
osl> ps ax | grep sc-2

Come back to the other terminal and terminate the running program ( an
endless loop ) with CTRL-C. You may read the manual of [ps].

Run the shell script in background with..
osl> sh sumit-script &

Note the job number within square bracket and the process number (pid)
from the output of this command.

Many instances of the same program can be run by using the above
command many times.

Check that the program is running with..
osl> ps -ax | grep sc-2

Bring the process in foreground with..
osl> fg job_number

The foreground process can be terminated with CTRL-C.

A history of previously executed commands are available. Try pressing
up-arrow key. Use the following command to view some of the previous
commands used by you.

osl> history

The number of commands to remember in the command history is set in
HISTSIZE environmental variable. The default value is 500.

osl> echo $HISTSIZE


File permissions:
File permissions can be described with a 12 bit
number. [chmod] command changes the permissions of a file. The user,
using this command must have requisite permissions to do so. [chmod]
either use symbolic representation of changes to be made or four octal
digits to manipulate permissions. The method using four octal digits
is described below.


...............[chmod] command..............
/ \
r w x r w x r w x
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
\ / | | | \ / \ / \ /
File type info | | Sticky Owner Group Others
set uid | bit
set gid




If the bit is "1" permission is turned "ON" & if it is "0" permission
is turned "OFF".

Create an empty file [bogusfile] with [touch] command.
osl> touch ./bogusfile
Record the permissions of [./bogusfile]
osl> ls -l

Owner has ____________________ permissions

Group has ____________________ permissions


Others have ___________________ permissions


Remove all permissions from the file

osl> chmod 0000 ./bogusfile

Verify with [ls -l] command
osl> ls -l


Give read, write and execute permissions to owner, read and execute
permissions to group and no permission to others

osl> chmod 0750 ./bogusfile

Verify with [ls -l] command


Set sticky bit for the file keeping other permissions unaltered

osl> chmod 1750 ./bogusfile

Use [file] command on [./bogusfile] and record the output

osl> file ./bogusfile
Output:


Set[ gid ]( group id ) bit for [./bogusfile] keeping other permissions
unaltered
osl> chmod 3750 ./bogusfile

Use file command on [./bogusfile] and record the output
osl> file ./bogusfile
Output:


Set [uid] ( user id ) bit for [./bogusfile] keeping other permissions
unaltered.
osl> chmod 7750 ./bogusfile


Use file command on [./bogusfile] and record the output.
osl> file ./bogusfile
Output:


File creation mask:
When a new file is created its permissions are set
using the file creation mask.

Examine the existing file creation mask

osl> umask
output--> ___________

Set all 12 bits of file creation mask. If any bit of the mask is 1,
that permission is turned off

osl> umask 777

Note: [umask] is shell built-in command. C system call has a function
with the sane name.

Create a new file
osl> touch ./garbage

All permissions should be turned off in this file.

Logout and login again to restore the default value of [umask].

Ordinary users are not allowed permissions to access and modify system
files. Try to view as [sumit], the contents of the file [/etc/shadow],
where encrypted password of the users are stored.
osl> less /etc/shadow

Try to create as [sumit], any file under [/usr/] directory
osl> mkdir /usr/yyy

[root] user can access and modify all of the file system. You will be
working on this machine as [ root ] user, to carry out certain tasks,
which only the [ root ] user is allowed to do. [ root ] user is not
prevented to execute any command by the operating system. It implies
that commands executed carelessly as [ root ] user, can damage/destroy
the operating system.



Creation of a new user in your PC:
Only [root] can add or remove users.
Login as [sumit] and execute the following commands. The prompts and
messages may be different.

osl> su
osl> Password: xxxxxxxxx /* enter root's password */
# whereis -b adduser /* find the path for [adduser] command */
# /usr/sbin/adduser yourname /* yourname is entered by you */
# passwd yourname /* yourname is entered by you */
Changing password for user yourname
New UNIX password: yourname /* yourname is entered by you */
BAD PASSWORD: it is too short
Retype new UNIX password: yourname /*yourname is entered by you */
passwd: all authentication tokens updated successfully

Though it was detected as a bad password, [root] was not prevented to
set a bad password. Be VERY careful while working as [ root ] user.

Then execute [exit] command to come out of [root] mode
# exit


Logout from Sumit's account.
osl> logout OR press CTRL-D

Login in your account

Check logged-in user
$ whoami


Check the home directory of the logged-in user.
$ echo $HOME

Create a directory named [intro] in your home directory.
$ mkdir $HOME/intro

Enter that directory
$ cd $HOME/intro/

Carry out rest of the experiment in [intro] directory.

Read manual of [passwd] command from section 1 of the manual

$ man 1 passwd

Read the topic titled "Hints for user passwords", to choose a strong
password for your account, using the [passwd] command.

$ passwd

Logout and then login with your new password.


Note: If the above command is used in hash (#) prompt, [root] password
would be changed. Please DON'T change [root] password.


Creation of a new UNIX command:

Write the following C program
$ nano source.c

------------------------------ snip ----------------------------------
// file-name source.c

#include
#include

int main( void )
{
time_t now;

time( &now );

printf("Hurry-up! seconds are slipping away!!\n" );
printf("%lu seconds have elapsed since zero hours of January 1970\n",
now );

return 0;
} // end of main
------------------------------ snip ----------------------------------

Compile the program
$ gcc -Wall ./source.c -o ./my-new-command

Run the program
$ ./my-new-command

You may verify the output of the program with...
$ date +%s

[gcc] is the C compiler. You can read gcc(1).

Create a directory [bin] under home directory
$ mkdir $HOME/bin

Copy the executable file [my-new-command] to the [$HOME/bin] directory
$ cp ./my-new-command $HOME/bin/

Remove the executable from the current directory
$ rm ./my-new-command

Include [ $HOME/bin/ ] in the [ PATH ] environmental variable

$ PATH=$PATH:$HOME/bin

Now execute the new command

$ my-new-command

You may check the partial completion feature of the [bash] shell. Type
the command partially

$ my-new-com

then press TAB.

Copy file [/etc/hosts] file in the current directory with a new name
$ cp /etc/hosts ./etc-hosts

A file may be renamed with [mv] command
$ mv ./etc-hosts ./etc-hosts-of-my-pc

Portions of files can be viewed with [head] and [tail] commands.

View first three lines of a file
$ head -3 /etc/passwd

View last 10 lines of a file
$ tail -10 /etc/passwd


Link command [ ln ] can be used to give more than one name to a file.

Example:
This command links the file [ /etc/hosts.allow ] with another
file [hostallow] in the current directory

$ ln /etc/hosts.allow ./hostallow

The above link is called a hard link. Command [ls -l] shows the file.
To verify that [/etc/hosts.allow] and [./hostallow] refer to the the
same file, use the following command and record output

$ ls -i ./hostallow /etc/hosts.allow

output:



The inode number of the two files should be same. OS keeps track of
files with inode numbers. The contents of the files [/etc/hosts.allow]
and [./hostallow] can also be checked with [ less ] command to verify
that they are same.


Soft links or symbolic links are also possible. This command creates
soft link to the file [ /etc/hosts.deny ], with a file [ denyhost ] in
current directory.

$ ln -s /etc/hosts.deny ./denyhost

[ ls -l ] command shows the link
$ ls -l ./denyhost

Use [file] command to check the file [./denyhost]
$ file ./denyhost
output:

The files [ /etc/hosts.deny ] and [ ./denyhost ], do not have the same
inode number
$ ls -i /etc/hosts.deny ./denyhost

The command
$ less ./denyhost
prints the contents of the file [/etc/hosts.deny] on standard output.



UNIX shells have three opened files called the standard input,
standard output and standard error. Normally the screen is the
standard output and the standard error. The keyboard is the standard
input.


The redirection operator [>]:

The output of the following command produces output on screen ( the
standard output ).
$ cat /etc/resolv.conf

The standard output can be redirected to a file using the command..
$ cat /etc/resolv.conf > ./name-server-directive

No output should appear on the screen if there is no error. If the
file [/etc/resolv.conf] does not exist, error is generated on standard
error. If the file [ ./name-server-directive ] does not exit, it is
created. If it already exists, the contents of that file gets
overwritten by the contents of the file [/etc/resolv.conf].

The following command generates error and the error appears on the
standard error (the screen), even though standard output is redirected
to a file.
$ cat /etc/shadow > ./password-data

The standard error can be redirected to a file
$ cat /etc/shadow 2> ./error

The role of the file [ ./error ] as the standard error ends with the
completion of the command and the screen again becomes the standard
error.


Files can be appended using redirection operators [>>].

$ more /etc/hosts.allow > ./allow-deny
$ more /etc/hosts.deny >> ./allow-deny

The same thing can be done by concatenation using [ cat ]. The name of
the command originates from this operation.
$ cat /etc/hosts.allow /usr/include/stdio.h > ./allow-stdio

Binary files can also be concatenated. Copy [/usr/sbin/identd] file to
the present working directory
$ cp /usr/sbin/identd ./identd

Break-up the file into 10K-byte chunks
$ split --byte=10k ./identd

Concatenate the parts
$ cat xaa xab xac > ./joined-identd

The two file [identd] and [joined-identd] should have the same size.


[sort] command is used for sorting text files. A simple use of [sort]:

Copy contents of directory [/var/log] in a file
$ ls -l /var/log > varlog

See the contents of the file [varlog] with [less]

Sort the file on 5Th field and display the result on standard output.
$ sort -g -k 5,5 ./varlog | more


Assignment-1.
Record the following by examining the [/etc/passwd] file.

Your default shell--->
Your home directory-->
Your UID ------------>
Your GID ------------>


Assignment-2.
Using [ file ] command record the types of the following
files.


/usr/share/man/man1/printf.1.gz -->

/usr/bin/awk --------------------->

/dev/initctl --------------------->

/dev/stderr ---------------------->

Assignment-3.
Allow read and execute permissions to owner for [./sc-1]
and execute it
$ ./sc-1

--------------------------------------