Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Saturday, 23 April 2016

How Unix works ?


Unix is a layered operating system.
The innermost layer is the hardware that provides the services for the OS.
The operating system, referred to in Unix as the kernel, interacts directly with the hardware and provides the services to the user programs. These user programs don’t need to know anything about the hardware.
The shell acts as an interface between the user and the kernel.

User programs interact with the kernel through a set of standard system calls. These system calls request services to be provided by the kernel. Such services would include accessing a file: open close, read, write, link, or execute a file; starting or updating accounting records; changing ownership of a file or directory; changing to a new directory; creating, suspending, or killing a process; enabling
access to hardware devices; and setting limits on system resources.

Unix is a multi-user, multi-tasking operating system. You can have many users logged into a system  simultaneously, each running many programs. It’s the kernel’s job to keep each process and user separate and to regulate access to system hardware, including cpu, memory, disk and other I/O devices.

What is the File structure of Unix ?








How to check and repair Unix file system ?


fsck 
is a Unix utility for checking and repairing file system inconsistencies .

File system can become inconsistent due to several reasons and the most common is abnormal shutdown due to hardware failure , power failure or switching off the system without propershutdown. Due to these reasons the superblock in a file system is not updated and has mismatched information relating to system data blocks,free blocks and inodes.

fsck operates in two modes :
Interactive – fsck examines the file system andstops at each error it finds in the file system and gives the problemdescription and ask for user response whether to correct the problem orcontinue without making any change to the file system.

Non interactive – fsck tries to repair all theproblems it finds in a file system without stopping for user responseuseful in case of a large number of inconsistencies in a file systembut has the disadvantage of removing some useful files which aredetected to be corrupt.

fsck   [ -F fstype]  [-V]  [-yY]  [-o options]  special

-F  
fstype type of file system to be repaired ( ufs , vxfs etc)

-V 
verify the command line syntax but do not run the command

-y or -Y  
Run the command in non interactive mode – repair all errors encountered without waiting for user response.

-o  
options Three options can be specified with -o flag

b=n 
where n is the number of next super block if primary super block is corrupted in a file system.

p  
option used to make safe repair options during the booting process.

  
force the file system check regardless of its clean flag.

special  
Block or character device name of the file system to be checked/repaired 

How to check and repair Unix file system ?


fsck 
is a Unix utility for checking and repairing file system inconsistencies .

File system can become inconsistent due to several reasons and the most common is abnormal shutdown due to hardware failure , power failure or switching off the system without propershutdown. Due to these reasons the superblock in a file system is not updated and has mismatched information relating to system data blocks,free blocks and inodes.

fsck operates in two modes :
Interactive – fsck examines the file system andstops at each error it finds in the file system and gives the problemdescription and ask for user response whether to correct the problem orcontinue without making any change to the file system.

Non interactive – fsck tries to repair all theproblems it finds in a file system without stopping for user responseuseful in case of a large number of inconsistencies in a file systembut has the disadvantage of removing some useful files which aredetected to be corrupt.

fsck   [ -F fstype]  [-V]  [-yY]  [-o options]  special

-F  
fstype type of file system to be repaired ( ufs , vxfs etc)

-V 
verify the command line syntax but do not run the command

-y or -Y  
Run the command in non interactive mode – repair all errors encountered without waiting for user response.

-o  
options Three options can be specified with -o flag

b=n 
where n is the number of next super block if primary super block is corrupted in a file system.

p  
option used to make safe repair options during the booting process.

  
force the file system check regardless of its clean flag.

special  
Block or character device name of the file system to be checked/repaired 

What is Sticky bit and how it is used ?


The sticky bit is an access-right flag that can be assigned to files and directories on Unix systems.
When the sticky bit is set, only the item's owner, the directory's owner, or the superuser can rename or delete files. Without the sticky bit set, any user with write and execute permissions for the directory can rename or delete contained files, regardless of owner.

Typically this is set on the /tmp directory to prevent ordinary users from deleting or moving other users' files.
The sticky bit can be set using the chmod command and can be set using its octal mode 1000 or by its symbol t.

For example
To add the bit on the directory /usr/local/tmp, one would type  
chmod +t /usr/local/tmp

Or, to make sure that directory has standard tmp permissions, one could also type  
chmod 1777 /usr/local/tmp

In Unix symbolic file system permission notation, the sticky bit is represented by the letter t in the final character-place.
$ ls -ld /tmp
drwxrwxrwt 4 root sys 485 Nov 10 06:01 /tmp

If the sticky-bit is set on a file or directory without the execution bit set for the others category (non-user-owner and non-group-owner), it is indicated with a capital T :
# ls -l test
-rw-r--r-- 1 root other 0 Nov 10 12:57 test

# chmod +t test; ls -l test
-rw-r--r-T 1 root other 0 Nov 10 12:57 test

Friday, 22 April 2016

What is Kernel ? Explain the task it performs


Kernel is used in xNIX and is considered to be the heart of the operating system.
It is responsible for communication between hardware and software components.
It is primarily used for managing the systems resources as well.

Kernel Activities
  • Allows multiple tasks to run concurrently
  • Managing the computer resources : I/O, CPU, memory
  • Allows the other programs to run and use the resources
  • Process management ; Allows multiple processes to run simultaneously
  • Allows the processes to access the memory and devices

What are the usage of pipes ?


A pipe is a chain of processes so that output of one process (stdout) is fed an input (stdin) to another.

The commands are written in sequence separated by |
Different filters are used for Pipes like AWK, GREP

e.g. sort file | lpr
Sort the file and send it to printer

Uses of Pipe
  • Several powerful functions can be in a single statement using 
  • Streams of processes can be redirected to specified locations, using >

Internal vs. External commands


Internal commands are commands that are already loaded in the system. They can be executed anytime and are independent.
External commands are loaded when the user requests for them.

Internal commands don’t require a separate process to execute them.
External commands will have an individual process.

Internal commands are a part of shell while external commands requires a path.
If the files for the command are not present in the path, the external command won’t execute.

How to use chmod and chown on directory including subdirectories ?


chown
If owner need to be changed on a directory including its recurrent sub-directories.
chown -R jonas:jonas work

chmod
If mode need to be changed on a directory including its recurrent sub-directories.
chmod -R 777 work

How to trace the route to go to a particular site ?


tarecert or traceroute command is used to track the route of the requested URL.
traceroute  [requested URL]

Example
# traceroute www.google.com
Tracing route to google.com [173.194.35.2]
over a maximum of 30 hops:
  1    <1 ms    <1 ms    <1 ms  dps-gateway-del.equant.com [10.239.199.254]
  2     3 ms     3 ms     3 ms  10.237.149.146
  3     5 ms     4 ms     5 ms  57.216.78.25
  4    92 ms    93 ms    95 ms  57.221.160.54
  5   105 ms    96 ms    94 ms  ksin1202-vlan30.ein.equant.com [10.239.219.
  6    93 ms    96 ms    92 ms  sin-bigip1.ein.equant.com [10.239.198.129]
  7    94 ms    91 ms    89 ms  10.237.69.242
  8    93 ms    91 ms    91 ms  57.250.231.252

Sunday, 17 April 2016

What is iNode number and iNode table ?


An inode is a data structure on a traditional Unix-style file system such as UFS. An inode stores basic information about a regular file, directory, or other file system object.

When a file system is created, data structures are created that contain information about files. Each file is associated with an inode that is identified by an inode number (often referred to as an"i-number" or "inode") in the file system where it resides.

Inodes basically store information of files and folders, such asuser and group ownership, access mode (read, write, execute permissions) and type of file.
On many types of file systems the number of inodes available is fixed at file system creation, limiting the maximum number of files the file system can hold.
A typical fraction of space allocated for inodes in a file system is 1% of total size.

The inode number indexes a table of inodes in a known location on the device; from the inode number, the kernel can access the contents of the inode, including the data pointers, and so the contents of the file.

A file's inode number can be found using the ls -i command, while the ls -l command will retrieve inode information (i.e. the file information)
  • Inodes do not contain file names, only file metadata.
  • Unix directories are lists of "link" structures, each of which contains one filename and one inode number.
  • The kernel must search a directory looking for a particular file name and then convert the file name to the correct corresponding inode number if the name is found. 
 
iNode Table in Unix
  • Each (unix) file system has its own inode table; on disk each cylinder group will hold a relevant part of that table.
  • Each inode is referenced by a "device + inode number" pair.
  • Each file is assigned an inode number which is unique within that file system; each directory structure will consist of a list of "filename + inode number" pairs; inodes won't hold filenames. 
  • Reserved inode numbers: 0, 1, 2 
         0: deleted files/directories
         1: (fs dependent) file system creation time/bad blocks count/.....
         2: refers to the root directory of the file system

Some common info that Unix INODE structure contains :
  • The "mode" field will always be the first field in the inode; the order of the other fields is file system dependent 
  • timestamps, access time, modification time
  • inode change time: updated after each modification of one of the fields in the inode (chmod, chown, chgrp, ln, ...)
  • triple indirect pointer: use is fs and max.file size dependent
  • status/flags like "compress file" or "do not update access time" or "do not extend file" are file system dependent
  • extra fields may hold: an inode generation number (for NFS) and/or ACL info (sometimes this field contains a "continuation
  • inode number: a pointer to a special inode that holds ACL info) and/or a file type identification
    (For device files: Major and minor number; For directories: inode number of parent directory);
    All extra/reserved fields are file system dependent !


Example
Using the df command, you can look at all mounted file systems or specific file systems. In this view, you can see the number of inodes used already in the respective file system as well as the percentage used overall in the file system

# df -k|head -6
Filesystem    1024-blocks      Free         %Used     Iused         %Iused    Mounted on
/dev/hd4        229376              138436    40%         4730          13%            /
/dev/hd2        8028160            962692    89%        110034      33%            /usr
/dev/hd9var  1835008            366400    81%        25829        24%            /var

Using istat command, you can find the inumber of the specific file as well as other inode items like permissions; file type; UID; GID; number of links (not symbolic links); file size; and time stamps for last updated, last modified, and last accessed.
# istat /usr/bin/ksh
Inode 18150 on device 10/8 File
Protection: r-xr-xr-x
Owner: 2(bin) Group: 2(bin)
Link count: 5 Length 237804 bytes
Last updated: Wed Oct 24 17:37:10 EDT 2007
Last modified: Wed Apr 18 23:58:06 EDT 2007
Last accessed: Mon Apr 28 11:25:35 EDT 2008

Using the ls -i command, you can view the inumber next to the file name.
# ls –i
38988 38991 -p 38984 fileC 38982 fileF 38977 fileI 38978 fileL
38989 - 38980 fileA 38986 fileD 38983 fileG 38987 fileJ