unix/linux basics 0010

69
Unix/Linux basics 0010 Operating systems lab Gergely Windisch [email protected] obuda.hu room 4.12

Upload: holmes-adkins

Post on 01-Jan-2016

64 views

Category:

Documents


2 download

DESCRIPTION

Unix/Linux basics 0010. Operating systems lab Gergely Windisch windisch.gergely@nik. uni-obuda .hu room 4.12. unix filesystems (1). Unix supports many filesystems Filesystems are not accessed via drive id mounted into the / mount point Virtual file system layer makes it unique. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Unix/Linux basics 0010

Unix/Linux basics0010

Operating systems labGergely Windisch

[email protected] 4.12

Page 2: Unix/Linux basics 0010

unix filesystems (1)• Unix supports many filesystems• Filesystems are not accessed via drive id

– mounted into the /• mount point

• Virtual file system layer makes it unique

http://www.csie.ntu.edu.tw/~pangfeng/System%20Programming/Lecture_Note_2.htmAkik az Advanced Programming in the Unix Environment (Richard Stevens) könyvből vették

Page 3: Unix/Linux basics 0010

unix filesystems (2) - VFS

http://www.csie.ntu.edu.tw/~pangfeng/System%20Programming/Lecture_Note_2.htmAkik az Advanced Programming in the Unix Environment (Richard Stevens) könyvből vették Source: http://tldp.org/LDP/tlk/fs/filesystem.html

Page 4: Unix/Linux basics 0010

unix filesystems (3) - drives

http://www.csie.ntu.edu.tw/~pangfeng/System%20Programming/Lecture_Note_2.htmAkik az Advanced Programming in the Unix Environment (Richard Stevens) könyvből vették

Page 5: Unix/Linux basics 0010

unix file systems (4)

Page 6: Unix/Linux basics 0010

Inode

• inode, cornerstone of all file storage– contains information about the file

• inode identifies the data itself (inode table)– link to the actual data– access times– owners, permissions etc.

• Name is not part of the inode– name is just a record in the directory file

• ls –i: print inode numbers• http://www.tutorialhero.com/click-42976-

speaking_unix:_it’s_all_about_the_inode.php

Page 7: Unix/Linux basics 0010

inode (2) - demonstration• mkdir fruits, cd fruits• touch apple, ls -i• touch orange, ls -i• the inode numbers are different

– numbers are in a stricly increasing manner• ls -ali : . and .. are visible

– what can you see concerning . and ..?– cd .. && ls –ali : anything interesting now?

• the indices of directories are also increasing, but starts from a different number

Page 8: Unix/Linux basics 0010

i-nodes

http://www.csie.ntu.edu.tw/~pangfeng/System%20Programming/Lecture_Note_2.htmAkik az Operation System Concepts (Silberschatz ,Galvin) könyvből vették

Page 9: Unix/Linux basics 0010

Links

• Hard link:– Creating a „true” second file

• (same inode)– also a record in the inode table– ln target new_link– Size, permission are in the inode table. The name is

stored in the directory --> creating new links (names) to the same inode

– Deleting one does not affect the others (there is no original)

– ls -l shows how many links point to that file

Page 10: Unix/Linux basics 0010

• Problem with hard link: only on the same filesystem

• cp: creates new inode• mv: same inode – only in the same file system• touch mule• ls -il• cp mule horse• mv mule hamster

Page 11: Unix/Linux basics 0010

• Symbolic (soft) link– Make the files and directories available with a

different name - eg: compatibility issues, simpler access

– ln -s target new_syslink– Symbolic link points to a file or directory name– Deleting the original file renders the link useless

Page 12: Unix/Linux basics 0010

Exercise

• Create a file, write your name into it.• Create a hard link that points to it. How many links

point to it?• Create a soft link that points to it. How many links

point to it now?• Modify the original file. What do you see in the

other files?• Modify the hardlink. What do you see now?• Modify the soft link. What do you see now?

Page 13: Unix/Linux basics 0010

[email protected] 13

Security measures in linux

• Login using username and password – cannot access anything without it

• Filesystem protection: files and directories have permissions

– File access permissions:

• r - read

• w - write

• x – execute (enter directories)

– The system stores permissions for the owner, owner group and everyone else

Page 14: Unix/Linux basics 0010

Permissions (1)

• chmod – set permissions• Owner/Owner group/Everyone else• read: 4 (list directories)• write: 2 (modify contents – create, delete)• execute: 1 (enter directory)

– if you cannot enter, you cannot list either

Page 15: Unix/Linux basics 0010

15

Permissions (2)

Setting permission: chmod number file

• For example: 754 means the following

Owner Owner group Others

r w x r w x r w x

4 2 1 4 0 1 4 0 0

7 5 4

Page 16: Unix/Linux basics 0010

16

Permissions (3)

Changing owner of objects: chown

chown owner file (or chown owner.group file)

Changing owner group: chgrp newgroup file

• pl. chgrp users letter

Page 17: Unix/Linux basics 0010

17

Access permissions

ls –l- rw-rw-rw- 1 demo guest 23456 Aug 23 20:23 file1d rwxrw-rwx 1 demo ... l rwxrwxrwx ...

• Meaning: (first column)- regular filed directoryp named pipel symbolic linkc character deviceb block device

rwx r-- rw-owner group others

permission

- denyw writer readx execute

Page 18: Unix/Linux basics 0010

18

Modifying access permissions I.

chmod [R] files: (read=4, write=2, execute=1 )pl. owner: read, write, execute (4+2+1=7)

group members: read, execute (4+1=5)eveyone else: read (4)the octal code is: 754

chmod 754 file1ls -l file1-rwxr-xr-- 1 demo guest 18 Aug 23 20:42 file1

Page 19: Unix/Linux basics 0010

19

Modifying access permissions II.Other way:

'u' (user : owner) '+' : grant right (add)'g' (group ) '-' : deny right (substract)'o' (others) '=‘ : make permissions exactly like

that'a' (all)

chmod a+x file1 ( executable for everyone (a+x))ls -l file1-r-xr-xr-x 1 demo guest ... (only the executable bit)

chmod u=rw file1ls -l file1-rw-r-xr-x 1 demo ... (owner will have read and write permissions, regardless of

previous state).

Page 20: Unix/Linux basics 0010

20

chmod command I.

echo „first example” >examplechmod u+x example or chmod 744 example

Execute rights for the user.chmod go-rw example

Read and write permissions to the group and others (nothing else changes).mkdir textschmod -R a+X texts

Recursively giving executable permissions to the content of the texts directory

X gives execution rights only to executable typeschmod o= example

Denying all the rights from the others (nothing else changes).

Page 21: Unix/Linux basics 0010

21

A chmod command II.

chmod a=r example or chmod 444 exampleRead permissions for everyone, nothing else.

chmod 750 exampleOwner can read, write, execute, group can read and execute,

others cannot do anything

chmod u=rwx examplechmod g=rx example chmod o= example

A szimbolikus jogok alkalmazásával

Page 22: Unix/Linux basics 0010

22

Pop quiz

• chmod 123 file• chmod 777 file• chmod 533 file• chmod 217 file• chmod 182 file

• chmod a=x file

Page 23: Unix/Linux basics 0010

Permissions - special flags (1)

• sticky bit: chmod +t filename– Obsolete for executables (keep in memory)– directories: only the owner of the files can delete them– useful for /tmp, shared ftp directories

• suid (set user id): chmod +s filename– program is executed with the owners permissions– for example copy to directories writeable only to root– could be considered a security threat

• sgid: – like suid, but with the group

Page 24: Unix/Linux basics 0010

• SUID, SGID, Sticky is the first number when there are 4 digits

• sticky: 1• sgid: 2• suid: 4• pl: chmod 4777 file

– 4: suid– 777: regular permissions

Permissions - special flags (2)

Page 25: Unix/Linux basics 0010

Exercise

• Create a directory. Create 3 files in that directory. Set different permissions for each file (for example: rw-rw-rw, r-x,r-x,---,rwxr---r---

Page 26: Unix/Linux basics 0010

Exercise 2

• Create a directory called public• Set the permissions for the directory to

• Let the user hallgato do everything with it.• Let the users of the group hallgato read the contents (ie.

open the files inside)• Anyone not part of the hallgato group should be denied

access altogether• The owner of the file should be the only one that has

permissions to delete the files.

Page 27: Unix/Linux basics 0010

Let's edit text - vi(m)

• user friendly, but chooses his friends carefully• important, because it is there on all unixes• vi is the original, we'll use vim (VI iMproved)• vim filename• vi: http://www.eng.hawaii.edu/Tutor/vi.html• vim: http://www.vi-improved.org/tutorial.php

Page 28: Unix/Linux basics 0010

Let's edit text - vi(m)

• 2 modes: insert, command - esc, i (insert)• quit: esc, :q, :wq, :q!• save: :w• delete the current line: dd (6dd: delete 6 lines)• copy the current line: yy (6yy: copy 6 lines)• paste the content of the buffer: p

Page 29: Unix/Linux basics 0010

Let's edit text - nano

• nano filename• menu bar: ctrl + key• ctrl+x: quit• ctrl+o: save• ctrl+w: search

Page 30: Unix/Linux basics 0010

Let's edit text - mcedit

• midnight commander editor• mcedit filename• F2: save, F3 select• install if not installed - on opensolaris

– add new software somewhere.– pkg list -s | grep packagename– pkg search -l packagename– pkg install -v packagename

Page 31: Unix/Linux basics 0010

Let's edit text - joe

• joe filename• quit: ctrl + k, ctrl + x

Page 32: Unix/Linux basics 0010

Let's edit text - emacs

• I don't know emacs, but it is popular• Anyone?

Page 33: Unix/Linux basics 0010

Shell scripting

• Multiple commands in one file• #!/bin/bash - first line - bash is the "compiler"• chmod a+x filename• ./filename• shell scripts are really powerful and useful.

There are many small commands which we can put together in a shell script to create one big application (that is the unix way)

Page 34: Unix/Linux basics 0010

Our first shell script

• #!/bin/bash# That's how the comments workecho "Shell scripts rule"exit 0

• exit 0 is not necessary, but good practice– tell the shell that all is well– remember the && and ||: that's how it works

Page 35: Unix/Linux basics 0010

Using variables

• number=43othervariable="oneword"other2="could be multiple words"– other2=that will result in severe error messages

• no spaces around the = !!!! (Really important)• don't forget to put ""-s around strings• accessing variables: $• echo $other2

Page 36: Unix/Linux basics 0010

Exercise 1

• Let's create a shell script where we have two variables. Add values to both and then print them both on the screen

Page 37: Unix/Linux basics 0010

Exercise 1 solution

• #!/bin/bashfirst="I don't know"second="me neither"

echo $first $second

exit 0

Page 38: Unix/Linux basics 0010

Exercise 2

• Let's create a shell script where we have two variables. They should have numerical values, and add them together.

• What happens?

Page 39: Unix/Linux basics 0010

Exercise 2 solution

• #!/bin/bashfirst=40second=50

echo 40+50

exit 0

Page 40: Unix/Linux basics 0010

Apostrophes - spaces mess things up

• ' ' : treat everything that's inside literally – echo '$first'– will print $first

• " " : use the special characters inside the string– echo "$first"– will print the value of $first

• ` ` : run command (alt gr+7 - hungarian keys)– echo `date` – runs date and then substitutes the result

Page 41: Unix/Linux basics 0010

Handing user input - parameters

• $# : number of command line parameters• $1..9: value of the nth parameter• $0 : name of the current shell script• $* : all the parameters in one big script

Page 42: Unix/Linux basics 0010

math in bash

• expr 3 + 4• number=`expr 3 + 4`

Page 43: Unix/Linux basics 0010

Exercise 2.5

• Make a shell script that sums the numbers it gets as parameters

Page 44: Unix/Linux basics 0010

Exercise 3

• Write a shell script which takes a parameter from the user, and lists the contents of the directory specified in the parameter. The result should go in a file called the actual date. The format of the filename should be year-month-day_hour-minute.

Page 45: Unix/Linux basics 0010

Hint - Exercise 3

• get the date formatting using man date

• ambigous redirect means that the shell thinks that there are more than one files after >

Page 46: Unix/Linux basics 0010

Solution to Exercise 3

• #!/bin/bashls -l $1 > `date +%F_%H-%M`.txt

or

• ls -l $1 > "`date`"

Page 47: Unix/Linux basics 0010

Exercise 4

• Create a shell script which takes an input parameter, and creates a symbolic link with the given name that points to /bin/cat

Page 48: Unix/Linux basics 0010

Solution to Exercise 4

#!/bin/bashln -s /bin/cat $1

Page 49: Unix/Linux basics 0010

Exercise 5

• Create a shell script which takes an input parameter, and sets the permissions of the file that was given so that the owner can have all rights, group should have read permissions, and no rights for the others

Page 50: Unix/Linux basics 0010

Exercise 5

#!/bin/bashchmod 740 $1

Page 51: Unix/Linux basics 0010

Evaluation

• eval command• eval can run a command that sits in variable• commandvariable=”ls -l /home | sort”• eval $commandvariable

Page 52: Unix/Linux basics 0010

Exercise 6

• Create a shell script which takes input parameters and runs the command that was given (for example: runme ls -l /home/hallgato)

Page 53: Unix/Linux basics 0010

Bad solution to Exercise 6

#!/bin/bash`$*`

What happens here?

Page 54: Unix/Linux basics 0010

Solution to Exercise 6

#!/bin/basheval $*

Page 55: Unix/Linux basics 0010

Mathematics

•expr command•calculator with 5 commands

•|,&: or, and0 (0 when true, 1 when false)•<,<=,=,==,!=,>=,>•+,-,*,/,% (remainder)•n1=20 n2=10 sum=`expr $n1 + $n2`

Page 56: Unix/Linux basics 0010

Compression

• tar (tape archiever)• tar -cvzf nameoffile.tar.gz *

– pack and compress everything in cwd

• tar -xvzf nameoffile.tar.gz– unpack the contents of nameoffile.tar.gz

• Switches– man tar– x: eXtract, c: create, v: verbose, z: gzip, f: filename

Page 57: Unix/Linux basics 0010

Exercise 6.5

• Use tar to compress all your shell scripts into one file

Page 58: Unix/Linux basics 0010

Solution to Exercise 6.5

tar -cvzf myShellScripts.tar.gz *

Page 59: Unix/Linux basics 0010

Exercise 7

• Create a shell script which takes two numbers as input parameters and add them together (those of you who already know ifs should write a full-featured calculator)

Page 60: Unix/Linux basics 0010

Solution to Exercise 7

#!/bin/bashsum=`expr $1 + $2`echo "The sum of $1 and $2 is $sum"

Page 61: Unix/Linux basics 0010

Exercise 8

• Write a shell script that adds the current date (date command) and the current uptime (uptime command) to the ~/uplog file. There should be a separator after these two data, so that the next run can be identified easily.

Page 62: Unix/Linux basics 0010

Solution to Exercise 8

#!/bin/bashnewfile="~/uptime"date >> $newfileuptime >>$newfileecho "----------------" >>$newfileecho " " >>$newfile

Page 63: Unix/Linux basics 0010

Exercise 9

• Write a shell script that takes two input parameters from the user, and then creates a symbolic link pointing to the file denoted by the first parameter with the name provided as the second parameter.

Page 64: Unix/Linux basics 0010

Solution to Exercise 9

#!/bin/bashln -s $1 $2

Page 65: Unix/Linux basics 0010

Exercise 10

• Write a shell script that prints the contents of the PATH (the one which holds the names of the directories where the shell would look for an executable) variable to a file. The filename is provided in the first input parameter.

Page 66: Unix/Linux basics 0010

Solution to Exercise 10

#!/bin/bashecho "Contents of \$PATH: $PATH" > $1

Page 67: Unix/Linux basics 0010

Exercise 11

• Write a shell script that takes the name of a directory as an input parameter and adds that directory to the PATH variable. Be careful not to overwrite the original content, just add it (and use the standard separator of that variable).

• Advanced versions (require for and if)– check if the directory exists– if the user provides more than one dir, add them as

well

Page 68: Unix/Linux basics 0010

Solution to Exercise 11

#!/bin/bashPATH="$PATH:$1"

Page 69: Unix/Linux basics 0010

Upload your precious work

• If you want, you can send your work to me. If the solutions are correct (or interesting), it could count in your final grades.

• Send it using the commands:– sudo dhclient eth2 (password: nik119)– scp name_of_tar [email protected]:~/

• (password: hallgato)

• Or via email