Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unix/Linux basics 0010 Operating systems lab Gergely Windisch uni-obuda.hu room 4.12.

Similar presentations


Presentation on theme: "Unix/Linux basics 0010 Operating systems lab Gergely Windisch uni-obuda.hu room 4.12."— Presentation transcript:

1 Unix/Linux basics 0010 Operating systems lab Gergely Windisch windisch.gergely@nik. uni-obuda.hu room 4.12

2 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.htm Akik az Advanced Programming in the Unix Environment (Richard Stevens) könyvből vették

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

4 unix filesystems (3) - drives http://www.csie.ntu.edu.tw/~pangfeng/System%20Programming/Lecture_Note_2.htm Akik az Advanced Programming in the Unix Environment (Richard Stevens) könyvből vették

5 unix file systems (4)

6 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

7 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

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

9 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

10 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

11 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

12 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?

13 poserne.valeria@nik.bmf.hu13 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

14 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

15 15 Permissions (2) Setting permission: chmod number file For example: 754 means the following OwnerOwner groupOthers rwxrwxrwx 421401400 754754

16 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

17 17 Access permissions ls –l - rw-rw-rw- 1 demo guest 23456 Aug 23 20:23 file1 d rwxrw-rwx 1 demo... l rwxrwxrwx... Meaning: (first column) - regular file d directory p named pipe l symbolic link c character device b block device rwxr--rw- ownergroupothers permission -deny wwrite rread xexecute

18 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 file1 ls -l file1 -rwxr-xr-- 1 demo guest 18 Aug 23 20:42 file1

19 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 file1 ls -l file1 -rw-r-xr-x 1 demo... (owner will have read and write permissions, regardless of previous state).

20 20 chmod command I. echo „first example” >example chmod u+x example orchmod 744 example Execute rights for the user. chmod go-rw example Read and write permissions to the group and others (nothing else changes). mkdir texts chmod -R a+X texts Recursively giving executable permissions to the content of the texts directory X gives execution rights only to executable types chmod o= example Denying all the rights from the others (nothing else changes).

21 21 A chmod command II. chmod a=r example or chmod 444 example Read permissions for everyone, nothing else. chmod 750 example Owner can read, write, execute, group can read and execute, others cannot do anything chmod u=rwx example chmod g=rx example chmod o= example A szimbolikus jogok alkalmazásával

22 22 Pop quiz chmod 123 file chmod 777 file chmod 533 file chmod 217 file chmod 182 file chmod a=x file

23 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

24 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)

25 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---

26 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.

27 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.htmlhttp://www.eng.hawaii.edu/Tutor/vi.html vim: http://www.vi-improved.org/tutorial.phphttp://www.vi-improved.org/tutorial.php

28 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

29 Let's edit text - nano nano filename menu bar: ctrl + key ctrl+x: quit ctrl+o: save ctrl+w: search

30 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

31 Let's edit text - joe joe filename quit: ctrl + k, ctrl + x

32 Let's edit text - emacs I don't know emacs, but it is popular Anyone?

33 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)‏

34 Our first shell script #!/bin/bash # That's how the comments work echo "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

35 Using variables number=43 othervariable="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

36 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

37 Exercise 1 solution #!/bin/bash first="I don't know" second="me neither" echo $first $second exit 0

38 Exercise 2 Let's create a shell script where we have two variables. They should have numerical values, and add them together. What happens?

39 Exercise 2 solution #!/bin/bash first=40 second=50 echo 40+50 exit 0

40 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

41 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

42 math in bash expr 3 + 4 number=`expr 3 + 4`

43 Exercise 2.5 Make a shell script that sums the numbers it gets as parameters

44 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.

45 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 >

46 Solution to Exercise 3 #!/bin/bash ls -l $1 > `date +%F_%H-%M`.txt or ls -l $1 > "`date`"

47 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

48 Solution to Exercise 4 #!/bin/bash ln -s /bin/cat $1

49 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

50 Exercise 5 #!/bin/bash chmod 740 $1

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

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

53 Bad solution to Exercise 6 #!/bin/bash `$*` What happens here?

54 Solution to Exercise 6 #!/bin/bash eval $*

55 Mathematics expr command calculator with 5 commands |,&: or, and0 (0 when true, 1 when false)‏ =,> +,-,*,/,% (remainder)‏ n1=20 n2=10 sum=`expr $n1 + $n2`

56 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

57 Exercise 6.5 Use tar to compress all your shell scripts into one file

58 Solution to Exercise 6.5 tar -cvzf myShellScripts.tar.gz *

59 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)

60 Solution to Exercise 7 #!/bin/bash sum=`expr $1 + $2` echo "The sum of $1 and $2 is $sum"

61 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.

62 Solution to Exercise 8 #!/bin/bash newfile="~/uptime" date >> $newfile uptime >>$newfile echo "----------------" >>$newfile echo " " >>$newfile

63 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.

64 Solution to Exercise 9 #!/bin/bash ln -s $1 $2

65 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.

66 Solution to Exercise 10 #!/bin/bash echo "Contents of \$PATH: $PATH" > $1

67 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

68 Solution to Exercise 11 #!/bin/bash PATH="$PATH:$1"

69 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 hallgato@10.4.1.6:~/ (password: hallgato) Or via email


Download ppt "Unix/Linux basics 0010 Operating systems lab Gergely Windisch uni-obuda.hu room 4.12."

Similar presentations


Ads by Google