Unix fundamentals

1,750 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,750
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • Shell is a Command Intrepreter www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • Hierarchical File System Information is stored on the disk in containers known as files. Every file is assigned a name, and user accesses a file by referencing its name. Files normally contain data, text, programs, and so on. In the UNIX system, a directory can be used to store files or other directories. Multi-tasking In the Unix system several tasks can be performed at the same time. From a single terminal, a single user can execute several programs that all seem to be running simultaneously. Programming facility The UNIX shell is also a programming language; it was designed for a programmer, not casual end user. It has all the necessary ingredients like control structures, loops and variables that establish it as a powerful programming language on its own right. These features are used to design shell scripts- programs that also include UNIX commands in their syntax. www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • A file has several characteristics associated with it. They can be displayed using the ls –l command. Type regular file or special file. Permission or mode access definition for the file. Links number of file names associated with a single collection of data. Owner user identification of file owner Group group identification for file access Size number of bytes file contains Timestamp date file last modified Name maximum of 14 characters www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • Who Has Access The three classes of users are: • Owner — Usually the person who created the file. • Group — Several users that have been grouped together by the system administrator. For example, the members of a department might belong to the same group. • Other — All other users on the system. www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • You should also be aware of the default permissions assigned to all of your files and directories at the time you create them. You can list or change the default permission settings by using the umask command. Default file permissions are assigned by the system whenever you create a new file or directory, and these are governed by your umask setting. To restrict these default permissions for newly-created files and directories, use the umask command. When a new file is created, each bit in the file mode creation mask that is set causes the corresponding permission bit in the file mode to be cleared (disabled); hence the term mask. Conversely, bits that are cleared in the mask allow the corresponding file mode bits to be enabled in newly created files. www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • setuid: When set-user identification (setuid) permission is set on anexecutable file, a user or process that runs this executable file is granted access based on the owner of the file (usually root) instead of the user who started the executable. For eg. the executable file /usr/bin/passwd is set with setuid so that when any nonprivileged user runs the command the effective user id is not of the user but that of root. Because of that the password is updated in the file /etc/shadow though the normal user dont have any write permission on this file. -r-sr-xr-x 1 root sys 17156 Jan 5 17:03 /usr/bin/su setgid: The set-group identification (setgid) permission is similar to setuid,except that the effective group ID of the user or the process is changed to the group owner of the file. Also, access is granted based on the permissions assigned to that group. For example, the mail program has a setgid permission used to read mail, or send mail to other users. -r-x--s--x 1 root mail 61288 Jan 5 16:57 /usr/bin/mail The setgid permission is a useful feature for creating shared directories. When a setgid permission is applied to a directory, files created in the directory belong to the group to which the directory belongs. Sticky Bit: If the directory has the Sticky Bit set, a file can be deleted only by the owner of the file, the owner of the directory, or by root. This prevents a user from deleting other users’ files from publicly writable directories. For example: # ls -ld /tmp drwxrwxrwt 6 root sys 719 May 31 03:30 /tmp www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • The vi editor is a powerful, versatile editing tool. The vi editor is included with every versions of UNIX. www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • l or -> will move the cursor right by one character h or <- will move the cursor left by one character j or down arrow will move the cursor one line down k or up arrow will move the cursor up by one line up w or W will move cursor to the next word b or B will move cursor to the previous word 0 will move cursor to the beginning of the line $ will move cursor to the end of the line 1G will move cursor to the first line of the file G will move cursor to the last line of the file <ctrl-f> will move cursor down by a page <ctrl-b> will move cursor up by a page H will move cursor to the top of the current page M will move cursor to the Middle of the current page L will move cursor to the bottom of the current page www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • PID: Process ID PPID: Parent Process ID UID: User ID ps command will only show the processes started by the current user. ps -af will display all the processes started by all users ps -ef will display all the processes including the system processed www.scmGalaxy.com, Author - Rajesh Kumar www.scmGalaxy.com, Author - Rajesh Kumar
  • Unix fundamentals

    1. 1. Unix Basics Prepared by Dima Gomaa
    2. 2. What Is an Operating System?• Interface between Users and the Hardware• Take care of Storage Management• Take care of I/O device management
    3. 3. History of the UNIX Operating System 1969 AT&T Bell Labs UNICS system -designed by Ken Thompson & Dennis Ritchie 1970 UNICS finally became UNIX 1973 UNIX rewritten in C, making it portable to different hardware Mid 1970s University of California at Berkeley (BSD) contributed many important features like vi, C shell etc. 1982 AT & T came back and started commercial production- Editions ->Systems Late 1980s AT & T released SVR4 unification of SV3.2,BSD,SunOs & XENIX 1991 Linux from Linus Torvalds 1990s POSIX Standard, MIT introduced X-Windows 3
    4. 4. UNIX Architecture – Kernel & ShellUser Shell Kernel Hardware 4
    5. 5. More Features of UNIX• Hierarchical file system• Multi tasking• Multi user• Pattern matching (wildcard characters)• Programming facility• Documentation 5
    6. 6. Command Line FormatSyntax: $ command [-options] [arguments]1. Separation2. Order3. Multiple options4. Multiple arguments 6
    7. 7. Basic Commands(1) ls list files and directories ls -a list all files and directories mkdir make a directory cd directory change to named directory cd change to home-directory cd ~ change to home-directory cd .. change to parent directory pwd display current dir path useradd add new user in the system usermod modify user userdel delete user groupadd add new group in the system
    8. 8. Basic Commands(2) cp file1 file2 copy file1 and call it file2 mv file1 file2 move or rename file1 to file2 rm file remove a file rmdir directory remove a directory cat file display a file more file display a file a page at a time who list users currently logged in lpr -Pprinter psfile print postscript file to named printer * match any number of characters ? match one character man command read the online manual page for a command
    9. 9. Basic Commands(3) command > file redirect standard output to a file command >> file append standard output to a file command < file redirect standard input from a file grep keyword file search a file for keywords % grep science science.txt wc file count number of lines/words/characters in file % wc -w science.txt sort sort data (numerically or alphabetically) Ex: to sort the list of object, type % sort < biglist and the sorted list will be output to the screen.
    10. 10. The File System Hierarchy / usr etc home dev var tmpbin sbin dsk rdsk adm sadm ls who 10
    11. 11. File Path nameA sequence of file names, separated by slashes (/), thatdescribes the path, the system must follow to locate afile in the file systemAbsolute pathname (start from the /-directory): Eg: /export/home/user1/file1Relative pathname (start from the current directory) ./test1 (. = current directory) ../team03/.profile (.. = parent directory) 11
    12. 12. File Characteristics $ ls -l -rw-r--r-- 1 user3 class 37 Jul 24 11:06 f1 -rwxr-xr-x 1 user3 class 52 Jul 24 11:08 f2 drwxr-xr-x 2 user3 class 1024 Jul 24 12:03 memoFile Links Group NameType Timestamp Owner Size Permissions 12
    13. 13. Who can access a File?The UNIX system incorporates a three-tier structure to define who has access to each file and directory: user The owner of the file group A group that may have access to the file other Everyone else• The ls -l command displays the owner and group who has access to the file. $ ls -l -rw-r--r-- 1 user3 class 37 Jul 24 11:06 f1 -rwxr-xr-x 1 user3 class 37 Jul 24 11:08 f2 drwxr-xr-x 2 user3 class 1024 Jul 24 12:03 memo | | owner group 13
    14. 14. File system security (access rights) -rwxrwxrwx a file that everyone can read, write and execute (and delete). -rw------- a file that only the owner can read and write - no- one else can read or write and no-one has execution rights (e.g. your mailbox file).
    15. 15. Unix File Permissions File type, owner, group, othersdrwx------ 2 jjoshi isfac 512 Aug 20 2003 risk managementlrwxrwxrwx 1 jjoshi isfac 15 Apr 7 09:11 risk_m->risk management-rw-r--r-- 1 jjoshi isfac 1754 Mar 8 18:11 words05.ps-r-sr-xr-x 1 root bin 9176 Apr 6 2002 /usr/bin/rs-r-sr-sr-x 1 root sys 2196 Apr 6 2002 /usr/bin/passwd File type: regular -, directory d, symlink l, device b/c, socket s, fifo f/p Permission: r, w, x, s or S (set.id), t (sticky)
    16. 16.  chmod [options] file change access rights for named file For example, to remove read write and execute permissions on the file biglist for the group and others, type % chmod go-rwx biglist This will leave the other permissions unaffected. To give read and write permissions on the file biglist to all, % chmod a+rw biglist
    17. 17. Default PermissionsThe default protections for newly created files and directoriesare: File -rw-r—r-- 644 Directory drwxr-xr-x 755These default settings may be modified by changing the umaskvalue. 17
    18. 18. umask - Permission MaskUmask specifies what permission bits will be set on a newfile or directory when created.New Directory: 777 – 022 755  rwxr-xr-xNew File : 666 – 022 644 rw-r—r—The default value of umask is set in /etc/profile. This can bechanged for all the users or a particular user 18
    19. 19. chown - Change File OwnershipSyntax:chown owner [:group] filename ... Changes owner of a file(s) and, optionally, theExample: group ID $ id uid=101 (user1), gid=101 (group1) $ cp f1 /tmp/user2/f1 $ ls -l /tmp/user2/f1 -rw-r----- 1 user1 group1 3967 Jan 24 13:13 f1 $ chown user2 /tmp/user2/f1 $ ls -l /tmp/user2/f1 -rw-r----- 1 user2 class 3967 Jan 24 13:13 f1Only the owner of a file (or root) can change the ownership of the file. 19
    20. 20. The chgrp CommandSyntax: chgrp newgroup filename ...Example: $ id uid=303 (user3), gid=300 (class) $ ls -l f3 -rw-r----- 1 user3 class 3967 Jan 24 13:13 f3 $ chgrp class2 f3 $ ls -l f3 -rw-r----- 1 user3 class2 3967 Jan 24 13:13 f3 $ chown user2 f3 $ ls -l f3 -rw-r----- 1 user2 class2 3967 Jan 24 13:13 f3 20
    21. 21. su - Switch User IdSyntax: su [user_name] Change your effective user ID and group IDExample: $ ls -l f1 -rwxr-x--- 1 user1 group1 3967 Jan 24 23:13 class_setup $ id uid=303 (user1), gid=300 (group1) $ su – user2 Password: $ id uid=400 (user2), gid=300 (group1) 21
    22. 22. Special File Permissions – setuid, setgid,sticky bit setuid – changes the effective user id of the user to the owner of the program  chmod u+s f1 or – chmod 4744 f1 setgid – changes the effective group id of the user to the group of the program  chmod g+s f1 or  chmod 2744 f1 sticky bit – ensures the deletion of files by only file owner in a public writable directory  chmod +t f1 or  Chmod 1744 f1 22
    23. 23. What Is vi?• A screen-oriented text editor• Included with most UNIX system distributions• Command driven• Categories of commands include – General administration – Cursor movement – Insert text – Delete text – Paste text – Modify text 23
    24. 24. Starting and Ending a vi Sessionvi [filename] Start a vi edit session of fileExample:$ vi testfile- If the file doesn’t exist, it will be created- Otherwise vi will open the existing fileAll modifications are made to the copy of the file brought intomemory.:wq or :x or <shift-zz> write and quit:w write:q quit:q! Quit without saving 24
    25. 25. Cursor Control Commands 1G <ctrl-b> H k <up-arrow> 0 $ b,B w,W h l<left-arrow> <right-arrow> <down-arrow> j L <ctrl-f> G 25
    26. 26. Input Mode: i, a, O, oi will insert character at the present cursor positionI will insert character at the beginning of the linea will append character at the present cursor positionA will append character at the end of the lineo will insert a blank line belowO will insert a blank line above 26
    27. 27. Deleting Text: x, dw, dd, dGx deletes the current characterdw deletes the current worddd deletes the current linedG delete all lines to end of file, including current line.With any of these you can prefix a numberExample: 3dd will delete 3 linesd$ to delete to the end of the lined0 to delete to the start of the line 27
    28. 28. Copying, moving and changing Textyy copy the lineyw copy the word p will paste the yanked lines belowdw cut the word P will paste the yanked lines abovedd cut the liner will overwrite the current characterR will replace all text on the right of the cursor positioncw will replace only the current word 28
    29. 29. Searching for Text: /, n, N/director will locate for the first occurrence of thepattern ‘director’n to locate the next occurrenceN to locate the previous occurence 29
    30. 30. Monitoring ProcessThe ps command dislays process status information$ ps -fUID PID PPID ... TTY ... COMMANDjohn 202 1 ... tty0 ... -kshjohn 204 202 ... tty0 ... kshjohn 210 204 ... yyu0 ... ls -R /john 212 204 ... tty0 ... ps -f 30
    31. 31. Shell Scripting Shell scripting skills have many applications, including: Ability to automate tasks, such as  Backups  Administration tasks  Periodic operations on a database via cron  Any repetitive operations on files Increase your general knowledge of UNIX  Use of environment  Use of UNIX utilities  Use of features such as pipes and I/O redirection
    32. 32. Examples of Shell Scripting Store the following in a file named simple.sh and execute it #!/bin/sh # Show some useful info at the start of the day date echo Good morning $USER cal last | head -6 Shows current date, calendar, and a six of previous logins Notice that the commands themselves are not displayed, only the results

    ×