Unix/Linux alapok by l79y007U

VIEWS: 0 PAGES: 70

									       Unix/Linux basics
             0010


        Operating systems lab
           Gergely Windisch
    windisch.gergely@nik.bmf.hu
              room 4.12
http://nik.bmf.hu/gwindisch/os_2010
                                        All about the console
         • The system to start today is OpenSolaris and
           Ubuntu
         • credentials for ubuntu: hallgato / nik119




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 (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
unix file systems (4)
                     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 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
• 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 target new_syslink
  – Symbolic link points to a file or directory name
  – Deleting the original file renders the link useless
                     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?
             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              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
                      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
                                                               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
              Access permissions

                        ls –l
- rw-rw-rw- 1 demo guest 23456 Aug 23 20:23 file1
d rwxrw-rwx 1 demo ...
 l rwxrwxrwx ...
                                rwx             r--      rw-
• Meaning: (first column)       owner        group    others
  - regular file                permission
  d directory
                            -   deny
  p named pipe              w   write
  l symbolic link           r   read
  c character device        x   execute
  b block device                                          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
       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 20
previous state).
        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).

                                                                    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         A szimbolikus jogok alkalmazásával
chmod o= example




                                                                   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
    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=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
                  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/bash
  first="I don't know"
  second="me neither"

 echo $first $second

 exit 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/bash
  first=40
  second=50

 echo 40+50

 exit 0
  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
    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/bash
  ls -l $1 > `date +%F_%H-%M`.txt


  or


• 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
                      Evaluation
• eval command
  • eval can run a command that sits in variable
• commandvariable=”ls -l /home | sort”
• eval $commandvariable
                Exercise 6
• Create a shell script which takes input
  parameters and runs the command that was
  given (for example: runme ls -l /home/hallgato)
      Bad solution to Exercise 6
#!/bin/bash
`$*`



              What happens here?
        Solution to Exercise 6
#!/bin/bash
eval $*
                  Mathematics
•expr command
  •calculator with 5 commands
•|,&: or, and0 (0 when true, 1 when false)‫‏‬
•<,<=,=,==,!=,>=,>
•+,-,*,/,% (remainder)‫‏‬
•n1=20 n2=10 sum=`expr $n1 + $n2`
                 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
                 Exercise 6.5
• Use tar to compress all your shell scripts into
  one file
        Solution to Exercise 6.5
tar -cvzf myShellScripts.tar.gz *
                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)
         Solution to Exercise 7
#!/bin/bash
sum=`expr $1 + $2`
echo "The sum of $1 and $2 is $sum"
                 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.
          Solution to Exercise 8
#!/bin/bash
newfile="~/uptime"
date >> $newfile
uptime >>$newfile
echo "----------------" >>$newfile
echo " " >>$newfile
                 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.
        Solution to Exercise 9
#!/bin/bash
ln -s $1 $2
                 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.
        Solution to Exercise 10
#!/bin/bash
echo "Contents of \$PATH: $PATH" > $1
                 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
       Solution to Exercise 11
#!/bin/bash
PATH="$PATH:$1"
     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

								
To top