OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, ... $ cat...
Transcript of OPERATING SYSTEMS LINUX - os.ucg.ac.me · OPERATING SYSTEMS LINUX Božo Krstajić, ... $ cat...
OPERATING SYSTEMSOPERATING SYSTEMSLINUXLINUX
Božo Krstajić, PhD, University of Montenegro Podgorica
The Bash shell
The shell is the traditional interface used by UNIX and GNU/Linux.
In contrast to the X Window System it is an interface that works with commands.
In the beginning this can be a bit difficult, but the shell is very powerful.
The default shell on Slackware Linux is Bash. Bash means “Bourne-Again SHell”,
Slackware Linux also provides some other shells, but Bash is the main topic of this chapter.
The Bash shell
The most important job for the shell is to execute your commands.
Let’s look at a simple example.
Most UNIX variants have a command named whoami, which shows as which user you are logged in.
Try typing whoami, and press the <Enter> after that. The <Enter> tells the shell that it should
execute the command that you have typed on the current line.
Browsing through shell commands
It often happens that you have to execute commands that you executed earlier.
Fortunately, you do not have to type them all over again.
You can browse through the history of executed commands with the up and down arrows.
Besides that it is also possible to search for a command. Press <Control> + <r> and start typing the command you want to execute.
CompletionCompletion is one of the most useful functionalities
of Unix-like shells. Suppose that you have a directory with two files
named websites and recipe. And suppose you want to cat the file websites (cat shows the contents of a file), by specifying websites as a parameter to cat.
Normally you would type “cat websites”, and execute the command.
Try typing “cat w”, and hit the <Tab> key. Bash will automatically expand what you typed to “cat websites”.
Wildcards (“joker sign”)Most shells, including Bash, support wildcards.
Wildcards are special characters that can be used to do pattern matching.
The table listed below displays some commonly used wildcards. We are going to look at several examples to give a general idea how wildcards work.* A string of characters ? A single character [ ] A character in an array of characters
Standard input and outputWhen a command is not getting data from a file, it
will open a special pseudo-file named stdin, and wait for data to appear on it.
The same principle can be applied for command output, when there is no explicit reason for saving output to a file, the pseudo-file stdout will be opened for output of data.
Usually, keyboard input will be used for stdin, and stdout output will be printed to the terminal.
RedirectionsThe shell allows you to take use of stdin and stdout
using the “<” and “>”. Data is redirected in which way the sharp bracket points.
In the following example we will redirect the output of ls command to a file named list:
ls > list
We can also use redirection to provide input to a command:
cat < list
PipesYou can also connect the input and output of
commands using so-called pipes. A pipe between commands can be made with the “|”
character. Two or more combined commands are called a pipeline.
$ cat /usr/share/dict/american-english | grep "aba" | wc –l
So, combined three commands to count the number of words containing the phrase “aba” in this particular dictionary.
User – logging inFrom Windows: telnet Hostname [IP address]
So you've booted or connected, and you're looking at something that looks like this:
Welcome to Linux 2.4.22Hostname login:
Hostname is just the name of our computer.
If this is your first time, you'll want to log in as root.You'll be prompted for a password; if you set one during the
setup process, that's what it's looking for.
Otherwise, type your username (account name) and password
Root: The SuperuserIn the world of Unix and similar operating
systems (like Linux), there are users and then there are users.
Root is the user above all users; root is all-powerful and all-knowing, and
nobody disobeys root. It just isn't allowed.
Root is what we call a “superuser”.
Files and directories - commandspwd
pwd(1) is a simple utility which shows the directory (name with path) you are currently working in.
The pwd does not require any parameters. This is an example output of pwd:
$ pwd/home/danieldk
Files and directories - commandslsls can be used to display files and directories located
in specific directories. Running the ls command without any parameters
shows the contents of the current directory:
You can do this by specifying the path as a parameter to the ls command:
$ ls /devA disadvantage of the default output is that it
provides little information about files and directories.
The ls has the many parameters to show more information:
ls –l -a -R
Files and directories - commandstree
The tree can be used to display directories tree of specified directory.tree - tree of current directorytree path
Files and directories - commandscd
It can be used to change the current working directory:
$ cd /home/st2cd .. - to parent directorycd .cd - to “HOME” directory
“HOME” is home directory of the logged user. For example:If you are logged as st4 your “HOME” directory is
/home/st4 or just ~
Files and directories - commandsmkdir
The mkdir command can be used to create directories. $
mkdir directory_name
It might happen that you want to create a directory in a parent directory which does not exist yet.
For example, if you want to create the test2/hello/directory, but the test2 directory does not yet exist. In this case you can make both directories with only one mkdir command:
mkdir -p ~/test2/helloOr you want to create many directories:
mkdir dir1 dir2 ./dir3 mkdir dir1 dir2 dir3
Files and directories - commandsrm (rmdir)
The rm command is used to remove files and directories.
(rmdir remove only empty directories)rm sm_name
Sometimes the rm asks for a confirmation. You canignore this with the -f parameter.It is also possible to delete directories or even whole
directory trees. You can use the -r parameter to do this.
Many commands allow you to combine parameters. For example :
rm -rf ./dir1/
Files and directories - commandsmv
The mv command is used to move (or rename ) files.
mv path1/name path2mv old_name new_name
It is also possible to move (rename) directories. But, mv always works recursively. For example, the
following command will move the dir1 directory to the dir2 directory:
mv dir1 dir2
Files and directories - commandscpFiles can be copied with the cp command, the basic
syntax is: cp source destinationIt is also possible to copy a file in the same directory
but with different name. For example:cp ./memo ./memo2
It is also possible to copy directories recursively, this can be done by adding the -R parameter. The following command copies the dir1 directory, and all subdirectories, and (sub)files to the dir2 directory:
cp -R dir1 dir2
Files and directories - commandscatThe cat command is one of the simplest commands
around. Its default behavior is just to send anything it receives on the standard input to the standard output until the end of file character (^D) is sent to the standard input.
You can create a file using this command::cat > file_nameHello world!Testing... 1 2 3^D (=CTRL + D)
You can also view a file content on the standard output:
cat file_name
Files and directories - commandsmore
The more is what we call a pager utility. Oftentimes the output of a particular command is too big to fit on one screen. The individual commands do not know how to fit their output to separate screens. They leave this job to the pager utility.
The more command breaks the output into individual screens and waits for you to press the space bar before continuing on to the next screen. Pressing the enter key will advance the output one line. Here is a good example:
cd /usr/binls –l | more
or more file_name
Files and directories - commandsless
The more command is quite handy, but oftentimes you will find that you have advanced past the screen you wanted.
The more does not provide a way to go back. The less command provides this functionality. It is
used in the same way as the more command, so the previous examples apply here too.
So, less is more than more. ☺
Files and directories - commandstouch
The touch is used to change the timestamp on a file. You can change access timestamps and modification timestamps with this command.
If the file specified does not exist, touch will create a zero length file with the name specified:
touch file1
There are several options for touch, including options to specify which timestamp to modify, the time to use, and many more. The online manual page discusses these in detail
Files and directories - commandsecho
The echo is used to send something to the standard output by specifying it as an argument to echo.
For example, one could use echo to send a simple message to the standard output:
echo "Hello world!"
Hello world!
Finishing sessionexit (logout)
To quit your telnet session, use either the exit command or the logout command.
To restart your host use reboot command. It has some parameters (see man reboot).
To shutdown your host use shutdown or haltcommand with some parameters.
For restart or shutdown host you must work on the host (console), not remotely.
Example 1:• In your HOME directory create subdirectories sdir1, sdir2
and sdir3. • In sdir1 create subdirectory temp and empty file named
ffile.• Show content of your HOME directory on the screen • File list of your HOME directories and subdirectories put
in file ~/HOME_list• Rename file HOME_list to listH• Remove sdir1 directory• Show files which name begin with letter p from /dev• Show your home directory tree
Example 2:• In your HOME directory create subdirectory tmp. • In tmp create file named student which contains your
first name.• Back in root directory • Show content of created file student without moving from
root directory• Move file student to your HOME directory• Copy all files which name begin with i from /etc to tmp
directory in your HOME.• Show content of /etc directory and its subdirectories page
by page• Delete tmp directory
Example 3:• In your HOME directory create subdirectories t1 and t2. • List of all files which name begin with s from /dev put in
new file ~/s_dev.• Try to copy s_dev file to / directory.• Copy file s_dev to t1 subdirectory.• Move t1 directory to the t2 and change his name to
newt1• Copy newt1 to your HOME directory• Remove newt1 from t1 subdirectory • Check how are you logged
File permissions and ownershipLinux is a multi-user operating system. Every aspect
of the system is multi-user, even the filesystem. The system stores information like who owns a file
and who can read it, …The filesystem stores ownership information for each
file and directory on the system. This includes what owner and group own a particular
file.The easiest way to see this information is with the ls
–l command
-rw-r--r-- 1 st21 users 9253 Dec 23 19:12 sample.ttt
File permissions
Moreover, permissions are the other important part of the multi-user aspects of the filesystem. With these, you can change who can read, write, and execute files.
As you might have noticed, file permissions can be shown with the ls -l command:
$ ls -l sample.ttt
-rw-r--r-- 1 st21 users 9253 Dec 23 19:12 sample.ttt
The permissions are shown in the first column.
File permissions Permissions that can be set are:
– read (r),– write (w),– execute (x).
These permissions can we set for three classes: – owner/user (u), – group (g), or all (a)– others (o).
File permissions The permissions are visible in the second to ninth
character in the first column. These nine characters are divided in three group;1. the first three characters represent the permissions
for the owner (u ser),2. the next three characters represent the permissions
for the group,3. finally the last three characters represent the
permissions for other users.
- rw- r-- r-- 1 st21 users 9253 Dec 23 19:12 sample.tttThe example file can be written(w) to by the owner and can be
read(r) by all three classes of users.
File permissions Each GNU/Linux system has many distinct users, anda user can be a member of certain groups. This kind of user management provides makes it
possibleto manage detailed permissions for each file.
In the example shown above st21 is the owner of the file sample.ttt and group permissions apply to the group users.
In this example groups rights do not differ from the rights of other users.
File permissions chownThe chown command is used to set the file owner and
to which group group permissions should apply to. You must be loged as root if you want to change file
ownership or group. Suppose we want to make st5 the owner of the file
sample..ttt, this can be done with the chown$ chown st5 sample.ttt
-R
The group can be changed by adding a dot after the owner, followed by the name of the group (st5.users).
File permissions chmod
File permissions can be manipulated using the chmodcommand. You must be a file owner if you want to change permission (or root).
The most basic syntax of chmod is:chmod [u,g,o,a][+/-][r,w,x] filename
Parameters determine the following elements: 1. which classes this manipulation permission applies
to (u, g, o, a), 2. if the permissions should be added (+) or removed
(-), and 3. which permissions should be manipulated (r,w,x).
File permissions Suppose we want to make the file memo writable for
the owner of the file and the groups for which the group permissions apply.
This can be done with the following command:
$ chmod ug+w memo
Just like the chown command it is also possible to do recursive (-R) operations.
File permissions Another way for using chmod codmmand is:
$ chmod xyz file_name
where 0 <= x, y, z <= 7 (x, y, z are integer numbers).
If you want to make this permissions: rwx rw- r--
We can express that in binary form: 111 110 100Or in decimal form: 7 6
4
Finally:$ chmod 764 file_name
Example 1:• In your HOME directory create subdirectory perm. • In perm create files: my_f, our_f and shared_f.• Show which are default permissions on these files.• Are permissions same for all new files and directories?• Change permissions for my_f such that owner will have
read and write,but group and other will not have any permission
• Change permissions for our_f such that owner and group will have read and write but other wont have any permission.
• Change permissions for shared_f such that owner, group and other (all users) will have read and write permission.
• Change permissions for perm such that owner, group and other (all users) can see and enter in this directory.
• Which permissions you established above?
Example 2/1:• In your HOME directory create subdirectory def_dir. • Change permissions for def_dir such that owner will have
only read permission– Can you see def_dir content?– Can you enter in def_dir subdirectory ?– Can you rename the subdirectory?– Can you delete the subdirectory?– Can you copy the subdirectory?
• Change permissions for def_dir such that owner will have only execute permission– Can you see def_dir content?– Can you enter in def_dir subdirectory ?– Can you rename the subdirectory?– Can you delete the subdirectory?– Can you copy the subdirectory?
Example 2/1 - answers:• If your directory has just read permisson YOU:
– Can see def_dir content.– Can not enter in def_dir subdirectory– Can rename the subdirectory– Can not delete the subdirectory– Can copy the subdirectory
• If your directory has just execute permisson YOU– Can not see def_dir content?– Can enter in def_dir subdirectory ?– Can not rename the subdirectory?– Can not delete the subdirectory?– Can not copy the subdirectory?
Example 2/2:Change permissions for def_dir such that owner will
have only write permission– Can you see def_dir content?– Can you enter in def_dir subdirectory ?– Can you rename the subdirectory?– Can you delete the subdirectory?– Can you copy the subdirectory?
• Change permissions for def_dir such that owner will have read and execute permission– Can you see def_dir content?– Can you enter in def_dir subdirectory ?– Can you rename the subdirectory?– Can you delete the subdirectory?– Can you copy the subdirectory?
Example 2/2 - answers:• If your directory has just write permisson YOU:
– Can not see def_dir content.– Can not enter in def_dir subdirectory– Can rename the subdirectory– Can not delete the subdirectory– Can not copy the subdirectory
• If your directory has read and execute permisson YOU– Can see def_dir content?– Can enter in def_dir subdirectory ?– Can rename the subdirectory?– Can not delete the subdirectory?– Can copy the subdirectory?
Example 3/1:• In your HOME directory create file def_file• Change permissions for def_file such that owner
will have only read permission– Can you see def_file content?– Can you add some text in def_file?– Can you rename the file?– Can you delete the file?– Can you copy the file in def_dir subdirectory?
• Change permissions for def_file such that owner will have only write permission– Can you see def_file content?– Can you add some text in def_file?– Can you rename the file?– Can you delete the file?– Can you copy the file in def_dir subdirectory?
Example 3/1 - answers:• If your file has just read permisson YOU
– Can see def_file content– Can not add some text in def_file– Can rename the file– Can not delete the file– Can copy the file .
• If your file has just write permisson YOU– Can not see the file content.– Can add some text in the file.– Can rename the file?– Can delete the file.– Can not copy the file. WR
Example 3/2:• Change permissions for def_file such that owner
will have read and write permission– Can you see def_file content?– Can you add some text in def_file?– Can you rename the file?– Can you delete the file?– Can you copy the file in def_dir subdirectory?– Can you change ownership over this file?– Change file group to root, if you can.
Example 3/2:• If your file has read and write permisson YOU
– Can see the file content.– Can add some text in the file.– Can rename the file.– Can delete the file.– Can you copy the file in some directory.– Can not change ownership over this file. Just root can
do that!– Change file group to root, if you can. You can’t do that
!!
Default permisionYou can change default permissions (permissions for new
file and directory) with command umask
umask p_mask
If you want rwxrw-r- - as default permission (764) you must use p_mask= 777-764 =013 and type
umask 013For files execute permission not allowed as default !
Therefore, above command will give rw-rw-r- -permission for files and rwxrw-r- - for directories .
LinksLinks are pointers between files. With links, you can have files
exist in many locations and be accessible by many names.
There are two types of links: – hard and – soft.
Hard links are names for a particular file. They can only exist within a single directory and are only removed when the real name is removed from the system. These are useful in some cases, but many users find the soft link to be more applicable.
The soft link, also called a symbolic link, can point to a file outside of its directory. It is actually a small file containing the information it needs. You can add and remove soft links without affecting the actual file.
Hard linkln
ln destination_file link_name
For example:ln file_s hardlink2file_s
Hard link not allowed for directories !
Soft linkln -s
ln -s destination_file softlink_name
For example:ln –s file softlink2file_s
Soft link is allowed for directories !
As you might have noticed, number of hard links and subdirectories can be shown with the ls -lcommand:
$ ls -l nameIf name is a file:
-rw-r--r-- 1 st21 users 9253 Dec 23 19:12 namethis is number of hard links
If name is a directory:drw-r--r-- 1 st21 users 9253 Dec 23 19:12 name
this is number of subdirectories
Example 1/1:• In your HOME directory create subdirectory number.
– Check number of subdirectories for number.• Create two subdirectories in number.
– Check number of subdirectories for number.• Create a nonzero file letter in number.
– Check number of subdirectories for number.• Create hard link in your HOME directory on letter file with
name hlink2letter.– Compare size of letter and hlink2letter files. Are
they same?• Create soft link in your HOME directory on letter file with
name slink2letter.– Compare size of letter, slink2letter and hlink2letter
files. Is there some difference? Why ?
Example 1/2:• See content of letter, slink2letter and hlink2letter files.
– Do you see some difference?• Remove file letter.
– See content of slink2letter and hlink2letter files.– Do you see some difference?– Explain that?
• Create hard link in your HOME directory on number with name hlink2number.– Can you do that operation? Why?
• Create soft link in your HOME directory on number with name slink2number.– Can you do that operation?– Can you enter to slink2number?
Mounting Devices
As you know all the drives and devices in Linux are one big filesystem. Various hard drive partitions, CD-ROMs, floppies, USB memories are all placed in the same tree.
In order to attach these drives to the filesystem so that you can access them, you have to use the mountand umount commands.
Some devices are automatically mounted when you boot up your computer. These are listed in the /etc/fstab file. Anything that you want to be mounted automatically gets an entry in that file.
For other devices, you'll have to issue a command every time you want to use the device (You must logged as root for manage devices).
Mounting DevicesLinux uses a technique named “mounting” to access
filesystems. Mounting means that a filesystem is connected to a directory in the root filesystem.
Linux supports many kinds of filesystems, like Ext2,Ext3, ReiserFS, JFS, XFS, ISO9660 (used for CD-
ROMs), UDF (used on some DVDs),FAT, FAT32and NTFS.
These filesystems can reside on many kinds of media, for example hard drives, CD-ROMs and Flash drives.
Mounting DevicesmountThe mount is used to mount filesystems. The basic
syntax is: mount /dev/devname /mountpoint
The device name can be any block device, like hard disks or CD-ROM drives.
The mount point can be an arbitrary point in the root filesystem.
mount /dev/cdrom /mnt/cdromThis mounts the /dev/cdrom on the /mnt/cdrom
mountpoint. The /dev/cdrom device name is normally a link to the real CD-ROM device name (for example, /dev/hdc).
Mounting DevicesAs you can see, the concept is actually very simple, it
just takes some time to learn the device names ;).
Sometimes it is necessary to specify which kind of filesystem you are trying to mount.
The filesystem type can be specified by adding the -t parameter:
mount -t vfat /dev/sda1 /mnt/flash
This mounts the vfat filesystem on /dev/sda1 to /mnt/flash (USB flash memory)
Unmounting DevicesumountThe umount command is used to unmount filesystems. The umount accepts two kinds of parameters, mount
points or devices. For example:
umount /mnt/cdromor
umount /dev/sda
The first command unmounts the filesystem that was mounted on /mnt/cdrom, the second commands unmounts the filesystem on /dev/sda1.
Example 2:• In your HOME directory create directory CD• Mount your CD to the new directory• Show content of CD ROM • Copy from CD some files in your HOME
directory• Can you copy some file from your HOME
directory to the CD ROM ?• Unmount CD ROM and eject them.
Example 3:• In your HOME directory create directory Flash• Copy MS word files from your flash memory to
the new directory• Unmount Flash memory• Show content of some copied files • Try to use these files• Remove Flash directory• Which the device name of memory flash you
have used?
The fstab fileAs we seen, Linux system has a special file, /etc/fstab, that specifies
which filesystems should be mounted during the system boot (automatically).
Let’s look at an example:
/dev/hda10 swap swap defaults 0 0/dev/hda5 / xfs defaults 1 1/dev/hda6 /var xfs defaults 1 2/dev/hda7 /tmp xfs defaults 1 2/dev/hda8 /home xfs defaults 1 2/dev/hda9 /usr xfs defaults 1 2/dev/cdrom /mnt/cdrom iso9660 noauto,owner,ro 0 0/dev/fd0 /mnt/floppy auto noauto,owner 0 0
As you can see each entry in the fstab file has five entries: fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, and fs_passno.
The fstab filefs_specThe fs_spec option specifies the block device,
or remote filesystem that should be mounted. As you can see in the example several /dev/hda partitions are specified, as well as the CD-ROM drive and floppy drive.
fs_fileThe fs_file specifies the mount point. This can
be an arbitrary directory in the filesystem.
The fstab filefs_vfstypeThis option specifies what kind of filesystem the entry
represents. For example this can be: ext2, ext3, reiserfs, xfs, nfs, vfat, or ntfs.
(CD-ROM drives are iso9660, and Windows-baseddevices will either be msdos or vfat)
fs_mntopsThe fs_mntops option specifies which parameters
should be used for mounting the filesystem. The mount manual page has an extensive description of the available options (man mount). “Defaults” is fine for just about everything, “ro” is read only, ....
The fstab filefs_freqIf the “fs_freq” is set to 1 or higher, it specifies after
how many days a filesystem dump (backup) has to be made. This option is only used when dump is set up correctly to handle this.
fs_passnoThis field us used by fsck to determine the order in
which filesystems are checked during the system boot.
Example 3:• Find file fstab• Which permission you have on this file?• Can you see content of fstab ?• Show on screen and explain fstab content.
Disk spaceHow many bytes on the disk use some directory
du
How check available disk space:df
in kilobytes:df -k