Linux Lab File 18 19
Linux Lab File 18 19
Aim: Installation of Linux operating system using CD/DVD/USB drive or PXE boot.
Thoery:
1. Boot your system with OpenSUSE 12.3 installation media i.e CD/DVD or ISO image.
4. Welcome screen, From where we can select Language and keyboard layout.
Read license agreement and proceed further installation once agreed.
5. Clock and timezone settings.
6. Please click on change if you want custom setting of date and time. You can
change it manually or sync with NTP Server as show below. Click Accept once
done.
8. Create new user and it’s password. Uncheck all three options. Click
on change to select authentication method.
14. Installation completed, remove installation media and click on Reboot Now.
OpenSuse Installation Completed
Objective: To learn how to implement and usage of file and directory handling
commands.
Theory:
Usage
Options
Mandatory arguments to long options are mandatory for short options too.
-m, mode=MODE set permission mode (as in chmod), not rwxrwxrwx - umask
cd - change directories
Use cd to change directories. Type cd followed by the name of a directory to access that
directory.Keep in mind that you are always in a directory and can navigate to directories
will show you the full path to the directory you are currently in. This is very handy to use,
rm -r
Usage
Options
Change the owner and/or group of each FILE to OWNER and/or GROUP. With --reference,
-c, changes like verbose but report only when a change is made
-dereference affect the referent of each symbolic link, rather than the symbolic link itself
-h, no-dereference affect each symbolic link instead of any referenced file (useful only on
-from=CURRENT_OWNER:CURRENT_GROUP
change the owner and/or group of each file only if its current owner and/or group match
those specified here. Either may be omitted, in which case a match is not required for the
omitted attribute.
-reference=RFILE use RFILE's owner and group rather than the specifying
OWNER:GROUP values
The following options modify how a hierarchy is traversed when the -R option is also
specified. If more than one is specified, only the final one takes effect.
Usage
r Change the permission on files that are in the subdirectories of the directory that you are
currently in. permission Specifies the rights that are being granted. Below is the
different rights that you can grant in an alpha numeric format.filenames File or directory
o - Other.
a - All.
Numeric Permissions:
cp - Copy files
cp myfile yourfile
Copy the files "myfile" to the file "yourfile" in the current working directory. This command
will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning
if it exists.
cp -i myfile yourfile
With the "-i" option, if the file "yourfile" exists, you will be prompted before it is
overwritten.
cp -i /data/myfile
Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt
Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-
poption), file attributes (-p option), and copy recursively (-r option). With these options, a
Conclusion: Hence we have implemented various file and directory handling commands.
Experiment No.03
Aim: Execution of simple C and C++ programs using CC and GCC compiler
Theory:
Type the command gedit hello.c and enter the C source code below:
#include main()
printf("Hello World\n");
This command will invoke the GNU C compiler to compile the file hello.c and output (-o) the result to an
Hello World
Objective: To learn how to create partition, how to mount and un mount partition and
how to resize a partition.
Theory:
Creating a New Partition in Linux
In most Linux systems, you can use the fdisk utility to create a new partition and to do other
disk management operations.
Note: To be able to execute the commands necessary to create a new partition on Linux,
you must have the root privileges.
As a tool with a text interface, fdisk requires typing the commands on the fdisk command
line. The following fdisk commands may be helpful:
Options Description
m Displays the available commands.
p Displays the list of existing partitions on your hda drive.
Unpartitioned space is not listed.
n Creates a new partition.
q Exits fdisk without saving your changes.
l Lists partition types.
w Writes changes to the partition table.
To create a new partition on Linux
1. Start a terminal.
2. Start fdisk using the following command:
/sbin/fdisk /dev/hda
where /dev/hda stands for the hard drive that you want to partition.
3. In fdisk, to create a new partition, type the following command:
n
When prompted to specify the Partition type, type p to create a primary
partition or e to create an extended one. There may be up to four primary
partitions. If you want to create more than four partitions, make the last
partition extended, and it will be a container for other logical partitions.
When prompted for the Number, in most cases, type 3 because a typical Linux
virtual machine has two partitions by default.
When prompted for the Start cylinder, type a starting cylinder number or
press Return to use the first cylinder available.
When prompted for the Last cylinder, press Return to allocate all the available
space or specify the size of a new partition in cylinders if you do not want to use
all the available space.
By default, fdisk creates a partition with a System ID of 83. If you're unsure of the
partition's System ID, use the
l
command to check it.
4. Use the
w
command to write the changes to the partition table.
5. Restart the virtual machine by entering the
reboot
command.
6. When restarted, create a file system on the new partition. We recommend that you use
the same file system as on the other partitions. In most cases it will be either
the Ext3 or ReiserFS file system. For example, to create the Ext3 file system, enter the
following command:
/sbin/mkfs -t ext3 /dev/hda3
7. Create a directory that will be a mount point for the new partition. For example, to
name it data, enter:
mkdir /data
8. Mount the new partition to the directory you have just created by using the following
command:
mount /dev/hda3 /data
9. Make changes in your static file system information by editing the /etc/fstab file in any
of the available text editors. For example, add the following string to this file:
/dev/hda3 /data ext3 defaults 0 0
In this string /dev/hda3 is the partition you have just created, /data is a mount point for the
new partition, Ext3 is the file type of the new partition. For the exact meaning of other
items in this string, consult the Linux documentation for the mount and fstab commands.
10. Save the /etc/fstab file.
This section shows you how to actually partition your hard drive with the fdisk utility.
Linux allows only 4 primary partitions. You can have a much larger number of logical
partitions by sub-dividing one of the primary partitions. Only one of the primary
partitions can be sub-divided.
Examples:
d delete a partition
Changes you make to the partition table do not take effect until you issue the write (w)
command. Here is a sample partition table:
The first line shows the geometry of your hard drive. It may not be physically
accurate, but you can accept it as though it were. The hard drive in this example is
made of 32 double-sided platters with one head on each side (probably not true). Each
platter has 621 concentric tracks. A 3-dimensional track (the same track on all disks)
is called a cylinder. Each track is divided into 63 sectors. Each sector contains 512
bytes of data. Therefore the block size in the partition table is 64 heads * 63 sectors *
512 bytes er...divided by 1024. (See 4 for discussion on problems with this
calculation.) The start and end values are cylinders.
All Mounts
Experiment No.05
Aim: Create user, group and assign various permissions to access a directory.
Objective: To learn creating a new user, group and assigning permissions to access a
directory.
Theory:
To add a new user account, you can run either of the following two commands
as root.
# adduser [new_account]
# useradd [new_account]
When a new user account is added to the system, the following operations are
performed.
.bash_logout
.bash_profile
.bashrc
# groups tecmint
# id tecmint
there are Three basic file/directory operations that a user/group/other users can perform on the files and
directories.
Read (r): Permission to read the contents of the file/directory. In case of directories, a person can
view all the files and sub-directories belonging to the directory.
Write (w): Permission to modify the contents of the file/directory. In case of directories, a person
can create a file or sub-directory in that directory.
Execute (x): Permission to execute a file as a script/program. Executing a directory! Well, it does
not make any sense. In case of directories, a person can enter that directory. In order to
use ls and cd commands in /bin directory, a user should have Execute permissions.
CHMOD assigns numeric values to the Read, Write and Execute permissions which are as follows:
Read : 4
Write : 2
Execute : 1
So, the permissions associated with any file/directory in Linux have a 3x3 format i.e. Three types of
permissions (Read, Write and Execute) that are available for three types of users (Owner, Group and
Other).
To observe this, just enter ls –l command that displays 9 characters for every file/directory
representing the permissions for all the three types of users.
For Example:
In above output,
Another example, to give Read + Execute permission (4 + 1 = 5) to user and no permission (0) to group,
and Write (2) permission to others, enter following command:
is
UMASK, along with default permission of file/directory, is responsible for determining the final value of the
default permission of a file/directory. The default permission for a file is 777 and for a directory, it is 666.
From these default permissions, the umask value is subtracted to get the final default permission for
newly created files or directory. The default value of umask is 022.
Final default permissions for file and directories are determined as follows:
umask 0077
After this action, when you make a new file/directory, the permissions associated with them will be as
shown below:
bolic Representation
The symbolic representation used for three different types of users is as follows:
Using above command, we can add Execute permission to the owner of the file.
Using above command, we can add Execute permissions to the group and other users of the file.
3. Removing a Permission
Removing a permission is as easy as adding a permission, just remember to use '-' symbol instead of '+'.
If we want to apply some specific changes to all the files inside a directory, we can make use of option -R
That's enough for this article. In this article, I tried to cover the basics of files and directory permissions
and the fundamental use of CHMOD command that helps us change those permissions associated with
Conclusion: Hence we have implemented creating user, group and assigning permissions to access a
directory.
Experiment No.06
Theory:
All commands must be done as root (precede each command with 'sudo' or use 'sudo su').
1. Install Samba
1. sudo apt-get update
2. sudo apt-get install samba
2. Set a password for your user in Samba
1. sudo smbpasswd -a <user_name>
You can also hide the user on the login screen by adjusting
lightdm's configuration, in /etc/lightdm/users.conf add the
newly created user to the line :
hidden-users=
3. Create a directory to be shared
mkdir /home/<user_name>/<folder_name>
4. Make a safe backup copy of the original smb.conf file to your home folder, in case you make an error
sudo cp /etc/samba/smb.conf ~
5. Edit the file "/etc/samba/smb.conf"
sudo nano /etc/samba/smb.conf
1. Once "smb.conf" has loaded, add this to the very end of the file:
2.
3. [<folder_name>]
4. path = /home/<user_name>/<folder_name>
5. valid users = <user_name>
6. read only = no
Tip: There Should be in the spaces between the lines, and note que
also there should be a single space both before and after each of
the equal signs.
6. Restart the samba:
sudo service smbd restart
7. Once Samba has restarted, use this command to check your smb.conf for any syntax errors
testparm
8. To access your network share
9. sudo apt-get install smbclient
10. # List all shares:
11. smbclient -L //<HOST_IP_OR_NAME>/<folder_name> -U <user>
12. # connect:
# !/bin/bash
echo "enter a number"
read num
fact=1
while [ $num -ge 1 ]
do
fact=`expr $fact\* $num`
num=’expr $num – 1’
done
echo "factorial of $n is $fact"
Output:
enter a number
4
factorial 0f 4 is 24
total
characters : 63
words: 12
lines : 3
Conclusion: Hence we have implemented shell script for input-output statements and
loops.
Experiment No.08
Aim: Write a shell script program using array & case statement.
Theory:
#!/bin/sh
option="${1}"
case ${option} in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
esac
Output:
$./test.sh
test.sh: usage: [ -f filename ] | [ -d directory ]
$ ./test.sh -f index.htm
$ vi test.sh
$ ./test.sh -f index.htm
File name is index.htm
$ ./test.sh -d unix
Dir name is unix
Conclusion: Hence we have implemented array and case statement in shell script.
Experiment No.09
Theory:
About grep
grep, which stands for "global regular expression print," processes
text line by line and prints any lines which match a specified pattern.
grep syntax
grep [OPTIONS] PATTERN [FILE...]
Overview
Grep is a powerful tool for matching a regular expression against text in a
file, multiple files, or a stream of input. It searches for the PATTERN of text
that you specify on the command line, and outputs the results for you.
General Options
--help Print a help message briefly summarizing command-line options, and exit.
-G, --basic-regexp Interpret PATTERN as a basic regular expression (see Basic vs. Extended Regular Expressions). This
when running grep.
-P, --perl-regexp Interpret PATTERN as a Perl regular expression. This functionality is still experimental, and may pro
messages.
-i, --ignore-case Ignore case distinctions in both the PATTERN and the input files.
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching su
at the beginning of the line, or preceded by a non-word constituent character. Or, it must be either
or followed by a non-word constituent character. Word-constituent characters are letters, digits, an
-x, --line-regexp Select only matches that exactly match the whole line.
About sed
sed, short for "stream editor", allows you to filter and transform text.
Description
sed is a stream editor. A stream editor is used to perform basic text
transformations on an input stream (a file, or input from a pipeline). While in
some ways similar to an editor which permits scripted edits (such
as ed), sed works by making only one pass over the input(s), and is
consequently more efficient. But it is sed's ability to filter text in a pipeline
which particularly distinguishes it from other types of editors.
sed syntax
sed OPTIONS... [SCRIPT] [INPUTFILE...]
-i[SUFFIX], --in-place[=SUFFIX] Edit files in place (this makes a backup with file extension SUFFIX, if SUFFIX is s
-l N, --line-length=N Specify the desired line-wrap length, N, for the "l" command.
-s, --separate Consider files as separate rather than as a single continuous long stream.
-u, --unbuffered Load minimal amounts of data from the input files and flush the output buffers mo
first~step This GNU extension of sed matches every step lines starting with line first. In particular, lines will be select
non-negative n such that the current line-number equals first + (n * step). Thus, to select the odd-numbered l
use 1~2; to pick every third line starting with the second, ‘2~3’ would be used; to pick every fifth line startin
‘10~5’; and ‘50~0’ is just another way of saying 50.
$ This address matches the last line of the last file of input, or the last line of each file when the -i or -s options
/regexp/ This will select any line which matches the regular expression regexp. If regexp itself includes any "/" chara
be escaped by a backslash ("\").
The empty regular expression ‘//’ repeats the last regular expression match (the same holds if the empty regu
passed to the s command). Note that modifiers to regular expressions are evaluated when the regular express
it is invalid to specify them together with the empty regular expression.
This also matches the regular expression regexp, but allows one to use a different delimiter than "/". This op
useful if the regexp itself contains a lot of slashes, since it avoids the tedious escaping of every "/". If regexp
delimiter characters, each must be escaped by a backslash ("\").
/regexp/I The I modifier to regular-expression matching is a GNU extension which causes the regexp to be matched in
opposed to case-sensitive) manner.
\%regexp%I
/regexp/M The M modifier to regular-expression matching is a GNU sed extension which causes ^ and $ to match resp
the normal behavior) the empty stringafter a newline, and the empty string before a newline. There are speci
\%regexp%M ("\`" and "\'") which always match the beginning or the end of the buffer. M stands for multi-line.
If no addresses are given, then all lines are matched; if one address is
given, then only lines matching that address are matched.
An address range can be specified by specifying two addresses separated by
a comma (","). An address range matches lines starting from where the first
address matches, and continues until the second address matches
(inclusively).
If the second address is a regexp, then checking for the ending match will
start with the line following the line which matched the first address: a range
will always span at least two lines (except of course if the input stream
ends).
If the second address is a number less than (or equal to) the line matching
the first address, then only the one line is matched.
GNU sed also supports some special two-address forms; all these are GNU
extensions:
0,/regexp/ A line number of 0 can be used in an address specification like 0,/regexp/ so that sedwill try to match regexp in
In other words, 0,/regexp/ is similar to 1,/regexp/, except that if addr2 matches the very first line of input the 0,/r
consider it to end the range, whereas the 1,/regexp/ form will match the beginning of its range and hence make th
second occurrence of the regular expression.
Note that this is the only place where the 0 address makes sense; there is no "0th" line, and commands which are
any other way will give an error.
addr1,~N Matches addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.
* Matches a sequence of zero or more instances of matches for the preceding regular expression, which mu
character, a special character preceded by "\", a ".", a grouped regexp (see below), or a bracket expression
extension, a postfixed regular expression can also be followed by "*"; for example, a** is equivalent to a
2001 says that * stands for itself when it appears at the start of a regular expression or subexpression, but
implementations do not support this, and portable scripts should instead use "\*" in these contexts.
\{i\} Like *, but matches exactly i sequences (i is a decimal integer; for compatibility, you should keep it betw
inclusive).
Apply postfix operators, like \(abcd\)*: this will search for zero or more whole sequences of ‘ab
while abcd*would search for ‘abc’ followed by zero or more occurrences of ‘d’. Note that suppo
required by POSIX 1003.1-2001, but many non-GNU implementations do not support it and hen
portable.
Use back references (see below).
^ Matches the null string at beginning of the pattern space, i.e. what appears after the ^ must appear at the
pattern space.
In most scripts, pattern space is initialized to the content of each line. So, it is a useful simplification to th
matching only lines where ‘#include’ is the first thing on line—if there are spaces before, for example, th
simplification is valid as long as the original content of pattern space is not modified, for example with an
^ acts as a special character only at the beginning of the regular expression or subexpression (that is, afte
scripts should avoid ^ at the beginning of a subexpression, though, as POSIX allows implementations tha
ordinary character in that context.
$ It is the same as ^, but refers to end of pattern space. $ also acts as a special character only at the end of t
or subexpression (that is, before \) or \|), and its use at the end of a subexpression is not portable.
[list] Matches any single character in list: for example, [aeiou] matches all vowels. A list may include sequenc
which matches any character between char1 and char2. For example, [b-e] matches any of the characters
[^list]
A leading ^ reverses the meaning of list, so that it matches any single character not in list. To include ] in
first character (after the ^ if needed); to include - in the list, make it the first or last; to include ^ put it aft
The characters $, *, ., [, and \ are normally not special within list. For example, [\*] matches either ‘\’ or
special here. However, strings like [.ch.], [=a=], and [:space:] are special within list and represent collati
equivalence classes, and character classes, respectively, and [ is therefore special within list when it is fol
Also, when not in POSIXLY_CORRECT mode, special escapes like \n and \tare recognized within list
information.
regexp1\|regexp2 Matches either regexp1 or regexp2. Use parentheses to use complex alternative regular expressions. The
each alternative in turn, from left to right, and the first one that succeeds is used. This option is a GNU ex
regexp1regexp2 Matches the concatenation of regexp1 and regexp2. Concatenation binds more tightly than \|, ^, and $, bu
other regular expression operators.
\digit Matches the digit-th \(...\) parenthesized subexpression in the regular expression. This option is called a b
Subexpressions are implicitly numbered by counting occurrences of \( left-to-right.
\char Matches char, where char is one of $, *, ., [, \, or ^. Note that the only C-like backslash sequences that yo
assume to be interpreted are \n and \\; in particular \t is not portable, and matches a ‘t’ under most implem
rather than a tab character.
Note that the regular expression matcher is greedy, i.e., matches are
attempted from left to right and, if two or more matches are possible
starting at the same character, it selects the longest.
For example:
abcdef Matches "abcdef".
a*b Matches zero or more "a" characters, followed by a single "b". For example, "b" or "aaaaaaab".
a\+b\+ Matches one or more "a" characters followed by one or more "b"s. "ab" is the shortest possible match, but oth
"aaaaab", "abbbbbb", or "aaaaaabbbbbbb".
.*or .\+ Either of these expressions will match all of the characters in a non-empty string, but only .* will match the em
^main.*(.*) This matches a string starting with "main", followed by an opening and closing parenthesis. The "n", "(" and
adjacent.
\\$ This matches a string ending with a single backslash. The regexp contains two backslashes for escaping.
\$ This matches a string consisting of a single dollar sign.
[^ tab]\+ (Here tab stands for a single tab character.) This matches a string of one or more characters, none of which is
Usually this means a word.
^\(.*\)\n\1$ This matches a string consisting of two equal substrings separated by a newline.
^.\{15\}A This matches the start of a string that contains 16 characters, the last of which is an ‘A’.
Theory:
$ awk 'BEGIN {
sum = 0; for (i = 0; i < 20; ++i) {
sum += i; if (sum > 50) exit(10); else print "Sum =", sum
}
}'
Output:
Sum = 0
Sum = 1
Sum = 3
Sum = 6
Sum = 10
Sum = 15
Sum = 21
Sum = 28
Sum = 36
Sum = 45