1
Unix Way
Unix approach to
Files
Processes
Devices
Files
Files are central to UNIX
Commands
System privileges and permission control
Device I/O
Inter-process communication
3
File ownership
File Ownership
User owner
Group owner
The group owner is independent of user owner
Example: Displaying file ownership “ls –l”
drwxrwxr-x 2 pop 403 512 Sep 24 11:47 acm drwxr-xr-x 9 pop csdept 1024 Oct 13 15:54 acm.contest -rw-r--- 1 john csdept 261 Mar 11 1999 address
More about file ownership
Who owns new files?
Owner is the one who create it
Group owner
On System V, current group of the user
On BSD-based systems, the group owner of the directory
Changing file ownership
Use chown , chgrp
Traditional System V, both superuser and user owner are allowed
POSIX-compliant SystemV and BSD, only
5
More about file ownership
More about changing file ownership
Use recursive option –R
#chown –R harvey /home/xyz
Use find command
# find /home/xyz –print | xargs chown test
Change owner and group using chown
#chown new-owner:new-group files
Change group owner
#chgrp newgroup file
BSD: non-root users of chgrp must be
The owner of the file and
A member of the new group to change
File permission
File Access Types
Access classes
User access (u)
Group access (g)
Other access (o)
Make it your current directory (cd to it) Run executable
file x
Alter directory contents ( e.g. delete files in it).
Alter file contents.
w
Search directory contents (e.g. use ls, ls *.txt).
View file contents.
r
Meaning on Directory Meaning on File
Access
7
File permission
Setting file protection
chmod accessstring_list files
Access string has three parts:
Access_classes operator access_type
Access classes u, g, o, a
Operator: +, -, =
Access type: any combination of r,w,x
For example:
#chmod g+w testfile
Recursive option: -R
#chmod –R go-rwx /home/xyz
Read the mannual of chmod for more options
File permission
Specifying numeric file modes
Example: r-xr—r– is 544
Specifying the default file mode
umask
Specify the inhibited access permission with a three digit numeric mode.
Example: if umask is 077
077 is 000 111 111
No r,w,x for group and others. Possible rwx for owners.
9
CASE STUDY
New employee Sandy joined the
department and needs to share the access to some files owned by John.
Possible ways:
1. Copy all the files from John to Sandy
2. Make all the files permission 777.
3.
File
Special Purpose Access Modes
Set mandatory file locking on read/writes.
File locking l
Set process group ID on execution SGID
s
Set process user ID on execution SUID
s
Keep executable in memory after exit
Sticky bit t
Meaning Name
Code
11
File
Sticky bit on directories
If set, a user can only delete files that she owns or she has explicit write permission.
Example: /tmp
[ruihong@dafinn ~]$ ls -ld /tmp
drwxrwxrwt 27 root root 4096 Jan 13 14:58 /tmp
Set it with User access class
#chmod u+t /tmp
Setgid access on directories
Files created in this directory will have the same group ownership as the directory itself.
Example: when groups of users need to share a lot of files, with s bit, correct group ownership will be set for new files.
#chmod g+s files
Files
Numerical equivalents for special access modes
Additional octal digit is prepended to the mode
Setuid, setgid, sticky bit
Example
4755: setuid
6755 : setuid and setgid
1777: stick bit
13
File
How to recognize a File access problem
File ownership or protection problem
If root can do it, then …
Example
The temporary files with the same name created by a different users still exist
Application switched the group ownership behind the scene.
A administrator decided to protect /dev/null, which caused some editor stopping working.
inode
Mapping Files to Disks - inode
Data structure
Created when initialize the disk – Max number
Typically, one inode for every 2 – 8Kbytes file storage
Information stored
User owner and group owner ID’s.
File type
Access modes
File creation, access and modification times
Inode modification time
Number of Links to the file
Size of the file
Disk addresses specifying/leading to
No file name in inode,
Where is file name stored?
Where is the mapping between name and inode?
15
File
File Types
Regular files
Directories
A directory is a binary file.
Directory entries are filename-inode pairs
Special files
Two types of special files
Character special files
Block special files
Located under /dev
Links
Several filenames to refer to a single file on disk
Hardlink and softlinks
File
More file types
Unix domain sockets
Communication connection points
Printing system
Syslog (/dev/log)
Named pipes
Also known as FIFO
Communication between programs
Commonly used to avoid writing temporary files
like regular pipe
17
File
Using “ls –l “to identify file types
- Plain file (hard link)
d Directory
l Symbolic link
b Block specific file
c Character special file
s socket
p Named pipe
File
Example:
Show file types with -l
[ruihong@dafinn filetypes]$ lslslsls ----ld * /dev/log /dev/hda1ld * /dev/log /dev/hda1ld * /dev/log /dev/hda1ld * /dev/log /dev/hda1 brw-rw---- 1 root disk 3, 1 Sep 15 2003 /dev/hda1 srw-rw-rw- 1 root root 0 Nov 30 10:08 /dev/log -rw--- 2 ruihong csdept 0 Jan 4 09:43 gold.dat -rw--- 2 ruihong csdept 0 Jan 4 09:43 hlink lrwxrwxrwx 1 ruihong csdept 8 Jan 4 09:43 slink -> gold.dat drwx--- 2 ruihong csdept 512 Jan 4 09:48 testdir
Show types with -F
[ruihong@dafinn filetypes]$ lslslsls ----ldFldFldF * /dev/log /dev/hda1ldF* /dev/log /dev/hda1* /dev/log /dev/hda1* /dev/log /dev/hda1 brw-rw---- 1 root disk 3, 1 Sep 15 2003 /dev/hda1 srw-rw-rw- 1 root root 0 Nov 30 10:08 /dev/log=
-rw--- 2 ruihong csdept 0 Jan 4 09:43 gold.dat -rw--- 2 ruihong csdept 0 Jan 4 09:43 hlink lrwxrwxrwx 1 ruihong csdept 8 Jan 4 09:43 slink -> gold.dat drwx--- 2 ruihong csdept 512 Jan 4 09:48 testdir/
Show hard link –i
[ruihong@dafinn filetypes]$ lslslsls ----ldFildFildFi *ldFi***
2624881 -rw--- 2 ruihong csdept 0 Jan 4 09:43 gold.dat 2624881 -rw--- 2 ruihong csdept 0 Jan 4 09:43 hlink 2624882 lrwxrwxrwx 1 ruihong csdept 8 Jan 4 09:43 slink -> gold.dat 2675566 drwx--- 2 ruihong csdept 512 Jan 4 09:48 testdir/
Check file type using file command, which uses /etc/magic or /usr/share/file/magic
[ruihong@dafinn filetypes]$ file /etc/file /etc/file /etc/file /etc/passwdpasswdpasswd /bin/passwd/bin//bin//bin/lslsls /dev/log /dev/hda1ls/dev/log /dev/hda1/dev/log /dev/hda1/dev/log /dev/hda1
19
Processes
What is a process?
A single program that is running in its own virtual address space.
Process type
Interactive program
Associated with a terminal session
Control on foreground and background
Batch processes
Not associated with any terminal
Submitted to a queue and run it later
UNIX provide the command batch, at
Others: Network Queuing System to support load balancing among workstations in the network.
Processes
Daemons
Server processes
Often initiated at boot time
Run continuously while the system is up
Idle until a request arrives
Examples:
init
syslogd
sendmail
lpd, lpsche, ..
crond,
Gettty
Pagedaemon
Inetd
Named
Nfsd
Ntpd
Portmap, rpcbind
.. …
21
Process
Process attributes
Process id (PID)
Parent process IP (PPID)
Nice number– different from priority
TTY
Real and Effective User ID (RUID, EUID)
Real and Effective Group ID (RGID, EGID)
SUID and SGID File access and Process Execution
Allow ordinary users to perform tasks requiring
privileges and access rights which are ordinarily denied to them
Example:
[ruihong@dafinn ~]$ ls -l /usr/bin/passwd
-r-s--x--x 1 root root 18992 Jun 5 2003 /usr/bin/passwd
Example:
$ps –eo pid,ppid,tty,ruser,euser,rgroup,egroup,cmd
Process Life Cycle
The life cycle of a process
Fork-and-exec
Inherit the env values from parent process
Example:
Run a grep Init
fork Init
Pid 1 Pid 424 exec
Getty
Pid 424 exec login
Pid 424 exec
sh fork sh
23
Processes Controlling
$kill %2 kill
$bg Restart stopped background process bg
$fg %?em Refers to the background job
command containing the characters in str.
%?str
$fg %1 bring background process to
foreground fg
Refers to background job number n
%n
$jobs List background processes
jobs
^Z stop foreground process
^Z
$long-cmd&
Run command in the background
&
Processes and files
The Relationship between commands and files
Build-in shell commands
Executable files
Search path
$PATH
$echo $PATH
/usr/bin:/usr/ucb:/bin:/usr/local/bin:.:/$HOME/bin
The order is important in $PATH
Edit $HOME/.profile or $HOME/.login
Check $PATH for error message “Command not found”
Most administrative utilities are located under /etc,
25
Devices
Allow device I/O operations to look just like file I/O
CASE study
A junior SA was asked to backup all the files under /apg to a tape
The correct tape device name was /dev/rmt2
The backup command should look like
#tar cvf /apg /dev/rmt2
Well, the SA typed the command with a typo
#tar cvf /apg /dev/rnt2
What will happen?
Will the command error out because of the wrong tape?
Will the command finish without error? Where did the stuff go?
As a matter of fact, in the middle of the backup, users started to get errors saying / was full.
Warning: Better to check the tape by reading it after the backup finish.
Devices
Example: disk drives
Disk partitions
Root partition/system disk
Disk special files (partitions, modes)
Mounting a disk partition (filesystem) into the Unix directory hierarchy
Mount /dev/disk0a /home
27
Devices: Naming conventions
On HP-UX,
/dev/rdsk/c2t4d0s0
Where
c2: the controller number
t4: driver number on the controller
d0: logical unit number for SCSI devices
Will be 0 only if more than one disk per target, say storage array.
S0: partition number on that drive
Devices: Naming conventions
Solaris Device Naming Conventions
Referenced in three ways
Physical device name - /devices
Instance name /etc/path_to_inst
Logical device name - /dev (links to /devices)
Linux Device Naming
Default is device type plus an incrementing value based on the order in which devices are found
Ethernet devices: eth0, eth1
SCSI and Serial ATA disks: /dev/sda, dev/sdb
IDE devices: /dev/hda, /dev/hdb
Linux 2.6 kernels and “udev” tool enable administrators
29
Devices: Naming conventions
Example: Special files for Other devices
Null devices.
/dev/null
Swap device /dev/swap
Map of kernel virtual memory /dev/kmem
Console device /dev/console
Virtual terminal console
Serial line (hardwired terminal/modem) /dev/tty1
/dev/ttyS1 /dev/ttyn
/dev/ttySn
Cdrom device /dev/cdrom
Non rewind tape device /dev/nrmt1
/dev/nrmtn
Tape devices /dev/rmt1
/dev/rmtn
Floppy disk /dev/fd0
/dev/[r]fdn*
Device/Use Example
Special file form
Devices
List the devices on a system
HP-UX: ioscan
Tru64 UNIX: hwmgr
Linux:
List pci devices “lspci”
List SCSI devices “scsiinfo –l”
List hard disk parameters “ hdparm”
Solaris:
Print system configuration: prtconf
Lists devices: getdev
31
The Unix Filesystem Layout
Common directories
/
Root directory
/bin
Executables for user commands and utilities.
Some files are links to file under /usr/bin
/dev
Device directory, may includes sub-dirs such as dsk, mnt, pts, etc.
/etc and /sbin
System configuration files and executables
Boot scripts
/etc/default
hold default parameter values for various commands
/home or /usr/users
/lost+found
Files marked as in use on disk, but not listed in any directory – found by fsck
There is a lost+found on every disk partition
The Unix Filesystem Layout
More about common directories
/mnt
Temporary mount directory
/proc
Designed to enable processes to be manipulated using UNIX file accessing system calls.
Linux puts more files about system configuration
/tmp
Available to all users as a scratch directory.
Normally, one of the UNIX startup script will clear /tmp.
/usr
Subdirs for programs, share libraries, administrative commands
/var
Spooling and other volatile directories.
Print spooling, mail system, cron facility
33
The Unix Filesystem Layout
The /usr directory
/usr/bin
Command binary files and shell scripts
X system: /usr/bin/X11
/usr/include
Include files *.h, C-language header files
Operating system include file /usr/include/sys
/usr/lib
Standard C libraries for mathematics and I/O
/usr/local
By convention, /usr/local/bin holds file that were developed locally or retrieved from other sources.
The Unix Filesystem Layout
More about /usr directory
/usr/share
Shared among a group of networked systems for static data files, …
/usr/share/man
Manual pages
Subdir /usr/share/man/man# for every man section
/usr/ucb
Contains standard UNIX commands originally developed under BSD.