690 likes | 860 Views
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.
E N D
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 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
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
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
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
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
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
Links • Hard link: • Creating a „true” second file • (same inode) • also a record in the inode table • ln targetnew_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
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
Symbolic (soft) link • Make the files and directories available with a different name - eg: compatibility issues, simpler access • ln -s targetnew_syslink • Symbolic link points to a file or directory name • Deleting the original file renders the link useless
Exercise • Create a file, writeyournameintoit. • Create a hard link thatpointstoit. Howmanylinkspointtoit? • Create a soft link thatpointstoit. Howmanylinkspointtoitnow? • Modifytheoriginal file. Whatdoyouseeintheotherfiles? • Modifythehardlink. Whatdoyouseenow? • Modifythesoft link. Whatdoyouseenow?
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 poserne.valeria@nik.bmf.hu 13
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
Permissions (2) • Setting permission: chmod number file • For example: 754 means the following 7 5 4 15
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 16
Access permissions • ls –l • -rw-rw-rw- 1 demo guest 23456 Aug 23 20:23 file1 • drwxrw-rwx 1 demo ... • lrwxrwxrwx ... • Meaning: (first column) - regular file d directory p named pipe l symbolic link c character device b block device rwx r-- rw- owner groupothers permission - deny w write r read x execute 17
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 18
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). 19
chmod command I. echo „first example” >example chmod 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 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). 20
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 21
Pop quiz • chmod 123 file • chmod 777 file • chmod 533 file • chmod 217 file • chmod 182 file • chmod a=x file 22
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
Permissions - special flags (2) • 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
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---
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.
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
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
Let's edit text - nano nano filename menu bar: ctrl + key ctrl+x: quit ctrl+o: save ctrl+w: search
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
Let's edit text - joe • joe filename • quit: ctrl + k, ctrl + x
Let's edit text - emacs • I don't know emacs, but it is popular • Anyone?
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)
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
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
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
Exercise 1 solution #!/bin/bashfirst="I don't know"second="me neither"echo $first $secondexit 0
Exercise 2 Let's create a shell script where we have two variables. They should have numerical values, and add them together. What happens?
Exercise 2 solution #!/bin/bashfirst=40second=50echo 40+50exit 0
Apostrophes - spaces mess thingsup ' ' : 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
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
math in bash • expr 3 + 4 • number=`expr 3 + 4`
Exercise 2.5 • Make a shell script that sums the numbers it gets as parameters
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.
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 >
Solution to Exercise 3 #!/bin/bashls -l $1 > `date +%F_%H-%M`.txtor ls -l $1 > "`date`"
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
Solution to Exercise 4 #!/bin/bash ln -s /bin/cat $1
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
Exercise 5 #!/bin/bash chmod 740 $1