Vous êtes sur la page 1sur 19

Types of users in linux

1. Root: administrator 2. Super-user: a user other than root, who knows password of root, can get administrative privileges by su command followed by root password. 3. Ordinary users: a user other than root and su with no administrative privileges. Note: user information is stored in /etc/passwd file. Each entry in this file consists of a single line containing a set of seven colon separated fields. login_name:password:UID:GID:Comment:Home_directory:Shell

Powers of Root
Root is equivalent to administrator login in windows. Root has all the powers like i. Changing system date ii. User Management iii. Hard Disk Management iv. Install & Remove softwares. v. Networking services. vi. Changing attributes of files. vii. Initiate or kill any process.

User Attributes A user has 6 attributes: 1. User Name 2. User ID 3. Primary Group 4. Full Name 5. Login Shell 6. Home Directory User Name is to be specified while creation of new user.. User ID value is by default is 500. Root has UID=0. Primary Group by default will be a private group having the same name as the User Name. Full Name by default is empty. Login shell by default is bash Home directory by default also has same name as User Name. To view the default options useradd -D

Group Attributes A group has 3 attributes 1. Group Name 2. Group ID 3. Group Members Group Name is to be specified. Group ID is 500 by default. Root has GID=0 Group members are added during user creation.

Managing Users

I. Adding a user
useradd command is used to create a user. useradd rohit will add new user with User Name rohit and default values of rest of the attributes. { useradd command doesnt update /etc/passwd file, nor creates home directory of new user, but adduser command does so.}. Option -p -u -g -G -s -d -e -f Use Assign password User ID(value should be >499) Existing group of which the new user is the member Additional groups of which the user is the member Default login shell Create Home directory if it doesnt exists Date for account to be disabled in YYYY-MMDD Number of days after which the account is disabled on the expiration of the password. If 0 is specified, the account is disabled immediately, if -1 is specified, the account is not disabled after password expiration. Comment i.e. a string giving some info about the user Example useradd p wipro rohit useradd u 600 rohit useradd g btech rohit useradd G mca, mtech rohit useradd s /bin/ksh rohit useradd -d /subjects/new rohit useradd e 2010-12-31 rohit useradd f 20 rohit

-c

useradd c USIT Delhi rohit

These options can be combined in one statement in any combination like useradd u 700 g DBA d /home/oracle s /bin/ksh da

II. Modifying user properties


usermod command is used to modify the user properties. It uses the same options as useradd to modify the properties. usermod l ravi rohit will rename user rohit as ravi usermod s /bin/ksh da will change default login shell of user da to bash.

III.

Removing user

userdel command is used to delete a user account. userdel da

Change Password

passwd rohit We will be prompted to enter new password, then to retype the new password.

Password Aging For security reasons, it is advisable to users to change their passwords periodically. This can be done using the Password info tab of the User Manager(User Manager -> Properties -> Password info). We can also do the same using chage command Option -l -m -M -I Show aging information Minimum number of days between which password expires. If the value is 0, the password does not expire. Maximum number of days for which password is valid. Number of days after password expiration before disabling the account. During this period account is inactive, and the user must contact the root to be able to use again. Number of days before password expiration to warn the user Specifies the date on which the account is disabled. chage m 30 rohit chage M 90 rohit chage I 5 rohit Use Example

-W -E

chage W 5 rohit chage E 2015-12-31 rohit

I. Adding a group
groupadd command is used to create a new group. groupadd msit

will add new group with User Name rohit and default values of rest of the attributes. groupadd -g 700 msit

II. Modifying group properties


groupmod n csemsit msit {new} {old} groupmod -g 800 msit {New ID} {name} OR groupmod g 800 900 {new ID} {old ID}

III.

Removing group

groupdel csemsit

LINUX FILE SYSTEM: BASIC PRINCIPLES Attributes of a file/directory

i. Permissions ii. Links iii. Owner iv. Owners group v. size vi. date & time of last modification vii. name Permissions Each file/directory has 3 types of permissions: read(r), write(w),execute(x). Links File has link value 1. Directory without any sub-directory has link value 2. The directory containing director(y)ies has link value 3. Owner & Owner Group These attributes tell us who is the owner of the file, and the group to which the owner belongs. Size The fifth attribute tells us about the size of file in bytes. Date & Time of last modification This attribute contains the timestamp of when the file was updated last time. Name This attribute contains the name of the file. ls -l command shows the attributes of all the files in the current directory.

To obtain permissions of a particular file in the current directory ls l chap1 will show the attributes of file chap1 only.

Directory handling commands Command pwd Options Use To know Remark the current

ls

ls -l ls -a ls -r ls -t ls -u ls ls ls ls mkdir rmdir -lt -lu -lut -i

mkdir test1, test2, test3 rmdir test1, test2

cd cd test1 cd ./test1 cd /home/rohit/books cd .. cd ../..

directory(i.e. present working directory) List all files in current Directories are in blue directory, sorted acc . to color. name. Files are in black color. Special files(output files) are in green color. Long listing i.e. show all seven attributes of files Show all hidden files Hidden files start with a dot . Sort file names in reverse order Sort listing by last modification time Sort filenames by last access time Combination of l t Combination of l u Combination of l u -t Displays inode number of files Make directory Remove directory remove sub-directories before main directory, otherwise main directory would not be deleted. Change directory Go from current directory to Here we are giving relative sub-directory test1. pathname of the directory. Go from current directory to Here we are giving absolute directory books. pathname(starting with / i.e. root). Go to parent directory i.e. Here we are giving relative one level up pathname of the directory. Go two levels up

1. Opening a file cat chap1 will open file chap1 cat chap1 chap2 will show output of file1 followed by of file2 (concatenation).

2. Creating a files cat > chap2 [enter] Delhi is capital of India. [Ctrl-D] will create a file chap2 with the content Delhi is capital of India. To close a file we use [Ctrl-D]. 3. Copying a file cp chap1 chap2 chap1 contents are copied to chap2. cp chap1 chap2 book chap1 chap2 files are copied into directory book 4. Renaming a file mv chap1 chap01 will rename chap1 as chap01 5. Moving a file mv chap1 chap2 book chap1 chap2 files are moved to directory book 6. Deleting a file rm chap1 chap2 will remove files chap1 and chap2 7. Printing a file lp chap1 will print file in default printer lp d hp chap1 if multiple printers are attached, we have to give printer name (e.g. hp) with d option. lpstat command is used to view print queue, and to cancel any print job we use cancel command with printer-name or request-id of job as argument. lpstat cancel hp

Pagers To view a file, we need pagers to show a file one page at a time. Linux offers two pagers more & less. less is the standard pager of linux.

more chap1 less chap2 more Spacebar or f 20f b 15b [Enter] less Spacebar or f or z b j or [Enter] k p G /was ?was h q h q Action One page forward 20 pages forward One page back 15 pages back One line forward One line back Beginning of file End of File Searches forward expression was Searches backward expression was Help Quit

/was

for for

Hard & Soft links When a file has more than one names, we say that the file has more than one links. Both files will have the same inode number. Hard links can be created by ln command which takes two filenames as arguments. ln file1 dbms Both files will have same content. Symbolic/Soft links links unlike hard links dont have the files contents, but simply provides the pathname of the file that actually has the contents. Shortcut files found in windows can be compared to these soft links. ln s /home/rohit/books/chap1 /home/rohit/Desktop {target file} {symbolic link}

1. To know type of the file file chap1 2. Compressing & decompressing the file

gzip chap1 Will compress file chap1 remove it and saves compressed file with gz extension. gzip d chap1.gz gunzip chap1.gz either command can be used to decompress the file. 3. Change file owner chown akshay chap1 will make akshay the owner of file chap1 Note: this command can only be used by super user. 4. Change file group chgrp old chap1 will assign group old to the file chap1 5. Counting lines, word, characters wc chap1 will give output like 5 80 200 chap1 i.e. file has 5lines, 80 words, 200 characters To get only lines, words or characters we use options l, -w, -c respectively. e.g. wc l chap1 wc chap1 chap2 will show count for both files in first two lines and in third line will show the total count of lines, words and characters. 6. Common data in two files comm chap1 chap2 will give output in 3 columns, the first column will contain lines unique to first file, the second column will contain lines unique to second file, and third column will contain lines common to both files. 7. Comparing two files cmp chap1 chap2 will return the position of first character mismatch between the files, if there is no mismatch then it will return the prompt without displaying anything. 8. Display differences between files diff chap1 chap2 will show a detailed output of differences between the two files

PATHNAMES
1. Absolute: complete path of a file is provided beginning with root(/) cd /home/rohit/books

2. Relative: only relative path to the current directory is provided cd .. i.e. one folder up cd ../.. i.e. two folder up cd ~ i.e. home directory cd/ i.e. root directory

MOUNTING & UNMOUNTING FILE SYSTEMS


If we want to access files present on CD or DVD or flash drive or external hard-disk on linux, then we have to first mount the file system of these devices. $ mount /dev/hda2 data the last column is the directory name into which the file system is to be mounted. The second last column contains the device name of the file system to be mounted. To unmount a file system $ umount /dev/hda2 Note: to mount windows file $ mount t vfat /dev/sda1 /mnt where we are mounting FAT file system of partition sda1 in mnt directory .

DIFFERENT FILE TYPES IN LINUX


1. Directories 2. Ordinary files 3. Device files - contains ordinary files - text files, images, etc. - linux treats even devices as files(hard-disks are named as sda

1. and partitions as hda) 4. Symbolic links - these are like shortcut files of windows. Note: linux kernel is represented by the file /boot/vmlinuz. Hidden files To make a file hidden, prefix it with a .(dot)

FILE PERMISSIONS
Each file/directory has 3 types of permissions: read(r), write(w),execute(x). These permissions are assigned in sets to 3 types of users: Owner/User(u), Group members(g), Others(o). File permission is represented in 10 columns. The first column of file permission shows the type i.e. d for directory, - for ordinary file. The rest nine columns show rwx permissions for the three categories of users. e.g. -rwxr-xr-- means owner can read, write and execute the file(the first set belongs to owner) ; the second set belongs to group members, so they can read and execute the file but cant write it; the third set belongs to others, so they can only read the file The permissions can also be given in numeric form where r=4, w=2, x=1. So, rwxr-xr- will be represented by 754(as rwx=4+2+1=7, r-x=4+1=5 and r-- =4). Default file permissions Files : 644(rw-r--r--) Directories : 755(rwxr-xr-x) Change file permissions chmod go+w chap1 will give write(w) permission to group members(g) and others(o) for file chap1 chmod a-x chap1 OR chmod x chap1 will revoke execute(x) permission from all(a) users. chmod 644 chap2 will assign rw(4+2=6) to u, r(4) to g, r(4) to o

DISK USAGE LIMITS / Quota management


Quotas are an optional feature of the operating system that allow you to limit the amount of disk space and/or the number of files a user or members of a group may allocate on a perfile system basis. This is used most often on timesharing systems where it is desirable to

limit the amount of resources any one user or group of users may allocate. This will prevent one user or group of users from consuming all of the available disk space quota -v edquota u rohit will display quotas assigned to each user. will open editor where we can modify quota of user rohit

quotacheck command examines each file system, builds a table of current disk usage, and compares this table against that recorded in the disk quota file for the file system. If any inconsistencies are found, both the quota file and the current system copy of the incorrect quotas are updated.

Soft Limit and Hard Limits:

A hard limit may not be exceeded. Once a user reaches his hard limit he may not make any further allocations on the file system in question. For example, if the user has a hard limit of 500 kbytes on a file system and is currently using 490 kbytes, the user can only allocate an additional 10 kbytes. Attempting to allocate an additional 11 kbytes will fail. Soft limits, on the other hand, can be exceeded for a limited amount of time. This period of time is known as the grace period, which is one week by default. If a user stays over his or her soft limit longer than the grace period, the soft limit will turn into a hard limit and no further allocations will be allowed. When the user drops back below the soft limit, the grace period will be reset. Grace Period is configured with the command edquota edquota u rohit t 5 will assign grace period of 5 days

LINUX DIRECTORY STRUCTURE


root(/)

home

usr

bin

sbin

dev

lib

tmp

etc

var

root directory is the parent directory of all the directories of linux. /home: The home directory will contain as many sub-directories as the number of users created on the system with the same name as the login name. When we log on to the system, we are placed automatically in the corresponding sub-directory e.g. if we login with student account we will automatically go to /home/student directory. /bin: Each command of linux has a file associated with it, and this file is located either in /bin directory or /usr/bin directory in binary form. /sbin: contains command which only a system administrator can execute. /dev: Linux treats devices as files, as some of the commands used to access an ordinary file also work with device files. The device files can be found in /dev directory. /etc: contains the configuration files e.g. inittab file /tmp: contains temporary files. /var: variable part of file system, contains all print jobs and mails. /lib: In Linux, some functions are built on top of system calls, in C language e.g. fread, fgetc, fgets. These function calls are stored as files in /lib directory.

External commands : commands having independent existence in /bin directory. Internal commands : built-in commands e.g. cd, pwd, echo, printf

Ext2 and Ext3 file system


Just like we have FAT32 and NTFS file systems in windows, there are ext2 and ext3 are standard file systems in linux. Every file system has 4 components

Boot block: the first block of a file system is known as boot block containing a program often referred to as Master Boot Record(MBR) which is loaded into RAM when the system is booted. MBR eventually loads the kernel into memory. Super block: the boot block is followed by superblock which contain information about the file system like the size of the file system, block size used by the file system, number of free data blocks available, etc. Inode blocks: Inode is a data structure used by linux to store information about each file. The inode block contains all the inodes. Data blocks: all data & programs created by users reside in this area. Inode(Index-NODE): Each file in linux is assigned an inode having a unique inode number. This inode contains all the information about the file. Mode Owner info Size Timestamps Data block pointers Indirect pointer Doubly indirect pointer Triply indirect pointer Mode indicates the type of the file i.e. whether directory or a file, it also contains the permissions on files/directory. Owner info indicates which user created and owns the file/directory. Size field contains size of the file. Time stamps indicates when the file was created and last updated. Data block pointers : there are 12 pointers in this block which can directly point to 12 data blocks Indirect pointer: this pointer points to an external data block pointer which can point to 12 data blocks. Doubly indirect pointer: this pointer points to an external indirect pointer which can point to 12 data block pointers, each data block pointer can point to 12 data blocks. Triply indirect pointer: this pointer points to an external doubly indirect pointer which can pointing to 12 doubly indirect pointers, each doubly indirect pointer can point to 12 indirect pointers, each indirect pointer can point to 12 data block pointers, each data block pointer can point to 12 data blocks. ext(extended) file system ext(extended) file system was a replacement for MINIX file system, which was Linuxs first default operating system. Minix only allowed 14 character file names and had a maximum of 64 megabytes for file size. The first ext file system overcame these limitations (255 character file names and 2 GB file size).

ext2 After its 1993 release, the second extended file system(ext2fs) quickly became the most widely used Linux file system. The maximal file name size is 255 characters. This limit could be extended to 1012 if needed. While the original kernel code restricted the max file system size to 2 GB, recent work in the VFS layer have raised this limit to 4 TB. ext3 It is a Journaling file system. Journaling file system contains a separate part known as journal or log, which records a change before writing it to a file system. Journaling file systems maximize file system consistency because log records are written before file system changes are made, and because the file system saves these records until they have been safely and completely applied to the file system. The ext3 file system supports three different journaling modes, which you can activate in the /etc/fstab entry for an ext3 file system. These journaling modes are the following: journal - the slowest of the three ext3 journaling modes, this journaling mode minimizes the chance of losing the changes you have made to any file in an ext3 file system. ordered - this is the default ext3 journaling mode. writeback - this is the fastest ext3 journaling mode. Specifying the journaling mode used by an ext3 file system is done in the /etc/fstab entry for that file system. For example, an /etc/fstab entry that specifies the "writeback" journaling mode would look like the following: /dev/hda5 /opt ext3 data=writeback 10 Note: There is one disadvantage with the ext3 file system as compared with ext2. With ext3, deleted files are, for all intents and purposes, unrecoverable.

CHECK & REPAIR FILESYSTEMS


fsck is a Unix utility for checking and repairing file system inconsistencies . fsck operates in two modes interactive and non interactive :

Interactive : fsck examines the file system and stops at each error it finds in the file system and gives the problem description and ask for user response whether to correct the problem or continue without making any change to the file system. Non interactive : fsck tries to repair all the problems it finds in a file system without stopping for user response useful in case of a large number of inconsistencies in a file system but has the disadvantage of removing some useful files which are detected to be corrupt .

fsck fsck y

will run in interactive mode by default will run the command in non interactive mode

1. To know current partitions #df h gives device name of partition, size, used space, free space, percentage of free space and directory in which it is mounted.

#fdisk l gives device name of partitions, which one is the boot sector, starting location, end point, number of blocks, ID, type of file system.

2. Creating partitions #fdisk -h /dev/sda2 Non-destructive partitioning: partition cannot be resized.e.g. swap, root partition Destructive partitioning: partition can be resized.

3. Free space #df #df h Will report free space in larger units like KB, MB, GB

4. Disk Usage #du #du s will give summary

5. Disk formatting mke2fs mke2fs j will format with ext2 file system will format with ext3 file system

Pipes: Inter-process communication


Pipes enable one process to send its output to anothers input. who command lists all the users who are currently logged on to the system. wc l command counts the number of lines in the given file.

We want to give the command to know how many users are logged onto the system. who | wc -l

Redirection(>): sending output to a file


We want to save the output into a file. ls l > abc.txt ls l | tee abc.txt will save output in a file abc.txt will display output as well as save in file abc.txt

Vous aimerez peut-être aussi