UNIX/LINUX Fundamentals

lecture 1:

  1. O/S objective:

1- HW resourses Managment (CPU, memory, I/O devices)

2- Basis for applications ( applications need OS to run)

3- user interface ( to deal with OS)

and there is 2 types of user interface -GUI -CLI

  1. UNIX History:

1965Bell labs ( which after that called AT&T and now called lousent technology) ,GE (general electric) and MIT make project called MULTICS (multiplexed Information and computer services) and from its objectives:

1- user friendly

2-Multiuser ( to share resources to save costs)

3- Data Sharing

and the worked till 1969

1969AT&T withdrew from the project and the project fail. The AT&T people (ken Thompson and Dennis Ritchie) who were involved the made there operating system and called it UNICS ( UNIplexed Information and computing services) – an 'emasculated MULTICS'

and they complete the project and UNICS become UNIX in 1970

1970First edition of UNIX released and it is included over 60 commands like :b (compile b program), cat ,chdir,chmod,chown,cp,......

1972Ritchie rewrote B and called the new language C and write UNIX OS with C language

1973UNIX is re-written mostly with C

1975 bourne shell is introduced begins being added onto

1991 linux was introduced by Linus Thorvald : to make unix on pc

linux is a unix like OS which has very powerful GUI like windows and CLI like UNIX

1994 Red Hat linux is introduced

  1. UNIX advantage:

1- reliability2-performance3- security4- virus Invulnerability

  1. UNIX and Linux Operating Systems:

UNIX:

SUN----->Solaris

HP----->HPUX

IBM----->AIX

LINUX:

Fedora------> free (buggy version)

Redhat-----> with money and they have deal in egypt called linux+ (free bug)

SUSE

  1. UNIX Structure:

1- Hardware2- Kernel3- Application

  1. LINUX Tree:

/Root

/rootThe root user home directory

/homeUser's home directory

/binCommon programs needed during boot up and reparing that might be needed by normal user as shells (sh,bash ), file manipulating program (tar,echo,vi,grep) and process handling programs (kill,ps)

/bootKernel and other boot files

/devDevice files

character device as keyboard,mouse

block device as hard disk, cdrom

/etcConfiguration files as /etc/passwd , /etc/shadow, /etc/group

/sbinSystem administration programs needed to boot and maintain the system as: ifconfig (interface configuration,use to add or remove interface and get or give ip address),mount, check file system, mkfs (make file system),......

/tmpTemporary Files and always clean out at boot time or by regular job process

example : download package

/varThe directory Contain Files for variable file storage aslog files,spool files, and other dynamic files as /var/mail ---> users mail

/var/spool ---> files for the print queue

/var/log----> logging information

/usrused to store application

/usr/binexecutable programs that are not required for booting or reparing the system

/usr/sbinsystem administration commands

  1. Most Common mistake using CLI:

1- spelling2- Cases3- Spaces4- Synopsis (syntax)

  1. Absolute and Relative Path

Absolute Path: The path from /

Relative Path: The path from the place that I'm standing on it

.. parent directory. current directory

  1. Some useful commands:

1- cat to read file ( display all)

cat /etc/passwd

cat -n /etc/passwd precede each line with a line number

cat f1 f2 f3  list a series of files

2-more /etc/passwd  divide file into pages

/pattern search for the pattern

Space to view next screen

Enter to view next line

b  go back 1 screen

3- mkdir  to create directory

mkdir dir1

mkdir dir1/dir2

mkdir -p dir1/dir2

4- touch  to create empty file

touch f1

5-rm and rmdir

Rmdirremove empty directory

rmdir -p remove dir and if the parent empty so remove it too

rm  remove file

rm -r remove dir and it's content

6- cp to copy file

example: cp /etc/passwd /export/home/marian/passwd

cp -r copy directory

7- mv move files and rename files

example: mv ~/passwd dir1/oldpass

8- ls list the current directory

ls /usr/bin/*sh - list what is ended with sh

ls /usr/bin/w*list what is start with w

9- pwd print working directory

10- cdchange directory

11- PS1='$PWD' to make the current working directory is my prompt string

12- list 4 ways to go to home directory

cd /export/home/marian

cd

cd ../../export/hme/marian

cd ~

13- head /etc/passwdby default list first 10 lines from /etc/passwd file

to list first 4 lines from /etc/passwd

head -4 /etc/passwd

14- tail /etc/passwd list last 10 lines from /etc/passwd file

tail -7 /etc/passwd list last 7 lines

15- wc /etc/passwd word count

the output: lines word characters file name

-lto get no of lines only

-wto get no of words only

-c to get no of characters only

16- who reports who is logged in to the system

fingeralso show who is logged in to the system but with more information

17-man manual pages (UNIX help)

manual pages divided into sections

1user commands

8administration commands

2system calls

5 configuration files

3C lib

man passwd get manual pages of passwd command

man -a passwddisplay all passwd files in all sections and it search in sections in order

output : it display passwd command manual the passwd file manual

  • To list sections number that have file called passwd

man -f passwdin some linux

then : man -s 5 passwdto display passwd file

in Ubuntu we can also make man 5 passwd

  • To search with keyword

man -k passwd

lecture 2: File Security

  1. File permissions

readrto read file

writewto edit file

executexto execute file if it is a program

example: cp f1 f2

cp permission: execute to run it

f1 permission: read

  1. Directory permissions

first we need to know that every file has id ( to deal with it by the computer)

so directory is considered to be a file that has table to match name with id

So cat mycv = ls dir

So in general directory = file (name + id)/etc

Passwd / 3472
Hosts / 5381
Shadow / 7294
Group / 3946

readrto list directory ( to make ls)

writewto create, rename and delete from the directory

(mkdir,touch,rm,rmdir)

executexhave right to enter or no ( to make cd)

NOTE: to rename mycv ( directory containing this file must have write permission )

example: cat /etc/passwd

cat permission: rx

/etc permission: read so i can see what is in /etc

executeso i can enter /etc

NOTE: x without r enter blinding and can't see anything

  1. List permissions

cat /etc/passwd

/etc/passwd owner: root

group:sys

and I have user Ali in ssdp29 need to access this file

so permission on file needs to be

userurwx

groupgrwx(user's primary group)

otherorwx

so /etc/passwd permissionrw-r--r--

u g o

since Ali is not the owner and in different group so he has read access only

  • To list file permission

ls -l /etc/passwd

output: - rw- r-- --- 2 root root 512 nov 25 10:30 /etc/passwd

1st element file type

regular file (head, mycv,.....)

d Directory (etc)

s Soft link

b block device file

c character device file

2nd element is file permission

3rd element number of hard links

4th element file owner

5th element file group

6th element file size

7th element modification date and time

8th element file name

NOTE: If we create file with touch command with existing file name so system will update file

modification date and time

  • To know information about files on specific directory

ls -l /dir

NOTE: ls -l /dir needs execute permission on the directory because we need information about files in that directory

  • To list information about the directory itself

ls -ld /dir

  1. Changing permission

original permissionrwx rw- r--

desired permissionrw- rw- rw-

so we need to change file mode

chmod u-x mycv

chmod o+x mycv

we can do it in one step

chmod u-x,o+w mycv

  • To append permission

chmod u+rw,g+r f1

  • To override existing file permission

chmod u=rw,g=r,o=

o= if we want others to take no permission

chmod u=rw f1

  • To delete some existing permission

chmod u-w,g-r f1

  • To change some permission

chmod u-r,g-r+w f1

  • chmod go+rw f1append read and write perm for group and others
  • chmod a=rto make all have read perm only

a+rto append read permission for all

a-r to delete read permission from all

a= to make all with no permission

  • Other way to change permission

we wantrw- rw- rw-

binary110 110 110

octal 6 6 6

then chmod 666 mycv

  • chmod 6 mycv is considered to be chmod 006 mycv
  • chmod 23 f1 is considered to be chmod 023 f1

NOTE: chmod used by owner or administrator

NOTE: when we want to remove file write protected (not have write permission)

The system will ask me for deletion and the same for directories

NOTE: To remove write protected directory the directory must be empty

example: d1rwx

d1/d2 rx

d1/d2/f1

rm -r d1/d2 can't remove d2 because it contain f1

example: d1rwx

d2rx

rm -r d1/d2 d2 will be removed

  1. Change default permission

existing defaultrw- r-- r--

disiredrw- rw- ---

want to hide--x --x rwx

001 001 111

umask 117

NOTE: if u make umask 117 it will be unsaved if you closed the shell , when you open new shell it will be back to default system umask

NOTE: to know default system umask: umask

  • to change umask to be rwx r-x r--

umask 027

lecture 3: User Administration and File System

User Administration

  1. Introduction

The Solaris 10 OS stores user account and group entry information in the following system files:

  • /etc/passwd

-Authorized system users have login account entries in the /etc/passwd file.

- Each entry in the /etc/passwd file contains seven fields. A colon separates each field. The following is the format for an entry:

loginID:x:UID:GID:comment:home_directory:login_shell

  • /etc/shadow

- The /etc/shadow file is a separate file that contains the encrypted passwords. To further control user passwords, you can enforce password aging. This information is also maintained in the /etc/shadow file.

- Each entry in the/etc/shadowfile contains nine fields. A colon separates each field. The following is the format for an entry:

loginID:password:lastchg:min:max:warn:inactive:expire:Flag

  • /etc/group

- The /etc/group file defines the default system group entries. You use this file to create new group entries or modify existing group entries on the system.

- Each entry in the/etc/groupfile contains four fields. A colon separates each field. The following is the format for an entry:

groupname:group-password:GID:username-list

  1. Managing User account
  • Create User account

Use theuseradd command to add new user accounts to the local system. These commands

add an entry for a new user into the /etc/passwd and /etc/shadow files

useradd loginname add user to local system with defaults

Linux defaults: uid begin from 500home directoryhome/username

gidit create new group (it's name is user's name and it's no is uid)shellbash

useradd -g gid -G gid,gid,...-md homedir-s shell -c comment loginname

-g Defines the new user's primary group

-GDefines the new user's secondary group memberships

-mdDefines the full path name for the user's home directory and -m option to create the user's home directory if it does not already exist

-sDefines the full path name for the shell program of the user's login shell

-cSpecifies any comment, such as the user's full name and location

User accounts are locked by default when added with the useradd command,So you use the passwd command to create a password for the new account.

passwd username

  • Modifying User account

Use the usermod command to modify a user's login account on the system.

usermod -g gid -G gid,gid -md homedir -s shell -c comment -l newname loginname

In general, the options for the usermod command function the same as those for the useradd command.

-l newloginnameChanges a user's login name for the specified user account.

NOTE: usermod -G ayhaga marian

usermod -G ITI marian

so marian's secondry group will be ITI only so it will make override

  • Deleting User account

Use the userdel command delete command to delete a user's login account from the system.

userdel username

To remove user account with his home directory from the system at the same time:

userdel -r username

  1. Managing Groups
  • Creating Group Entry

you create new group entries on the local system by using the groupaddcommand. These commands add an entry for the new group into the /etc/groupfile.

groupadd groupnameadd group to a system

  • Deleting Group Entry

Use the groupdelcommand to delete group entry on the system.

groupdel groupname

File System

  1. UNIX/Linux File system

linux file system: ext3 ( 3rd extended file system), ext4

unix file system: ufs (unix file system)

  • each partition (file system) have inode table that have info about

each file in that partition

  • The information is put in inode table ( each file have 1 entry) and

the content saved in the partition in blocks

  • Disk label = master boot record (MBR)

Disk label have info about the partition ( which partition is an

active partition, the system wil be boot from which partition, also

it have partition table)

  • The info in inode table can be:

-inode number (file id)- file type

- permission- owner- group-no of hard links

- size- last modification time (mtime)-last access time (atime)

- inode info last change (ctime)-block number

  • In inode table we didn't put file name because:

1- if the file have more than 1 link

2-length of inode record

3-already found of inode of directory

NOTE: depthof directory in Unix/Linux= max. number of records in inode table

  • when we create new file we have 2 constraints: -not enough free blocks and there is no available inode record
  1. Directories
  • Directories store info that associated file name with inode number but inode table of a partition contains all data about each file in each partition
  • Directories contain entries for files of all types that are logically found within that directory

NOTE: x(execute) permision on a directory used to reach files in the directory and get info about them rw- so example: ls /dir2 ok

cd no

ls -l no

NOTE: The inode number of the root of each partition =2

so inode number of / =2 and if /home is a partition so /home inode number =2

NOTE: we can list and get inode number of files name in adirectory by read permission only

ls -i mydir

  • when copy f1 to dir1 it will create new file in dir1 with new inode number so it will make new hard link
  • whenmoving f3 to dir1 it will be moved with same hard link , so f3 entry in dir2 will be removed and new entry will be added in dir1 table
  • while when moving between different partition it will create new hard link

NOTE: when file created it has 1 hard link

while directory creation it will have 2 hard links by default (. ,.. )

rm dir1/f3remove entry from inode table and from directory table

  1. Hard Link
  • we can't make hard link between different partitions ( only within the same partition)
  • A hard link considered to be another name for the same file (alias for a file)
  • every file have at least 1 hard link ( at creation time)
  • every entry in a directory called hard link

NOTE: maximum number of hard link depends on a space of hard links field in inode table

  • when we make hard link for a file it increment the links count in inode table

ln dir1/f1 dir1/f2

So both files have the same inode number

ls -li list files with i node number

ls -lid to list directory inode number

  • when we delete f2, number of hard links in file record in inode table will be decreased by 1

so also i can still access the file till number of hard link be 0

  • hard link is faster than soft link
  • we can't make hard link for directories

what does it mean that the dir11 have 65 hard link?

It mean that it have 63 subdirectories points to it and (. , ..)

  1. Soft link ( as shortcut)
  • A symbolic link is a file that points to another file
  • symbolic link can be made between different partitions
  • symbolic link contains only one type of data (text data)
  • symbolic link contains the path of the file which it is pointing to
  • if we delete original file , there is no need for a symbolic link
  • symbolic link size always matches the number of character of the path name ( for the file that is pointing to it)
  • we can make symbolic link for directories
  • To create symbolic link

ln -s /etc/passwd ~/pass

cat ~/pass it will display /etc/passwd

  • we can make read and write operation to symbolic link so original file will be affected
  1. Some useful commands
  • df summarize disk format (deal with partitions)

df used to report the number of disk blocks, used blocks and free blocks for each file system

df -l local disk

df -h give partition size in human readable format ( as in Giga or Mega )

  • dureport disk space in use for files or directories you specify (deal with directories and files)

du -s display summary

du -h display size in human readable format

  • find look for specific file

find /path/dir -name passwd find file called passwd in dir directory

-type f,d,b,c,l find with file type

-perm 777 find file with permission 777

-user marian find files that owned by marian

-group iti find files that belong to iti group

-mtime +3 / 3 /-3 find file that their data was last modified

+3 more than 3 days3 that modified in 3 days

-3 less than 3 days

-atime / ctimethe same as mtime

-inum 3375secarch with file with inode number

-size 657 size in block

example: find .

List all files and directories in current directory

example : ls -li /etc/passwd

output: 1234 -rw-r--r-- 4 root root nov25 10:30 /etc/passwd

and we find that /etc/passwd have 4 hard links

To know names of these 4 hard links

find / -inum 1234 it will list the file names of 4 hard links

  • grepprint lines matching a pattern

grep marian /etc/passwd print lines match 'marian' pattern