Upload
niyaman
View
217
Download
0
Embed Size (px)
Citation preview
8/14/2019 LAMP Chapter2 New
1/79
FOSS / LAMPFOSS / LAMP
Revathi J
Bharathidasan University Technology Park (BUTP)Bharathidasan University
8/14/2019 LAMP Chapter2 New
2/79
Standard Files Linux provides three I/O channels to processes.
Standard input, Standard output & Standard error.
Keyboard
Program
Standard Output
Standard Error
Standard Input
8/14/2019 LAMP Chapter2 New
3/79
Standard I/O and Pipes Linux supports shell redirection operators allow
standard I/O channels to be redirected to or from
a file.
The following common redirection operators;,
2>,|.
8/14/2019 LAMP Chapter2 New
4/79
Redirection Input : '
8/14/2019 LAMP Chapter2 New
5/79
Redirecting output Command
The "greater than" symbol '>' is used for save theoutput.
Example :$ ls -l > outputfile
If you want to add or append the date to thisoutput file, use the command:
$ date >> outputfile
with two "greater than" symbols (>>) we will addor append to the file without erasing its contents.
8/14/2019 LAMP Chapter2 New
6/79
The command 2> symbol is used for creating afile for an error message that you may get.
You can redirect the standard error into some file.
Example$ find / -name resolv.conf 2> errorfile
To display the contents of the errorfile,
$ cat errorfile
Redirecting Standard Error
8/14/2019 LAMP Chapter2 New
7/79
Pipe | command
This symbol is like two vertical slashes, one on topof the other.
That means that the pipe will separate two
commands so that they will be done one after theother.
Example$ ls -l /proc$ ls -l /proc | more Scroll down with the ENTER key$ ls -l /proc | less Scroll with arrows/pgup/pgdown
8/14/2019 LAMP Chapter2 New
8/79
Tee command
Lets you tee a pipe: redirect output to a file whenstill piping it to another program.
Example
$ set | tee set.out | less
The output from set is written to file set.out whilealso piped to less
Tee is useful to save the output at various stagesfrom a long sequence of pipes.
Example
$ | tee file1.out | sort | tee file2.out
8/14/2019 LAMP Chapter2 New
9/79
Cat Command Contents of the files are displayed sequentiallywith no break.
Files display concatenated.
Syntax$ cat [options] [file...]
Example$ cat /etc/profile$ cat /etc/profile | /etc/passwd >> output.txt
$ cat /etc/profile | tee out.txt
8/14/2019 LAMP Chapter2 New
10/79
The Linux Security Model
Users and groups are used to controlaccess to file and resources.
Users log in to the system by supplyingtheir user name and password.
Every file on a Linux system is owned by auser and associated with a group.
Every process has an owner and groupaffiliation, and can only access theresources its owner or group can access
8/14/2019 LAMP Chapter2 New
11/79
Users
Every person t at ogs nto t e computer sconsidered to be a user.
Every user has a unique User ID number (the uid)
User names and uids are stored in /etc/passwd file.
User have full access to their home directories.
Users can not change or even read eachother's files without being given permission.
8/14/2019 LAMP Chapter2 New
12/79
Groups Users are ass gne to groups w t un que group ID
numbers (the gid). All users in a group can share filesthat belongs to the group.
A users primary group is defined in the/etc/passwd file and secondary groups (gids) aredefined in the /etc/group file.
Primary group can be changed using the newgrpcommand: # newgroup othergroup
where othergroup is one of the userssecondary groups.
8/14/2019 LAMP Chapter2 New
13/79
Root User
The root user has special administrative
account sometimes called the superuser
root has complete control over the system
To protect the system from accidentaldamage, it is important for systemadministrators to use a normal user accountfor as much work as possible.
8/14/2019 LAMP Chapter2 New
14/79
Linux File SecurityEvery file and directory has permissions set thatdetermine who can access it.
Permissions are set for :Owner of the fileGroup membersAll others
Permissions that are set are calledRead,write andexecute permissions.
8/14/2019 LAMP Chapter2 New
15/79
Permission TypesFour symbols are used when displayingpermissions:
r permission to be read a file orlist a directorys contents.
w permission to be write a file orcreate & remove files from a directory.
x permission to be execute a program orchange into a directory and do a longlisting of the directory.
- no permission
8/14/2019 LAMP Chapter2 New
16/79
Examining Permissions File permissions may be viewed using
$ ls -l /bin/login
-rwxr-x-r-x 1 root root 19080 Apr 1 18:30 /bin/login
Directories permissions may be viewed using
$ ls -l /bin
drwxr-x-r-x 2 root root 4096 Apr 15 18:30 /bin
File type and file access permissions aresymbolized by a 10 character string
8/14/2019 LAMP Chapter2 New
17/79
Interpreting Permissionsdrwxr-x-r-x 2 root root 4096 Apr 15 18:30 /bin
The 1 Character is used to distinguishesdirectories (d) from regular fiels (-).
- regular file, d directory, l symbolic link, b block special file, c character special file,p named pipe, s - socket
Characters 2,3 and 4 identify permissions for the
owner(u).
Characters 5,6 and 7 identify permissions for thegroup(g).
Characters 8,9 and 10 identify permissions for allothers(o).
8/14/2019 LAMP Chapter2 New
18/79
Linux Process SecurityW en a process accesses a i e, t e user an group
of the process are compared with the user andgroup of the file.
If the user matches the user permission apply.
If the group matches, but the user doesn't, the
group permissions apply. If neither match, the other permissions apply.
8/14/2019 LAMP Chapter2 New
19/79
Changing Permissions The permission instruction can be issued either
symbolically or numerically.
The chmod command changes permission of filesand directories.
Who may be chosen from Operator may be Permission may bechosen from chosen from
u owner of the file + Set permission r readg group - Unset permission w writeo others = Assign a permission x executea all three categories s set user ID bit or
group ID bitt sticky bit
(for directory)
8/14/2019 LAMP Chapter2 New
20/79
Symbolic Method Examples
# chmod u+w,go-w txtfile// grants write access to owner but denies to groupand others.
# chmod u=rw txtfile
// Sets user permissions to read and write, with executeturned off, regardless of the current permission.
# chmod +r txtfile// Makes the file world-readable.
8/14/2019 LAMP Chapter2 New
21/79
Numeric Method To change the permission on a file the numeric
method is easier than the symbolic method.
Uses a three digit mode number first digit specifies owner permission second digit specifies group permission third digit specifies others permission
Permissions are calculated by adding: 4 read 2 write 1 execute 0 no permission
8/14/2019 LAMP Chapter2 New
22/79
Numeric Method - Examples # chmod 664 file
// Read and write set for owner and group, read-only for others. # chmod 660 file
// Read and write set for owner and group, no permission forothers.
# chmod 444 file// Read-only permission set for all.
With directory permissions: # chmod 755 dir
// Full permission set for owner, read and execute setfor group and others.
# chmod 770 dir// Full permission set for owner and group, nopermission others.
# chmod 555 dir
// Read and execute permission set for all.
8/14/2019 LAMP Chapter2 New
23/79
Advanced Topics in Users, Groups & Permissions
All authentication is stored in plain text files:
/etc/passwd/etc/shadow/etc/group
The /etc/passwd file contains seven fields. Username, password, uid, gid, GECOS field, homedirectory, and shell to be run when a user logs in.
root:x:0:0:root:/root:/bin/bash
8/14/2019 LAMP Chapter2 New
24/79
The /etc/shadow file is referenced when someonelogs in: the file contains a mapping of a user nameto a password.
The /etc/shadow file contains user name,password encryption, days from linux started,minimum days, maximum days, warning days.
root:!!:13326:0:99999:7:::!! - refers password lock.
8/14/2019 LAMP Chapter2 New
25/79
8/14/2019 LAMP Chapter2 New
26/79
Changing your identityThe su command is used to change identities.
To start a new shell as a different user:# su # su -
Lets suppose jack has bob permission to read hisemail. He can temporarily assume jack user id:
$ su bobpassword:
$ whoami bob
8/14/2019 LAMP Chapter2 New
27/79
User Information Commands The information commands provide the
information about the users.
Example, Find out who you are
whoami Find out what groups you belong to
groups, id Find out who is logged in
users, who, w Find login/reboot history
last
8/14/2019 LAMP Chapter2 New
28/79
Default permissions and UmaskDe au t permission or i es is 666Default permission for directories is 777
umask is subtractor from default to determine newfile/directory permissions
non-privileged users umask is 002
files will have permissions of 664 directories will have permissions of 775
roots umask is 022
8/14/2019 LAMP Chapter2 New
29/79
Special permissions A fourth permission set (in addition to
user/group/other) Applicable in four cases:
Set user ID or suid for an executable Set group ID or sgid for an executable Set group ID or sgid for a directory Sticky bit for a directory
Use the chmod command, preceding the usualthree digits with a digit representing thespecial permission : 4 for suid, 2 for sgid, 1 for the sticky bit.
Example:# chmod 3775 groupdir
8/14/2019 LAMP Chapter2 New
30/79
Special Permissions for Executables
e et erm ss on
Suid : Command run with permissions ofthe owner of the command, not exexcutorof the command
In long listing, the suid permission isdisplayed as a lower case s where the x
would otherwise be located for the userpermission.
$ chmod 4551 passwd$ ls -l passwd
-r-s- -x- -x 1 root root 15367 May 20 2007 passwd
8/14/2019 LAMP Chapter2 New
31/79
Set gid command sgid : command runs with group affiliation of the
group of the command:
$ chmod 2551 passwd $ ls -l passwd
-r-xr-s- -x 1 root john 30899 Aug 18 passwd
8/14/2019 LAMP Chapter2 New
32/79
Special Permissions for Directories
Sticky Bit
Files and directories with the sticky bit set can onlybe removed by the owner and root, regardless of thewrite permissions of the directory
$ chmod 1777 /tmp
$ ls ld /tmpdrwxrwxrwt 30 root root 7089 Mar 9 10:28 /tmp
8/14/2019 LAMP Chapter2 New
33/79
sgid : files created in directories with the sgid bitset have group affiliations of the group of thedirectory.
This is commonly used on group directories:
$ chmod 2770$ ls l groupdir
drwxrws- - - 2 john eric 4089 Mar 9 10:20 groupdir
Often both the sticky bit and the sgid permissionwill be set on a group directory
8/14/2019 LAMP Chapter2 New
34/79
Vi Editor
vi : the 'visual editor', standard Linux and Unix editor.
Let's make a text file. Type: vi textfile
Three modes in vi editor, Command mode
Cursor movementChange, delete, yank, put and search
Insert mode
Type in new textReturn to command mode with Ex mode
Configuring, exiting, savingSearch and Replace
To enter ex mode, type : while in command mode
8/14/2019 LAMP Chapter2 New
35/79
Common vi editor command listFor this Purpose Use this vi Command SyntaxTo insert new text ESC +i
To save file ESC +: +w
To quit without saving ESC +: +q!
To save and quit vi editor
ESC +/word
n
To save file with file nam(save as)
ESC +: +w "filename"
ESC +: +wq
To search for specified word inforward direction
To continue with search
8/14/2019 LAMP Chapter2 New
36/79
Command list continued....For this Purpose Use this vi Command Syntax
ESC +?word
ESC +p
To delete word from cursor position
To undo ESC +u
To redo
To search for specified word inbackward direction
To copy the line where cursor islocated
ESC +yyESC +4yy
To paste the text just deleted orcopied at the cursor
To delete entire line where cursor islocated
ESC +ddESC +4ddESC +dw
8/14/2019 LAMP Chapter2 New
37/79
Partitions and Filesystems n nux, ar s r ves are v e nto
partitions.
These partitions are formatted withfilesystems, and these filesystems are allowuser to store data and access files.
There are different Filesystem types areavailable, the default Filesystem is ext3.Other common filesystems are ext2,msdosand iso9660.
8/14/2019 LAMP Chapter2 New
38/79
Symbolic (or Soft) links
A symbolic link points to another file
ls l displays the link name and thereferenced file
To create a symbolic link$ ln -s /etc/passwd password
$ ls - li password /etc/passwd
30338 -rw-r--r-- 1 root root 1729 Aug 24 11:43 /etc/passwd30376 lrwxrwxrwx 1 ram ram 11 Aug 24 10:43 password /etc/passwd
8/14/2019 LAMP Chapter2 New
39/79
Hard links One physical file on the filesystem
File is present in the filesystem as long as at least
one link remains.
To create an hard link to an existing file, use theln command:
$ ln txtfile new $ ls -li txtfile new 24567 -rw-rw-rw- 1 ram ram 29 Aug 24 11:43 txtfile 24567 -rw-rw-rw- 1 ram ram 29 Aug 24 11:43 new
8/14/2019 LAMP Chapter2 New
40/79
Checking free space df reports disk space usage
Reports total kilobytes, kilobytes used, kilobytesfree per file system
- h displays sizes in easier to read units
$ df /optFilesytem 1K-blocks Used Available Use% Mounted on/dev/hda7 15685 51234 10244 20% /opt
$ df -h /opt
Filesytem Size Used Available Use% Mounted on/dev/hda7 16G 4.9G 11G 20% /opt
8/14/2019 LAMP Chapter2 New
41/79
du command du reports disk space usage
Reports kilobytes used per directory Includes subtotals for each subdirectory -s option only reports single directory summay Also takes -h option
Example :$ du -s /dev 11630 /dev
8/14/2019 LAMP Chapter2 New
42/79
Removable media Mounting means making a foreign filesystem
look like part of the main tree
Before accessing, media must be mounted.
Before removing, media must be unmounted.
Mountpoints are usually under /media.
8/14/2019 LAMP Chapter2 New
43/79
Mounting CDs and DVDs When using X Window System, inserting a CD
into the drive automatically mounts the CDand adds an icon to the desktop.
Otherwise, must be manually mounted, CD/DVD Reader
$ mount /media/cdrom$ umount /media/cdrom
CD/DVD Writer$ mount /media/cdrecorder$ umount /media/cdrecorder
If you want to eject the CD out, you have to
execute the eject command.
8/14/2019 LAMP Chapter2 New
44/79
Mounting USB Media When using X Window System, automatically
mounted in Gnome/KDE and icon created incomputer window.
USB disks are treated as SCSI devices, thus theyare referenced as /dev/sda, /dev/sdb, etc.
If you want to mount manually,$ mount /media/$ umount /media/
8/14/2019 LAMP Chapter2 New
45/79
8/14/2019 LAMP Chapter2 New
46/79
Compression Utilities gzip, gunzip
Standard linux compression utility Up to 75% compression for text files
# gzip v file# gunzip file.gz
bzip2, bunzip2 newer Linux compression utility Generally archieves better compression than gzip.
# bzip2 -v file# bunzip2 file.bz2
8/14/2019 LAMP Chapter2 New
47/79
String Processing - head The head command displays first few lines of the
text in a file. (i.e) it display from top of the file.
Example: # head n 3 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:
daemon:x:2:2:daemon:/sbin:
-n or --lines specifies the number of lines todisplay.
8/14/2019 LAMP Chapter2 New
48/79
tail The tail command displays last few lines of the
text in a file. (i.e) it display from bottom of thefile.
Example: # tail -n 2 /etc/passwd ntp:x:38:38: :/etc/ntp:/sbin/nologin
gdm:x:42:42: :/var/gdm:/sbin/nologin
-n or --lines specifies the number of lines todisplay.
8/14/2019 LAMP Chapter2 New
49/79
wcwc - word count is used to count lines and
characters.
Example: # wc text.txt 20 200 1500 text.txtl - for line count
w - for word countc - for character count
8/14/2019 LAMP Chapter2 New
50/79
grep grep general regular expression processor,
displays the lines in a file that match a pattern. Itcan also process standard input.
# grep eric /etc/passwd eric:x:501:501:eric:/home/eric:/bin/bash
# ls | grep d*
# grep [Dd]og testfile -it displays either the string Dog or
the string dog from the file testfile.
8/14/2019 LAMP Chapter2 New
51/79
find Finds one or more files assuming that you know
their approximate filenames. Syntax: $ find -name
Examples: $ find / -name 'myhome.html'
In the above command the system would search forany file named myhome.html in the currentdirectory and any subdirectory
8/14/2019 LAMP Chapter2 New
52/79
Investigating and Managing Processes Process :
It is an executing program with several components and
properties.
pstree shows the process ancestry for all processrunning on the system.
# pstree
ps displays processes information
8/14/2019 LAMP Chapter2 New
53/79
Viewing Processes ps displays processes information # ps -[option]
ps -a processes by all users ps -x processes from all terminals ps -u show process owners
ps -w
include command arguments ps -f show process ancestry ps -l long listing ps -A to see all the process
8/14/2019 LAMP Chapter2 New
54/79
Scheduling the process Comman s wi e execute at t e time in icate .
The output is mailed to the user.
Syntax:
# at Example
# at 8:30pm July 10> ls l
8/14/2019 LAMP Chapter2 New
55/79
# atq // list the current at jobs
pending.# atrm [jobID] // used to remove pending at jobsTo see the job result, # mail // used to view the executed
jobs
8/14/2019 LAMP Chapter2 New
56/79
Scheduling Periodic Processes cron The cron mechanism allow processes to be invokedperiodically.
Syntax:
# crontab [-u user] file# crontab [-l | -r | -e] [username]l - used to display the crontab filer - used to remove the crontab filee - used to edit the crontab file
8/14/2019 LAMP Chapter2 New
57/79
A users crontab may look like the following:
# crontab -eu root
#Min Hour DoM Month DoW Command0 4 * * * find / -name passwd0 0 31 10 * echo Welcome to BUTP*/1 11 * * * ls
: wq
For activate the crontab,# service crond restart# chkconfig crond on
8/14/2019 LAMP Chapter2 New
58/79
Installing Application Most of the linux application that you want to install
are available in RPM format.
To install an RPM archive file with the rpm command,use the following syntax;
# rpm i [option] package
The options:--v - to get more verbose feedback, along with
some of the output.-h - Prints 50 hash marks(#) as the package is
unpacked.
8/14/2019 LAMP Chapter2 New
59/79
Querying packages
Querying Packages
Using the query options (-q) of rpm, you can getinformation about RMP package.
# rpm -qalist all the installed packages
8/14/2019 LAMP Chapter2 New
60/79
Thank You
8/14/2019 LAMP Chapter2 New
61/79
8/14/2019 LAMP Chapter2 New
62/79
8/14/2019 LAMP Chapter2 New
63/79
Command list continued ....For this Purpose Use this vi command Syntax
Go to the first line in the file ESC +g
Go to the last line in the file ESC +G
ESC +1G or H
Go to middle line on screen ESC +M
Go to last line on screen ESC +L
ESC +:$s/word-to-find/word-to-replace/cg
ESC +:!shell-command For e.g. :!pwd
Go to first line in file(any number can be givenand cursor will jump to that line)
Go forward one full screenGo back one full screenGo down half a screen
Go up half a screen
To Find all occurrence of given word andReplace then globally without confirmation
ESC +:$s/word-to-find/word-to-replace/g
To Find all occurrence of given word andReplace then globally with confirmation
To run shell command like ls, cp or date etc
within vi
8/14/2019 LAMP Chapter2 New
64/79
8/14/2019 LAMP Chapter2 New
65/79
InodesAn inode table contains a list of all files in an ext2
or ext3 filesystem.
An inode (index node) is an entry in thetable, containing information about a file(the meta data), including : File type, permissions, link count, UID, GID The file's size and various time stamps Pointers to the file's data blocks on disk Other data about the file
8/14/2019 LAMP Chapter2 New
66/79
Inode - example
$ ls -il
80777 -rw-r----- 1 ram ram 5120 Sep 11 12:50 txtfile59907 drwxrw---- 2 ram ram 4098 Sep 11 12:50 data80787 -rw-r----- 1 ram ram 1500 Sep 11 12:52 file
The inode value is showed in the first column
8/14/2019 LAMP Chapter2 New
67/79
cp and inodeWhen a file is copied to a new name in the same
directory, the directory and the inode table get anew entry.
Example: $ ls - li txtfile 24567 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 txtfile
$ cp txtfile new 24567 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 txtfile 24568 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 new
8/14/2019 LAMP Chapter2 New
68/79
mv and inodeWhen a file is moved, the inode number remains
the same.
The data on the file system is not moved.Example: $ ls - li txtfile
24568 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 txtfile $ mv txtfile new
$ ls - li txtfile new 24568 -rw-rw-r-- 1 ram ram 26 Sep 25 20:16 new
8/14/2019 LAMP Chapter2 New
69/79
8/14/2019 LAMP Chapter2 New
70/79
8/14/2019 LAMP Chapter2 New
71/79
cut cut is used to cut fields or columns of text from
a file and display it to standard output.
Example: # cut -f3 -d: /etc/passwdWill display a list of uids from /etc/passwd,because uids are stored in field three and : is
used as a delimiter. # cut -c2 -5 /etc/passwd
It will display character starting from 2nd andending with 5th character.
8/14/2019 LAMP Chapter2 New
72/79
paste paste combines files horizontally. It takes a line
from each file and paste them together tostandard output, separated by a tab.
Use the d option to change the output delimiter. # paste -d: test1 test2 > mergefile
It takes the line from each file (test1 and test2)and put it into the output file.
8/14/2019 LAMP Chapter2 New
73/79
uniq Theuniq removes duplicate lines from a file.
Example# vi uniqfile
Sunday Monday
Tuesday Sunday Friday Monday
# uniq uniqfile
Sunday Monday Tuesday Friday
8/14/2019 LAMP Chapter2 New
74/79
Sort Sorts text to stdout original file unchanged
By default sort sorts the file in ascending order using
the entire line as a sorting key.
Syntax:# sort [option] filename
-r reverses sort-n numeric sort-u unique (remove duplicate lines in output)
8/14/2019 LAMP Chapter2 New
75/79
tr tr is used to translate characters; that is given two
ranges of characters, any time a character inrange 1 is found, it is translated into the
equivalent character in range 2.
# tr a-z A-Z < testfile
The translated output is stored into testfile.
8/14/2019 LAMP Chapter2 New
76/79
diff Diff is used to compare the contents of two files
for differences.
# diff test1 test2
< This is test file // first file test1 -------
> Welcome // second file test2
8/14/2019 LAMP Chapter2 New
77/79
Specll check aspell is an interactive spell checker. It offers
suggestions for corrections via a simple menu-driven interface.
# aspell check testfile aspell -l will non-interactively list the misspelledwords in a file read from standard input.
# aspell -l < stdfile
8/14/2019 LAMP Chapter2 New
78/79
look A quic spe ing ictionary oo up can e
performed with look. It comes in handy when you need the spelling of a
word of which you know the first few letters.
# look le Leave
Letter Lead Least
.
8/14/2019 LAMP Chapter2 New
79/79
Regular Expressions Regular expressions are pattern matching engine sed - stream editor. It will perform a search and
replace on all lines in the file, sending the modifieddata to standard output; that is, it does not actually
modity the existing file.
# sed [sting] Example:
# sed s/cat/dog/ pets// cat being replaced by dog string. # sed s/cat/dog/g pets
// entire cat string being replaced by dog string.