square brackets to separate them from lines of text. The square
brackets have no other meaning.
Login as [sumit].
Create a directory [your-name] in the home directory of [sumit]...
sumit $ mkdir your-name
Enter that directory and carry out today's experiments in that
directory...
sumit $ cd your-name
When any user ( such as [sumit] ) logs on, the UNIX operating system
puts the user in his/her home directory and starts a new, interactive
program for that user. This program is the interface between the user
and the operating system. This program is called the [ shell ]. The
[shell] takes the commands from the user and arranges the commands to
be executed by the operating system and sends the result to the user.
In this role the [shell] acts as command interpretor.
The [ shell ] stores some values of environment of the user. The home
directory and default [ shell ] are stored in two environmental
variables.
Display the values of two variables, using [echo] shell command...
Command: $ echo $SHELL Enter Output:
Command: $ echo $HOME Enter Output:
Usually more than one [shell] is available. User might use a [shell],
which is different from the default shell. The list of available
[shells] are usually stored in file [$ /etc/shells]. Run the command
and examine the output...
$ less /etc/shells
The home directory and the default [shell] for any user is set in file
[/etc/passwd]. The following is a compound command.
$ less /etc/passwd | grep sumit
The first part of the command is [ $ less /etc/passwd ]. If executed
separately, the contents of the file [/etc/passwd] is displayed on the
standard input ( the CRT screen ).
In the compound command, the output of the first command is piped to
the input of the second command [ grep ], which selects the line
containing the pattern sumit.
Record the output of the compound command.
Output:
[less] command is used to display text one screen-full at a time. Search
for a string with [/]. Press [q] to exit [less].
Note:
Don't use [CTRL-Z] to exit a program. Pressing [ CTRL-Z ] sends a
SIGTSTP signal to the process. This signal stops the process. The
process is not terminated.
Linux commands and filenames are case sensitive. [pwd], [Pwd], [PWD]
are all different. The current or present working directory can be
viewed by [pwd] command.
The number of commands available to the user is large. To find out the
commands that start with letter [a], try the following...
$ a Press TAB twice
Here we have used "completion" feature of the [shell].
It is not required to memorise the usage of the commands as manuals
for almost all commands are available in your UNIX machine, in "UNIX
Programmer's Manual". To find the location of the manual pages, use
command [ $ echo $MANPATH ]. If it returns a null string, the file
[ /etc/manpath.config ] should be consulted. The location might be
[/usr/share/man/man1].
Some of the user command are given below. Many of these commands take
options. The manual for a command should be consulted if required.
[ls-l] command:
This command lists directory contents in long format.
With this, find out what [shell] commands are available in section 1
of the UNIX Programmer's Manual.
$ ls -l /usr/share/man/man1 | less
You can read manual by using [man] command. The section of the manual
should be specified if names of topics are the same.
Examine the output of the following commands.
Press [q] to exit [less]; do not press CTRL-Z.
$ man 3 printf
$ man 1 printf
$ man printf
[cd] command:
This is used to change directory. To change into a
directory, the user must have execute permission for that directory.
The following command should fail due to lack of proper permission.
$ cd /lost+found/
[whereis] command:
To find out where [less] is located, use command...
$ whereis less
Commands can be used using their full pathnames, such as...
$ /usr/bin/less /etc/passwd
Commands work without proper pathname, because [ shell ] searches for
the command in the directories, mentioned in the PATH environment
variable of the user. Find the PATH of [sumit]...
$ echo $PATH
[locate] command:
It is a powerful way to find a file in the computer.
Try the command [$ locate less]. System administrators update a data
base periodically, using [updatedb] command, so that users can use
[locate] command to find files. There is a [find] command also to find
files.
[mkdir] command:
Directories can be created by the user in the home
directory and in some other directories if permissions exist.
Make a directory [sumitdir1] in the present directory.
$ mkdir ./sumit1
Make a directory [sumit-tmp] in [/tmp] directory.
$ mkdir /tmp/sumit-tmp
The following command should fail due to lack of proper permission.
$ mkdir /usr/sumit-tmp
[rm] command:
Files and directories can be removed with [rm] command.
Create a zero length file [xxx] in the present directory.
$ touch ./xxx
Remove the file [xxx]
$ rm ./xxx
Remove the directory [sumit1] in the present directory.
Example: $ rm -r ./sumit1
The option [-r] stands for recursive.
[cp] command:
Files (and directories) can be copied with [cp] command.
Copy the system password file into your present directory.
$ cp /etc/passwd ./passwdfile
The following command should fail due to lack of proper permission.
$ cp /etc/passwd /usr/sbin/password
Copy the directory [ /etc/network/ ] with its sub-directories, into a
directory called [network-configuration], in the present directory.
$ cp -r /etc/network/ ./network-configuration
The option [-r] stands for recursive.
[mv] command:
Files can be moved or renamed with [mv] command.
Example: Rename passwdfile to system-password-file-of-my-unix-machine.
$ mv ./passwdfile ./system-password-file-of-my-unix-machine
Linux supports long filenames.
[head] and [tail] commands:
Portions of files can be viewed with [head]
and [tail] commands.
Display first 3 lines of the file [/etc/hosts]
$ head -3 /etc/hosts
Display last 10 lines of the file [/etc/passwd]
$ tail -10 /etc/passwd
[ln] command:
[ln] command can be used to give more than one name to a
file.
Link the file [/etc/hosts.allow] from your present directory.
$ ln /etc/hosts.allow ./hostallow
To verify that [/etc/hosts.allow] and [./hostallow] refer to the same
file, use the command..
$ ls -i hostallow /etc/hosts.allow
The inode number of the two files should be same.
Such links are called hard links.
Soft links or symbolic links are also possible with [ln] command.
Create soft link to the file [ /etc/hosts.deny ] from your present
directory.
$ ln -s /etc/hosts.allow ./host-allow
[ls -l] command shows the link.
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 [shell] is highly programmable. In your spare time read manual of
[bash]. [bash] is a widely used [shell].
A simple example of reading the standard input( the keyboard ) is...
----------------- snip -----------------
#!/bin/bash
read -p "Your name please ? " tomar_nam
echo Hi $tomar_nam, pleased to meet you!
exit
----------------- snip -----------------
Save the above as file [f1]. Use text editor you are familiar with.
Note:
[vi], [nano], [joe] are text editors. [nano] is menu driven and
may be used for writing source programs.
Examination of [f1]:
Though comments start with [#], the first line,
#!/bin/bash
is not a comment. It specifies the particular [shell] to be used to
run the following lines of shell program. Shell program is also called
shell script. A shell script need not be compiled.
In the second line [ tomar_nam ] is a variable which is used to store
the data entered from the keyboard. In this case the variable was
declared by using it. The contents of the variable is $variable_name.
In the third line, contents of the variable [tomar_nam] is printed on
standard output.
Run the above shell script...
$ sh ./f1
The file [f1] can be made executable and [f1] may be treated as a new
shell command.
The redirection operator [>]:
The output of the following command produces output on screen ( the
standard output ).
$ less /etc/network/interfaces
The standard output can be redirected to a file using the command..
$ less /etc/network/interfaces > ./interfaces
No output should appear on the screen if there is no error. If the
file [interfaces] does not exist,it is created. If it already exists,
the contents of that file gets overwritten by the contents of the file
[/etc/network/interfaces].
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 /var/log/messages > messages
The standard error can be redirected to a file.
$ cat /var/log/messages 2> messages
The role of the file [messages] as the standard error ends with the
completion of the command and the screen again becomes the standard
error.
[cat] command:
[cat] command can be used to view files.
$ cat /etc/sysconfig/network
Files can be appended using redirection operators [>>].
$ less /etc/hosts.allow > allow-deny
$ less /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/atd] executable
file to your present working directory.
$ cp /usr/sbin/atd ./atd
Break-up the file into two parts.
$ split -10 ./atd
Concatenate the the two parts.
$ cat xaa xab > joined-ata
The two file [ata] and [joined-ata] should have the same size.
The [file] command looks at a file and returns some information on the
contents of a file.
Try the following commands.
$ file /usr/share/man/man1/exit.1.gz
$ file /bin/awk
$ file /dev/initctl
$ file /dev/stderr
$ file /etc/ppp/options
$ file /usr/bin/file
$ file /usr/lib/sendmail
[gcc] is the C compiler. You can read gcc(1).
Save the following program as a file [ f2.c ]. This is a "Hello World"
program using [write] system call.
------------------ snip --------------------
#include
int main ( void )
{
write( STDOUT_FILENO,"Hello CEMK\n", 11 );
return 0;
}
------------------ snip --------------------
Compile the program...
$ gcc -Wall ./f2.c -o ./2
The option [ -Wall ] stands for Warning-all. There are other kinds of
warning which are not printed with [ -Wall ] option. The executable
program [ 2 ] is created in the present directory, on successful
compilation.
Run the program...
$ ./2
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 [ f3 ]. This is an endless
loop program. Endless loop in a program is a serious fault, and it
must be avoided in real programs. It is used here and would be used
in some of the other experiments, to illustrate some points.
------- snip --------
#!/bin/bash
while :
do
sleep 1
done
------- snip --------
Run the shell script in foreground..
$ sh ./f3
PID of a process:
Login as [sumit] in another terminal. The Process IDentity(PID) of the
running program [f3] can be determined with [ps] command.
$ ps ax | grep f3
Go back to the other terminal and terminate the running program, the
endless loop, with CTRL-C. You can read the manual of [ps].
Run the shell script in background with..
$ sh ./f3 &
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..
$ ps ax | grep f3
Bring the process in foreground with..
$ fg job_number
The foreground process can be terminated with CTRL-C.
Partial completion of commands and filenames can be used. Remaining
portion is examined/entered with TAB key.
Try this..
$ /usr/sbin/send then press TAB key.
A history of previously executed commands are stored in a file. Try
pressing up-arrow key.
Use the following command to view the commands used by you, till now.
$ less ./.bash_history
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".
You will be working on this machine as superuser to carry out certain
tasks, which only the superuser is allowed to do. Ordinary users are
not allowed to execute such commands. The superuser is not prevented
from executing any command by the operating system. It implies that
commands executed carelessly as superuser, can damage the operating
system. The System Administrator should work on a system, logged in as
an ordinary user and change to superuser to carry out tasks, which
have to be done as superuser.
Here is an example:
To create a login for [yourname], login as [sumit]
and execute the following commands.
$ su
$ Password: xxxxxxxxx /* enter root's password */
# whereis 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, superuser was not prevented
to set a bad password. Be VERY careful while using wild-card with [rm]
command. Whenever possible the superuser should use [-i] option with
[rm] command.
######################################################################
WARNING:
Do NOT use the following command...
# passwd ---- Enter
The above command would change password of superuser. If this
password is lost, the PC becomes unusable in this laboratory.
######################################################################
Then execute [exit] command to come out of superuser mode.
Now read manual passwd(1). Then choose a password for your account
using the [passwd] command.
you $ passwd
Create an empty file [bogusfile] with [touch] command.
$ touch ./bogusfile
Record the [ls -l] of [./bogusfile], as shown below.
-rw-rw-r-- 1 sumit sumit 0 Nov 17 09:53 ./bogusfile
Remove all permissions from the file.
$ chmod 0000 ./bogusfile
Record the [ls -l] of [./bogusfile].
Give read permission to owner, write permission to group and execute
permission to others. ( It is a bogus file ! ).
$ chmod 0421 ./bogusfile
Record the [ls -l] of [./bogusfile].
Set sticky bit for the file keeping other permissions unaltered.
$ chmod 1421 ./bogusfile
Record the [ls -l] of [./bogusfile].
Use file command on [./bogusfile] and record the output.
$ file ./bogusfile
Output:
Set[ gid ]( group id ) bit for [./bogusfile] keeping other permissions
unaltered.
$ chmod 3421 ./bogusfile
Record the [ls -l] of [./bogusfile].
Use file command on [./bogusfile] and record the output.
$ file ./bogusfile
Output:
Set [uid] ( user id ) bit for [./bogusfile] keeping other permissions
unaltered.
$ chmod 7421 ./bogusfile
Record the [ls -l] of [./bogusfile].
Use file command on [./bogusfile] and record the output.
$ file ./bogusfile
Output:
The following were introduced in this experiment:
Commands:
mkdir, cd, echo, less, grep, pwd, ls, man, whereis,
updatedb, locate, rm, cp, mv, head, tail, ln, sh,
cat, split, file, ps, gcc, fg, adduser, passwd,
touch and chmod.
Note: [more] command was not illustrated, as [less]
command is more powerful pager.
Others:
[shell] as command interpretor,
Environmental variables,
Compound commands using pipe,
UNIX Programmer's Manual,
Hard link and symbolic link,
A simple shell script,
Redirection operators [ > ] and [ >> ],
A simple C program using system call,
PID of a process,
Creating account of a user,
Changing password of any user and
Changing permissions of a file.
