The UNIX System Introduction to UNIX: Basic Concepts 1
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to UNIX: basic concepts 2 · GNU/Linux -- various (Debian, RedHat, Ubuntu, SuSE, Gentoo, Mandriva, ...) · FreeBSD, NetBSD, OpenBSD 3. UNIX design Thereare 4 main layers to the UNIX system Introduction to UNIX: basic concepts 1. the kernel P. Regnauld <[email protected]> 2. the shell 3. applications AfNOG 2006 4. users The UNIX system 1. Content Launch processes Background processing User interaction (terminal) Non−interactive Job control • history (no terminal) (scripting) • introduction to UNIX design (kernel, virtual memory,multi-user,filesystem, access-rights) • basic concepts of UNIX (sessions, manuals, users, groups, processes, terminals, shells, sig- DAEMONS nals, job control, ...) SHELLS 2. History KERNEL • long history: over 25 years old (On the Early History and Impact of Unix1 ,Ronda Hauben). • 1969 at Bell Laboratories (pun on «Multics») -- K. Thompson / B. Kernighan APPS File management • Clanguage around 1974 (B. Kernighan / D.Richie) Editors USERS Compilers • Ported to several platforms in C («Portable OS») I/O (+ terminals) Network tools • dsitributed to laboratories / Universities for small fee ($150) Memory management (VM) ... Interruptions • 1978: V7 is the first commercial version Scheduling / Timesharing Networking • two main «branches»: BSD (4.4BSD) and SVSTEM V (SVR4), and now Linux Eat Resources :−) (uid / gid) • most ported operating system -- free versions available Today: over 50 variants (see UGU). Most known: UNIX ®lesystem layout • Solaris -- Sun Microsystems 3.1 the Kernel • HP-UX -- Hewlett Packard • AIX -- IBM · Can be monolithic (Linux, BSD) or micro-kernel (OS X/Darwin) • Irix -- Silicon Graphics, Inc. · preemptive multitasking • MacOS X / Darwin -- Apple · takes careof: · I/O (disk, terminals, printers, tapes, ...) 1. <URL:http://internet-history.org/archives/early.history.of.unix.html> Introduction to UNIX: basic concepts 1 Introduction to UNIX: basic concepts 3 Introduction to UNIX: basic concepts 4 · basic peripheral control (disk, serial, NICs...) 3.4 the Filesystem · memory management (allocation, recovery,Virtual Memory / swap) · timesharing / scheduling of processes 3.4.1 The UNIX philosophy: Everything is a file · networking (IP,etc...) The kernel is always running. It schedules running tasks one at a time (context switching) so that Most of the resources areaccessible through "special ®les" in the ®lesystem. For example: they get an equal shareofthe CPU time. · /dev/da*, /dev/ad* -- the harddisks and partitions /dev/tty*, /dev/pty* P2 · -- terminals and pseudo-terminals Scheduling · /dev/lpt0 -- the printer · ... 5 Other special ®les: · named pipes (FIFO): allow communication between processes · unix sockets: another form of interprocess communication P12 1 kernel 4 P17 · links: pointers to other ®les/directories in the ®lesystem The special ®les above can be manipulated like any other ®le: open, read, write, close. Additionally,some special ®lesystems exist which contain "virtual" ®les - for example, /proc in 3 2 Linux. 3.4.2 Hierarchy P15 P8 UNIX filesystem / ad0a Scheduling of processes dev etc sbin bin usr mnt cdrom boot 3.2 Virtual memory ... ... ... x y z x z y x z y x y kernel On UNIX, the total memory is composed of the physical memory + an on-disk area, commonly ad0d local called "swap" -- this together represents virtual memory. Programs can address the total amount of memory in the system (T = Physical + Virtual). The acd0c swap is usually 2 x the amount of physical RAM in the system. ad0e ... ... bin 3.3 Multi-user UNIX ®lesystem layout Several users can be logged in to the system simultaneously,running each multiple processes The ®lesystem tree has only one root: /.Thereare nomultiple top-level roots like on DOS/Win- (timesharing + scheduling). dows (C:, D:, ...). Each user has shared access to the resources (disk, printers, memory,...) according to his privi- Additional partitions, removal mediums (CD-ROMs), network ®lesystems are mounted on top of leges. an existing directory (see ®gure). Depending on the type of mount, the content of the directory below is visible or not. Introduction to UNIX: basic concepts 5 Introduction to UNIX: basic concepts 6 444 3.5 Access-rights 222 <-values 111 rwx r-x r-x <- permissions Files and directories areprotected with access-rights.They consist of two categories: 111 101 101 <- bit 1. permissions (Read, Write, Execute) 755 <-decimal 2. ownership (User,Group, Others) Below is a sample output of the ls -l command 3.5.1 Various example combinations -rwx r-x r-x 2root staff [...] filename || | | || | ---x--x--x 1 bin bin 65322 26 Jun 19:42 makelist || | | || +-- belongs to group "staff" as well || | | || An execute-only program: the user can only run the program, but cannot read the || | | |+-- belongs to the user "root" content of the program ®le. || | | | || | | +-- link count (number of name references to this file) d-wx-wx-wx 1 bin bin 415Jan 20:54 private || | | Adirectory than one can cd to (change directory), but cannot list ®les, or write ®les. || | +-- permissions for "others" (Read and Execute) But if the user knows the name of a "hidden" ®le, he can access it. || | || +-- permissions for the group (Read and Execute) d-w--w--w- 1 bin bin 415Jan 20:54 private || Special case: hereitisnot possible to even write ®les in the directory: for this to be |+-- permissions for the owner (user) (Read, Write and Execute) possible, the "x" but should be present so that the shell can at least check to see if the | ®le already exists. +-- File type: normal (d = directory, l = link, p = named pipe, ...) 3.5.2 Special bits Additionally,certain extra permissions exist, such as setuid and setgid, which arenot explained here. 4flags for each file: 3.5.2.1 Setuid /Setgid 1. file type: • - plain file The 'x' (eXecute) bit for the User and the Group can be made setuid (SET User ID): • d directory -r-sr-xr-x 1 bob users 12288 21 May 10:43 program • p named pipe In the above example: • s socket · Any user can run the program • l link · When the program runs, it will run with the privileges of user 'bob', since the access rights • b block device (disk, tape) for User are r-s (setuid). • c character device (terminal, serial port, parallel port) Other example: 2. Read, Write and eXecute bits for the User (owner of the file) -r-sr-x--- 1 root wheel 12288 21 May 10:43 program 3. Read, Write and eXecute bits for the Group Here: 4. Read, Write and eXecute bits for the Others · Only users of the group 'wheel' and root itself can launch the program To manipulate these access rights, two commands: · When it runs, it does with the privileges of the 'root' user • chown:change ownership For example: chown root:staff file will change the owner and group of the file to respectively root and staff This makes it possible to restrict commands to a certain group of users. • chmod:change mode. For example: chmod o-rx file or chmod 755 file This above is also true with the setgid (SET Group ID) bit: The 755 above represents the bit-values of the fields above, i.e.: -r-xr-sr-x 1 bin bin 12288 21 May 10:43 program In this case: Introduction to UNIX: basic concepts 7 Introduction to UNIX: basic concepts 8 · all users can run the program 4.4 The manuals · when the program runs, it does with the privileges of group 'bin' 3.5.2.2 The sticky bit Every UNIX system has online a complete set of documentation available with the command man.Type man man for morehelp on man :-) Thereare 9 manual sections: On a directory,itispossible to set what is called the 'sticky' bit: 1. general user commands and utilities 2. system calls drwxrwxrwt 1 bin staff 421May 10:43 files/ 3. C functions and libraries This means that all users can create ®les in this directory,but only the owner of these ®les can 4. devices and drivers modify / delete them. Example: /tmp 5. ®le formats 4. Basic concepts 6. games 7. misc. 4.1 Sessions 8. system maintenance and operation commands 9. kernel interfaces command(sec#) ls(1) To work with UNIX, one has to start a session.This is done by logging in to the system with a The standardnotation is ,asin ,which means "command ls, in section 1". login and password. Once this is done, a command shell is started and the user can start to work. 4.5 Usersand Groups To close the session, use the commands "exit" or "logout". During the time of the session, the user is logged in and is visible to other users (who and w com- mands). Other users can send messages or talk with the user (write and talk). To identify the users on a system, UNIX a combination of users and groups. To see which processes arerunning, one can use the ps and top command (top is not always User areknown by their user id or uid,which is a unique number < 65534, recorded in the pass- available). word®le (/etc/passwd). The group id, or gid allows user to belong to different groups and thus shareresources/®les with other members. It is possible for one user to be logged in several times on the same system, for example using multiple virtual terminals. The user will appear as many time as he is logged in. Only the super-user can add or remove users to/from a group. 4.2 The shell and environment 4.6 Processes, signals and job control The shell (also known as "command-line interpreter" or CLI) is a special application which inter- Everytime a new application, shell or daemon is started, it gets a PID or process id. The list of acts with the user and the system, thus creating an interface between the two.