Sunday, 29 March 2015

Linux File System

       In the Linux file structure files are grouped according to purpose. Ex: commands, data files, documentation. Several major directories are associated with all modern Unix/Linux operating systems. These directories organize user files, drivers, kernels, logs, programs, utilities, and more into different categories. The standardization of the FHS [Filesystem Hierarchy Standard] makes it easier for users of other Unix-based operating systems to understand the basics of Linux.

       Every FHS starts with the root directory, also known by its label, the single forward slash (/). All of the other directories shown in Table are subdirectories of the root directory. Unless they are mounted separately, you can also find their files on the same partition as the root directory.

root(/)  The home directory for the root user
/home Contains the user's home directories along with directories for services
ftp
HTTP
samba 
/bin Commands needed during bootup that might be needed by normal users
/sbin Like bin but commands are not intended for normal users. Commands run by LINUX.
/proc This filesystem is not on a disk. It is a virtual filesystem that exists in the kernels
imagination which is memory.  A directory with info about process number  and each process has a directory under proc.
/usr Contains all commands, libraries, man pages, games and static files for normal operation.
bin
Almost all user commands. some commands are in /bin or /usr/local/bin.
sbin
System admin commands not needed on the root filesystem. e.g., most server programs.
include
Header files for the C programming language. Should be below /user/lib for consistency.
lib
Unchanging data files for programs and subsystems
local
The place for locally installed software and other files.
man
For manual pages
info 
For Info documents
doc
For Documentation
/boot Files used by the bootstrap loader, LILO. Kernel images are often kept here.
/lib Shared libraries needed by the programs on the root filesystem
modules
Loadable kernel modules, especially those needed to boot the system after
disasters.
/dev Stored Device files
/etc Configuration files specific to the machine.
skel
When a home directory is created it is initialized with files from this directory
sysconfig
Files that configure the linux system for devices.
/var Contains files that change for mail, news, printers log files, man pages, temp files
lib
Files that change while the system is running normally
local
Variable data for programs installed in /usr/local.
lock
Lock files. Used by a program to indicate it is using a particular device or file
log
Log files from programs such as login and syslog which logs all logins and
logouts.
run
Files that contain information about the system that is valid until the system is
 next booted
spool
Directories for mail, printer spools, news and other spooled work.
tmp
Temporary files that are large or need to exist for longer than they should in /tmp.
catman
A cache for man pages that are formatted on demand
/mnt Mount points for temporary mounts by the system administrator.
/tmp Temporary files. Programs running after bootup should use /var/tmp.

Types of Files Used by Linux
       When working with Linux, you need to be aware of the fact that there are a number of different file types used by the file system. This is another area where the Linux file system differs significantly from the Windows file system. In Linux, there are a variety of different file types used by the file system. These include the file types shown in Table:
File Type Description
Regular files
These files are similar to those used by the file systems of other operating systems—for example, executable files, OpenOffice.org files, images, text configuration files, etc.
Links
These files are pointers that point to other files in the file system.
FIFOs
FIFO stands for First In First Out. These are special files used to move data from one running process on the system to another. A FIFO file is basically a queue where the first chunk of data added to the queue is the first chunk of data removed from the queue. Data can only move in one direction through a FIFO.
Sockets
Sockets are similar to FIFOs in that they are used to transfer information between sockets. With a socket, however, data can move bidirectionally.

The File system in reality
       For most users and for most common system administration tasks, it is enough to accept that files and directories are ordered in a tree-like structure. The computer, however, doesn't understand a thing about trees or tree-structures.
       Every partition has its own file system. By imagining all those file systems together, we can form an idea of the tree-structure of the entire system, but it is not as simple as that. In a file system, a file is represented by an inode, a kind of serial number containing information about the actual data that makes up the file: to whom this file belongs, and where is it located on the hard disk.
       Every partition has its own set of inodes; throughout a system with multiple partitions, files with the same inode number can exist.
       Each inode describes a data structure on the hard disk, storing the properties of a file, including the physical location of the file data. When a hard disk is initialized to accept data storage, usually during the initial system installation process or when adding extra disks to an existing system, a fixed number of inodes per partition is created. This number will be the maximum amount of files, of all types (including directories, special files, links etc.) that can exist at the same time on the partition. We typically count on having 1 inode per 2 to 8 kilobytes of storage.

At the time a new file is created, it gets a free inode. In that inode is the following information:

  • Owner and group owner of the file.
  • File type (regular, directory, ...)
  • Permissions on the file
  • Date and time of creation, last read and change.
  • Date and time this information has been changed in the inode.
  • Number of links to this file (see later in this chapter).
  • File size
  • An address defining the actual location of the file data.

       The only information not included in an inode, is the file name and directory. These are stored in the special directory files. By comparing file names and inode numbers, the system can make up a tree-structure that the user understands. Users can display inode numbers using the -i option to ls. The inodes have their own separate space on the disk.
$ ls -il
total 1
 639036 -rw-r--r--    1 Anil     Administ       24 Mar  1 20:12 file1.txt
2044316 -rw-r--r--    1 Anil     Administ       24 Mar  1 21:26 file2.txt
You can see the inode number of files [file1.txt & file2.txt] highlighted in Red.

Tuesday, 24 March 2015

How to delete user in Linux

To delete user, you can use userdel command. Refer below syntax:
$ userdel [options] <username>

Example:
$ userdel anil

To remove the user's home directory pass the -r option to userdel:
$ userdel -r anil

Default values of userdel command are taken from configuration file /etc/login.defs  for RHEL (Red Hat) based distributions. Debian and Ubuntu Linux based system use /etc/deluser.conf file:

 # /etc/deluser.conf: deluser configuration - Debian / Ubuntu Linux only.

# Remove home directory and mail spool when user is removed
REMOVE_HOME = 0

# Remove all files on the system owned by the user to be removed
REMOVE_ALL_FILES = 0

# Backup files before removing them. This options has only an effect if
# REMOVE_HOME or REMOVE_ALL_FILES is set.
BACKUP = 0

# target directory for the backup file
BACKUP_TO = "."

# delete a group even there are still users in this group
ONLY_IF_EMPTY = 0

# exclude these filesystem types when searching for files of a user to backup
EXCLUDE_FSTYPES = "(proc|sysfs|usbfs|devpts|tmpfs)"

Sunday, 22 March 2015

How to add user in linux

To add user, you can use useradd command in linux. Refer below syntax:

useradd [options] <user-name>

Example: useradd anil

While creating users as mentioned above, all the default options will be taken except group id. To view the default options give the following command with the option -D.

$ useradd -D
GROUP=1001
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
GROUPThis is the only option which will not be taken as default. Because if you don’t specify -n option a group with same name as the user will be created and the user will be added to that group. To avoid that and to make the user as the member of the default group you need to give the option -n.
HOMEThis is the default path prefix for the home directory. Now the home directory will be created as /home/USERNAME.
INACTIVE-1 by default disables the feature of disabling the account once the user password has expired. To change this behavior you need to give a positive number which means if the password gets expired after the given number of days the user account will be disabled.
EXPIREThe date on which the user account will be disabled.
SHELLUsers login shell.
SKELContents of the skel directory will be copied to the users home directory.
CREATE_MAIL_SPOOLAccording to the value creates or does not create the mail spool.

To create user with custom configurations using useradd Command:
The below example creates an account "anil" with home directory /home/linuxbasics, default shell as /bin/ksh and with comment “Linuxinanutshell admin”.

$ useradd -s /bin/ksh -m -d /home/linuxbasics -c “Linuxinanutshell admin” -g root anil

$ grep anil /etc/passwd
anil:x:1083:0:Linuxinanutshell admin:/home/linuxbasics:/bin/ksh

To change default options in configuration file:
As we mentioned earlier to change the default values during command execution, there is another option to change default values for account creation, which is defined in /etc/default/useradd file under most of the Linux distributions. Open this file in a text editor, ie.:
$ vi /etc/default/useradd

The default home directory defined by HOME variable, find line that read as follows:
HOME=/home

Replace with:
HOME=/linuxbasics/user

Save and close the file. Now you can add user using regular useradd command:
$ useradd anil


To change existing User's Home Directory:
You need to use the usermod command to set the user's new login directory.

Syntax: usermod -m -d /path-to-new-home-dir userName

-d dirnanme : Path to new login (home) directory.
-m : The contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.

Example: usermod -m -d /users/linuxinanutshell/anil anil
In above example set the user's new login directory to /users/linuxinanutshell/anil from /home/anil

To set Password for user:
$ passwd anil
Changing password for user anil.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

[Note: Always set the password immediately after user creation]

Saturday, 7 March 2015

Kill or terminate process in Linux

Kill command is used to kill a process in Linux.The kill command sends the specified signal such as kill process to the specified process. If no signal is specified, the SIGTERM signal is sent.

Syntax:

kill [signal] PID

A list of common signals:
SIGHUP-(1) Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to reload configuration files and open/close log files
SIGKILL-(9) Kill signal. Use SIGKILL as a last resort to kill process. This will not save data or cleaning kill the process
SIGTERM-(15) Termination signal. This is the default and safest way to kill process

Example:
In example, we are terminating process java with -15 signal, which will terminate the process safely.

Points to remember:

  • You can kill all your own process.
  • Only root user can kill system level processes.
  • Only root user can kill process started by other users.

To kill multiple process in single command, mention all PID's in command:
kill -15 PID1 PID2 PID3


To kill process with its name:
You can kill any process by specifying its full name or partial name. So there is no need for you to find out the PID of the process to send the signal.
pkill java
Further, you can also kill process with its name using killall command but with killall, you need to specify full name of process.


Note: There are many kill signals that each serve a particular purpose. Typing "kill -l" will list the kill signals. 



Sunday, 1 March 2015

Find difference between 02 files in linux

To compare two text files in Linux, use the diff command. In nutshell, diff compares the contents of the two files from-file and to-file.
Syntax: diff [options] from-file to-file2
Following options can be used with diff command:
-b Ignore changes in amount of white space.
-B Ignore changes that just insert or delete blank lines.
-i Ignore changes in case; consider upper- and lower-case letters equivalent.
--new-file In directory comparison, if a file is found in only one directory, treat it as present but empty in the other directory.
-r When comparing directories, recursively compare any subdirectories found.
Example:
Suppose you have a file 'file1.txt': 
and another file 'file2.txt':
Now, if you execute the diff command 'diff file1.txt file2.txt', then it will display the output like this:
Lines "1d0" and "6a6" are the coordinates and types of the differences between the two compared files, while lines like "< aa" and "> kk" are the differences themselves. Diff change notation includes 2 numbers and a character between them. Characters tell you what kind of change was discovered:

a – line added

c – line changed

d – line deleted

Left number of the character defines the line number in the first file, and Right number of the character defines the line number in the second file.
Output details:
1d0
< aa
It means that 1 line was deleted. < aa denotes that the aa line is present only in the first file.
3c2
< cc
---
> kk
It means that the line#3 has changed from "cc"[in first file] to "kk"[in second file].
6a6
> gg
It means that one new line added in the second file, it's "gg" at line#7.