Unix-2-converted
Unix-2-converted
The following system files (i.e. directories) are present in most UNIX file systems:
o bin - short for binaries, this is the directory where many commonly used executable
commands reside
o dev - contains device specific files
o etc - contains system configuration files
o home - contains user directories and files
o lib - contains all library files
Unit 2 2
ln name-of-real-file name-of-physical-link
Soft Link/Symbolic Link
o This command will tell you how many users are currently logged into the system.
o The standard output from the “who” command is a list of all the users currently
logged into the system.
o This output is piped into the wc command as its standard input.
o Used with the -l option this command counts the numbers of lines in the standard
input and displays the result on its standard output - your terminal.
File Names
UNIX permits file names to use most characters, but avoid spaces, tabs and characters that
have a special meaning to the shell, such as:
& ; ( ) | ? \ ' " ` [ ] { } <> $ - ! /
Case Sensitivity: uppercase and lowercase are not the same! These are three different files:
NOVEMBER November november
link count of the parent directory is increased by one for every sub-directory the parent
contains.
Every directory also contains the name "." (dot), a map to the directory itself, so the smallest
link count of any Unix directory is 2: one for the map in the parent directory that gives the
directory its "name", and one for the dot map in the directory itself.
Finally aninode is handle to a file and contains the following information:
o file ownership indication
o file type (e.g., regular, directory, special device, pipes, etc.)
o file access permissions. May have setuid (sticky) bit set.
o time of last access, and modification
o number of links (aliases) to the file
o pointers to the data blocks for the file
o size of the file in bytes (for regular files), major and minor device numbers for special
devices.
An integral number of inodes fits in a single data block.
Information the inode does not contain:
o path (short or full) name of file
For example, when a user issues open(``/etc/passwd'', ...) the kernel performs the following
operations:
1. Because the file name is a full path name, find the inode of the root directory (found in
superblock) and search the corresponding file for the entry ``etc''
2. when the entry ``etc'' is found, fetch its corresponding inode and check that it is of type
directory
3. scan the file associated with ``/etc'' looking for ``passwd''
4. Finally, fetch the inode associated with passwd's directory entry, verify that it is a regular
file, and start accessing the file.
Note: What would the system do when opening ``/dev/tty01''?
Eventually, the system would find the inode corresponding to the device, and note that its file
type was ``special''.
Thus, it would extract the major/minor device number pair from the length field of the inode,
and use the device number as an index into the device switch table.
Example
Suppose the root directory has node number #2. Here is a small part of a Unix file system
tree, showing hypothetical node numbers:
Note how one directory (#555) has three name-to-number maps for the same node.
All three names (cp, ln, mv) refer to the same node number, in this case a file containing an executable
program. (This program looks at its name and behaves differently depending on which name you use to
call it.)
Node #111
Node #333
. (dot) Node #335
111
Example
Here are two shell programs that are linked into different directories under different names.
The only way you can tell which names point to the same program files is by looking at the
inode numbers using the "-i" option to ls:
$ls -i /sbin/sh /usr/bin/sh
136724 /sbin/sh 279208 /usr/bin/sh
$ncheck -i 279208,136724
/dev/dsk/c0t3d0s0:
279208 /usr/lib/rsh
136724 /sbin/jsh
136724 /sbin/sh
279208 /usr/bin/jsh
279208 /usr/bin/sh
The ncheck command is usable only by the Super User. It finds all pathnames that lead to a
particular inode.
Unit 2 7
Now that we have a basic understanding of how filesystems work, we'll turn our attention to
understanding how filesystems influence the security of a Unix system. Nearly all of this
discussion will be concerned with the metadata that a filesystem contains?the filenames,
permissions, timestamps, and access control attributes.
You can use the ls command to list all of the files in a directory. For instance, to list all the files
in your current directory, type:
% ls
instructions invoice letter more-stuff notes stats
%
Actually, ls alone won't list all of the files. Files and directories beginning with a dot (".") are
hidden from the ls command but are shown if you use ls -a:
% ls -a
. .. .indent instructions invoice letter notes more-stuff stats
%
The entries for "." and ".." refer to the current directory and its parent directory, respectively.
The file .indent is a hidden file. If you use ls -A instead of ls -a, you'll see hidden files, but "."
and ".." will not be shown.
You can get a more detailed listing by using the ls -lF command:
% ls -lF
total 161
-rw-r--r-- 1 sian user 505 Feb 9 13:19 instructions
-rw-r--r-- 1 sian user 3159 Feb 9 13:14 invoice
-rw-r--r-- 1 sian user 6318 Feb 9 13:14 letter
-rw------- 1 sian user 15897 Feb 9 13:20 more-stuff
-rw-r----- 1 sian biochem 4320 Feb 9 13:20 notes
-rwxr-xr-x 1 sian user 122880 Feb 9 13:26 stats*
%
The first line of output generated by the ls command (total 161 in the example above)
indicates the number of KBs taken up by the files in the directory. Each of the other lines of
output contains the fields, from left to right, as described in Table 6-2.
File Permissions
The file permissions on each line of the ls listing tell you what the file is and what kind of file
access (that is, the ability to read, write, or execute) is granted to various users on your system.
-rw-------
drwxr-xr-x
The first character of the file's mode field indicates the type of file (described in Table 6-4).
The next nine characters taken in groups of three indicate who on your computer can do what
with the file. There are three kinds of permissions:
Permission to read
Permission to write
x
Unit 2 9
Permission to execute
Owner
Group
Other
The file command determines the file type of a file. It reports the file type in human readable
format (e.g. ‘ASCII text’) or MIME type (e.g. ‘text/plain; charset=us-ascii’). As filenames in
UNIX can be entirely independent of file type file can be a useful command to determine how to
view or work with a file.
To determine the file type of a file pass the name of a file to the file command.The file name
along with the file type will be printed to standard output.
file file.txt
file.txt: ASCII text
file -b file.txt
ASCII text
The file command can be useful as filenames in UNIX bear no relation to their file type. So a file
called somefile.csv could actually be a zip file. This can be verified by the file command.
file somefile.csv
somefile.csv: Zip archive data, at least v2.0 to extract
How to determine the file type of multiple files
The file command can also operate on multiple files and will output a separate line to standard
output for each file.
Unit 2 10
file unix-*.md
unix-cat.md: ASCII text, with very long lines
unix-comm.md: ASCII text, with very long lines
unix-cut.md: UTF-8 Unicode text
unix-exit-status.md: ASCII text
unix-file.md: ASCII text, with very long lines
How to view the mime type of a file
To view the mime type of a file rather than the human readable format pass the -i option.
file -i file.txt
file.txt: text/plain; charset=us-ascii
This can be combined with the -b option to just show the mime type.
file -i -b file.txt
text/plain; charset=us-ascii
Changing Permissions
To change the file or the directory permissions, you use the chmod (change mode) command.
There are two ways to use chmod — the symbolic mode and the absolute mode.
The easiest way for a beginner to modify file or directory permissions is to use the symbolic
mode. With symbolic permissions you can add, delete, or specify the permission set you want by
using the operators in the following table.
+
1
Adds the designated permission(s) to a file or directory.
-
2
Removes the designated permission(s) from a file or directory.
=
3
Sets the designated permission(s).
Here's an example using testfile. Running ls -1 on the testfile shows that the file's permissions
are as follows −
$ls -l testfile
Unit 2 11
Then each example chmod command from the preceding table is run on the testfile, followed by
ls –l, so you can see the permission changes −
The second way to modify permissions with the chmod command is to use a number to specify
each set of permissions for the file.
Each permission is assigned a value, as the following table shows, and the total of each set of
permissions provides a number for that set.
0 No permission ---
Here's an example using the testfile. Running ls -1 on the testfile shows that the file's permissions
are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile, followed by
ls –l, so you can see the permission changes −
While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the
permissions mentioned above are also assigned based on the Owner and the Groups.
Two commands are available to change the owner and the group of files −
• chown − The chown command stands for "change owner" and is used to change the
owner of a file.
• chgrp − The chgrp command stands for "change group" and is used to change the
group of a file.
Changing Ownership
The chown command changes the ownership of a file. The basic syntax is as follows −
The value of the user can be either the name of a user on the system or the user id (uid) of a
user on the system.
NOTE − The super user, root, has the unrestricted capability to change the ownership of any file
but normal users can change the ownership of only those files that they own.
The chgrp command changes the group ownership of a file. The basic syntax is as follows −
The value of group can be the name of a group on the system or the group ID (GID) of a group
on the system.