Está en la página 1de 97

Ti liu hng dn

MrKahn-iData

MC TIU KHA HC
S dng my tnh Linux p ng cc yu cu chc nng nh my tnh Windows. Thay th m hnh mng cc server Windows bng m hnh mng cc server Linux. Cc server Linux p ng vai tr nh server Windows, c kh nng qun l tt, chu li tt.

MC TIU KHA HC

Bind/ LDAP

Sendmail/ Postfix

Squid server

Firewall/ IDS

Samba/ NFS

DHCP server

FTP/ SSH server

Apache server

MySQL server

MC TIU KHA HC
Installing Linux as a Server
Technical Summary of Linux Distributions Installing Linux in a Server Configuration Installing Software

MC TIU KHA HC
Single host Administration
Managing Users The Command Line Booting and shutting down File Systems Core System Services Compiling the Linux Kernel

MC TIU KHA HC
Intranet services
Networking Fundamentals DHCP server Samba/ NFS server NIS LDAP

MC TIU KHA HC
Internet services
FPT/ SSH server DNS server Web server/ Database server Proxy server Mail server Firewall server IDS

Technical summary of Linux Distributions

Ni dung
Phn mm m ngun m v GNU General

Public License. Lch s pht trin ca Linux c im ca h iu hnh Linux. Khc bit gia h iu hnh Linux v Windows. Li ch v hn ch ca h iu hnh Linux. Cc phin bn Linux chnh.

M ngun m v GPL
GNU GPL: GNU General Public License. Mi ngi u c th c source code ca m ngun m, chnh sa, bin dch theo ring. M ngun m chnh sa c th dng cho mc ch ring hoc cng khai. Nu cng khai phi cung cp y source code. Linux l h iu hnh m ngun m, c phn phi theo quy nh ca GNU GPL.

M ngun m v GPL
C th tnh ph khi phn phi mt sn phm c ngun gc l m ngun m. Tuy nhin, khi phn phi phi km theo source code. Khi ngi s dng c mt phn mm m ngun m, h c t do chnh sa, chia s, phn phi li

Lch s Linux
Linux c Linus Torvalds vit nm 1991. c cng ng Internet n nhn. Nhiu ngi tnh nguyn tham gia pht trin Linux. H iu hnh Linux gm:
Linux kernel. Nhng ng dng v tin ch GNU. Nhng ng dng khc.

Lch s Linux

c im ca Linux
Hardware:
Chy trn nhiu platform, Alpha, AMD, Intel, MIPS, PowerPC, Sparc http://hardware.redhat.com/hcl

Software:
http://www.freshmeat.net http://www.linuxberg.com http://distrowatch.com/ Document: http://www.tldp.org/

c im ca Linux
Giao din ha:
H tr GNOME, KDE, Linux khng yu cu giao din ha.

Ngn ng lp trnh: C, C++, FORTRAN, Java, Perl, Python, PHP D dng qun l t xa:
D dng remote t xa bng commandline hoc GUI.

Tnh n nh cao: Linux c th chy nhiu nm, khng cn reboot.

Linux v Windows
Windows l h iu hnh c thit k cho single users. Unix l h iu hnh c k cho multi users. Nhiu ngi cng chy mt chng trnh trn mt my tnh vo cng mt thi im. T Windows 95, h tr multi user. Tuy nhin, Unix h tr multi user t 1969.

Linux v Windows
S tch bit gia GUI v Kernel:
GUI l thnh phn chim nhiu memory nht, v rt phc tp, d b li. Vi Windows, GUI v kernel l khng th tch ri => tin li cho ngi dng. Vi Linux, GUI tch bit vi kernel. Ngi s dng c th khng s dng GUI, hoc s dng nhng GUI khc nhau. Cho php ty bin, ph hp vi server, vn khng cn GUI, tit kim c memory, v t b li.

Linux v Windows
Tt c nhng cu hnh ca Windows c lu trong registry. Khi mun chnh sa rt phc tp. Thng phi c phm mm thirdparty. Cu hnh ca Linux l file text, v vy d dng chnh sa theo mun. C th xa b hon ton nhng cu hnh c khi khng cn => khng c mt chun cu hnh. Mi dch v nh ngha mt chun cu hnh ring.

Li ch & hn ch ca Linux
Tnh n nh cao v hu nh khng c virus. Nhiu kin cho rng ai cng c th kim sot source code khin n khng an ton. Tuy nhin, b mt khng phi l an ton. Code ca linux c hng ngn programer kim tra. Nu c bug, d dng c tm thy hn m ngun ng.

Installing Linux in a Server Configuration

Ni dung
Tm tt cc bc ci t.

Kim tra s h tr phn cng.


Cu hnh mng.

Linux file system.


Linux boot loader. Cc mode hot ng ca Linux.

Cc bc ci t
Chn la kiu ci t:
T CD local. Qua mi trng mng. T mt volume trn network server. Dng CD shared t my tnh khc Qua FTP, HTTP.

Cc bc ci t
Kim tra s h tr phn cng. Phn chia partition:
Phn chia t ng hoc theo nh dng ring. Bt buc phi c phn vng / v /swap. Chia cc phn vng cn li theo nhu cu.

Cu hnh mng. Chn la software ci t. Chn la boot loader. Tin hnh ci t.

H tr phn cng
Kim tra s h tr phn cng.
http://hardware.redhat.com/hcl.

Hu ht cc distribution ca Linux t nhn din cu hnh phn cng nh: PCMCIA, CD-ROM, Hard drive, Laptop issues, Memory, NIC, Modem, Mouse, SCSI adaptor Cn ch n nhng thit b phn cng c bit, mi.

Linux file system

Linux file systems


Mc nh, cc phn vng c mount trn phn vng / /swap: virtual memory. /bin: lnh quan trng. /boot: file cu hnh boot loader. /dev: file devices. /etc: file cu hnh. /home: d liu ca users. /lib: file th vin quan trng, v kernel module.

Cu hnh mng
Thit lp cc thng s cu hnh mng cho server:
IP Address Netmask Gateway IP Address Nameserver IP Address Domain name Hostname

Linux boot loader


Boot loader
LILO GRUB

Boot loader cho php chn h iu hnh no boot. Ti boot loader, c th can thip bng command thay i cc tham s boot

Linux boot loader


File grub.conf boot=/dev/sda default=0 timeout=10 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Linux Fedora (2.6.5-1.358smp) root (hd0,0) kernel /vmlinuz-2.6.5-1.358smp ro root=LABEL=/ rhgb quiet initrd /initrd-2.6.5-1.358smp.img title Windows 2000 rootnoverify (hd0,0) chainloader +1

Mode hot ng ca Linux


Linux c cc mode hot ng sau:
0: shutdown. 1: single mode. 2: multi user without networking. 3: multi user with networking. 4: unused. 5: graphic. 6: reboot.

CC LNH CN BN
Kin trc h thng Linux Gii thiu ve Shell H thng file Cc lnh thng dng Qun l file v th mc Mt s tnh nng ca bash shell Mt s lnh hu ch

Kin trc h thng Linux


Kernel Shell Applications and Utilities

Shell l g?
B thng dch dng lnh Mt chng trnh ng dng c bit Tng tc vi h iu hnh Cung cp kh nng lp trnh n gin

Login v du nhc Shell


[root prompt] # [user prompt] $ Kt thc phin lm vic ca Shell ^D (Ctrl+D) exit Logout Lu : Linux phn bit hoa thng Shell mc nh trn Linux la Bash( GNU Bourne-Again Shell)

Job control
Tc v chy nn
V d: # google & #jobs #bg <number> #fg <number>

xem tc v ang chy chay background chy foreground

Cu trc h thng file

Cu trc h thng file


/boot /bin /dev /etc dng /home /lib /mnt kernel v cu hnh boot cc lnh c bn khai bo thit b cu hnh file h thng v ng th mc d liu ngi dng th mc d liu dng chung th mc mount (Cdroom,floppy)

Cu trc h thng file


/proc thng tin process (psedu-file system) /sbin cc lnh qun tr /tmp d liu tm /usr ng dng v th vin /var d liu bin ng

Kiu file
File thng thng: program, text, library. Th mc: container File c bit: divice, socket, pipe Lin kt symbolic links: symlinks

Quy c t tn file
Ti a 225 k t C th bt k k t no k c k t c bit File n bt u bng mt du chm .
.desktop/ .kde/

ng dn
ng dn tuyt i: bt u bng du /
/
bin

/etc /usr /usr/bin


../sbin ./

ng dn tng i

ng dn c bit:
..-th mc cha .-th mc lm vic hin ti

Thc thi lnh


Lnh l mt chng trnh, script thng dch, hoc lnh built-in ca shell Thc thi lnh trn dng lnh $ command $ /full/path/to/command $ relateive/path/to/command V d [usr]$ ls

Cc lnh c bn
shutdown h +5 Tt my sau 5 pht shutdown r +5 Reboot sau 5 pht shutdown h now Tt my ngay shutdown r now Reboot ngay shutdown c Hy b tt c lnh
Note:
halt init 0 shutdown h now reboot init 6 shutdown r now

Cc lnh c bn
su user add passwd <user> Chuyn i user To user i pass user

Cc lnh c bn
passwd pwd cd ls -i mt khu ng nhp -in th mc lm vic hin ti -chuyn i th mc lm vic -lit k ni dung th mc

Cu trc dng lnh


Command [options] [arguments] Cc lu v dng lnh: Cc thnh ph n phn tch bng khong trng Tu chn thng bt u bng - hoc -- Nhiu tu chn c th kt hp bng mt du - Tu chn v c php lnh xem trong man page Lnh c th khng tun theo quy tc trn Linux cho php tu chn c th theo sau tham s

Tr gip v lnh
man info hng dn dng lnh
$ man command $ man k keyword

hng dn dng info

Duyt cc man page: spacebar trang k b trang trc q quit /keyword tm trong ni dung man page

Phm iu khin Terminal


^C cancel tc v ^D end-of-file ^\ thot khi lnh ang thc thi (quit) ^S ngng xut mn hnh (screen) ^ cho php xut mn hnh ^H/ <- xo li 1 k t ^W xo li 1 t ^U xo li n u dng ^K xo li n cui dng Arrow di chuyn trn dng lnh

Qun l file v th mc
To Sao chp Di chuyn/i tn Xo Xem ni dung

mkdir - to th mc
mkdir [OPTIONS] DIRECTORY ... $ mkdir dir1 $ mkdir dir1 dir2 -p : to th mc cha nu cha tn ti $ mkdir -p dir3/dir4

rmdir xo th mc rng
rmdir [OPTIONS] DIRECTORY ... $ rmdir dir1 $ rmdir dir1 dir2 -p : xo tt c cc th mc to nn ng dn

$ rmdir -p dir3/dir4 (<=> $ rmdir dir3/dir4 dir3)

touch, cat to file


$ touch file1 $ touch file2 file3

$ cat > file4 C th nhp vo ni dung dng text kt thc bng ^D trn dng trng ^

cp copy file/th mc
cp [OPTIONS] SOURCE DEST $ cp file1 file5 $ cp file1 dir1 -f: ghi khng cn hi (force) -i: hi trc khi ghi (interactive) -R,-r: copy ton b th mc k c con $ cp -r dir1 dir5

mv di chuyn/i tn

mv [OPTIONS] SOURCE DEST


$ mv file5 file6 $ mv file1 dir5 $ mv dir1 dir2

-f: ghi khng cn hi (force) -i: hi trc khi ghi (interactive)

rm xo file/th mc
rm [OPTIONS] FILE $ rm file6 $ rm file1 file2 -f: xo khng cn hi -i: hi trc khi xo -R,-r: xo ton b th mc k c con $ rm -r dir5 KHNG: # rm -rf /

ln to lin kt (link)
ln [OPTIONS] TARGET [LINKNAME] -f: xo file ch nu tn ti -s: to symbolic link thay v hard link $ ln -s /usr/local/bin $ ln -s dir1 firstdir $ ln -s lib .so.0 lib .so.1

ls xem ni dung th mc
ls [OPTIONS] FILE -a: lit k tt c gm c th mc n -l: hin th kt qu dng y -R : lit k c trong th mc con

Hiu kt qu lnh ls -l
d b c p s l file thng thng th mc block device character device pipe socket symlink

Hin th ni dung file


echo

$ echo This is a test


cat $ cat /etc/group more/less $ more /etc/passwd $ less /etc/bashrc head/tail

Tnh nng ca BASH shell


M rng k t thay th (wildcard) T ng in dng lnh (auto complete) Lnh s (command history) nh hng li xut/nhp (I/O redirection) ng lnh (pipe)

M rng k t thay th
Tn file hoc th mc lm tham s dng lnh c th dng khng tng minh Dng k t thay th cho ton b hoc mt ph n ca tn * tng ng mi chui, k c chui rng ? tng ng mt k t n [...] tng ng mt trong cc k t bn trong ngoc [!/^] khng tng ng vi mt trong cc k t bn trong ngoc \ loi b ngha c bit ca cc k t *,?,)

V d v k t thay th
ls a* ls a?.txt ls [aei]* lit k tt c cc tn bt u bng a lit k tt c tn dng a?.txt vi ? l k t bt k lit k tt c cc tn bt u bng a,e, hoc i lit k tt c tn bt u t a n d v kt

ls [a-d]*[0-9] thc t 0 n 9

ls [!L-T]* lit k tt c cc tn khng bng u t L n T

T ng in dng lnh
Nhn <Tab> t ng in y dng lnh
username, hostname, command, filename lit k tt c kh nng c th

$ cd /usr/lo<Tab> (/usr/local) $ cp<Tab><Tab> cp cpp cpio cproto $ cd dir<Tab><Tab> dir1 dir2 dir3

Lnh s
Danh sch cc lnh thc thi lu trong ~/.bash_history ^P, <Up> lnh k trc ^N, <Down> lnh k sau history: in ra danh sch cc lnh thc thi $ history 1 clear 2 cd / 3 ls 4 mkdir /tmp/dir1

!n: thc thi li dng lnh th n !string: thc thi li dng lnh ngay trc bt u bng string

nh hng li xut/nhp
Ch s xut nhp chun Stdin 0 Stdout 1 Stderr 2

< nh hng li nhp > nh hng li xut (overwrite) >> nh hng li xut (append) 2> nh hng li bo li (overwrite) 2>> nh hng li bo li (append) 2>&1 nh hng li li/xut chung

S dng nh hng li
$ ls -la ~ > homelist.txt $ cat < homelist.txt $ ls -l ~/dir1 >> homelist.txt $ ls -lR /etc 2>/dev/null

$ ls -lR /var > varlist.txt 2>&1 (<=> $ ls -lR /var &> varlist.txt)

ng lnh(pipe)
Xut ca lnh trc l nhp ca lnh sau trong ng lnh K hiu ng lnh: |
$ command1 | command2 | ... | commandn $ ls -la ~ | less $ cat /etc/profile | more

Quyn truy cp file


S hu v quyn truy cp Biu din quyn truy cp Thay i quyn truy cp

S hu v quyn truy cp
Tt c file v th mc thuc s hu user to ra chng
Quyn truy cp file c chia lm 3 nhm User ch s hu file (owner) Group nhm c user l thnh vin Others cc user khc cn li trn h thng Xem quyn truy cp vi lnh ls -l

Biu din quyn truy cp


user | group | others rwx rwx rwx Biu din dng k hiu r cho php c w cho php ghi x cho php thc thi loi b quy n Biu din dng s r=4 w=2 x= 1 -=0

nh danh v tc v
nh danh quyn truy cp u user, ch s hu file g group, nhm c user l thnh vin o others, cc user khc trn h thng a all, tt c user (u, g v o) Tc v trn quyn truy cp + thm quyn loi b quyn = gn quyn

chmod thay i quyn


chmod [OPTIONS] MODE FILE -R : thay i c trong th mc con V d s dng chmod g+w thm quy n ghi cho group o-rwx loi b tt c cc quy n ca others u+x thm quy n thc thi cho user +x thm quy n thc thi cho tt c a+rw thm quy n ghi cho tt c ug+r thm quy n c cho user v group o=x ch cho php thc thi vi others

Mt s lnh hu dng
Nn file v lu tr tar, gzip/gunzip/zcat, bzip2, bunzip2/bzcat X l vn bn text head, tail, cut, sort, uniq, tr, tac, wc Thao tc trn h thng file file, dd, du, df, sync, find, grep Tin ch khc w, date, dmesg, dirname, basename, id, who, uname, tee, whereis, which, locate, sleep ,

tar tin ch lu tr
tar [OPTIONS] [DIRECTORY/FILE] -c: to mi mt archive -x: trch file t mt archive -z: nn/gii nn archive bng gzip -j: nn/gii nn archive bng bzip2 -f: s dng archive c ch nh bi file $ tar zcvf dir1.tar.gz dir1/ $ tar zcvf alldir.tgz dir1 dir2 dir3 $ tar jxvf kernel.tar.bz2

find tm kim file


find [PATH] [EXPRESSION]
$ find ./ -name *.txt -ls $ find /usr/local -type f -print $ find /usr/X11R6 -type d $ find . -perm 755 -a -type f $ find . -type d | xargs chmod +x

grep tm trong file


-i: khng phn bit hoa thng -n: km theo s th t dng khi xut kq -r: tm lp li trong th mc con -v : tm nghch o $ grep -n sv /etc/passwd $ grep -n false /etc/passwd $ grep -ri LANG /etc/X11 $ grep -H network /etc/sysconfig

Qun l User

Cc file lu thng tin


/etc/passwd : lu thng tin user /etc/shadow : lu thng tin password /etc/group : lu thng tin nhm => xem thng tin, s dng lnh sau: #more /etc/passwd

Cu trc cc file thng tin


/etc/passwd root:x:0:0:root:/root:/bin/bash Chi tit theo th t: root : user name x : password 0 : Uid 0 : Gid root : m t /root : home directory /bin/bash : shell

Cu trc cc file thng tin


/etc/shadow root:$1$98aycrr:13977:0:99999:7::: Chi tit theo th t: root: user name $1$98aycrr : password m ha 13977 : ngy password thay i gn nht 0 : ngy trc khi password ht hn 99999 : ngy password ht hn 7 : thi gian ch trc khi pasword ht hn Lu "password m ha": - du * cha t password hoc b disable - du !! password b lock

Cu trc cc file thng tin


/etc/group adm:x:4:root,adm,deamon Chi tit theo th t: adm : group x : password group (ch s dng trn h thng trc kia, hin ti khng cn s dng) 4 : GID root,adm,deamon: cc thnh vin trong group Lu :

UID=0 user l root hoc user c quyn tng ng root UID>0 v UID <500:>=500 l user thng

To, xa, hiu chnh user


To user #useradd [option] [user name] v d: #useradd -c "user root" txchien option: -c : thng tin m t cho user -g : ch ra group chnh cho user -s : ch ra shell cho user (vd:/bin/shell; /sbin/shell: ch logmail, khng login h thng; ...) -G : ch ra group ph cho user (mt user ch c mt group chnh v c th c mt hoc nhiu group ph) -d m : ch ra home directory cho user, vd: #useradd -d /data/txchien -m txchien

To, xa, hiu chnh user


t password #passwd [user name] Xem thng tin user #id [user name] Hiu chnh thng tin #usermod [option] [user name] option: -c : thng tin m t cho user -g : ch ra group chnh cho user -s : ch ra shell cho user (vd:/bin/shell; /sbin/shell: ch logmail, khng login h thng; ...) -G : ch ra group ph cho user (mt user ch c mt group chnh v c th c mt hoc nhiu group ph)

To, xa, hiu chnh user


Lock user #usermod -L [user name] or #passwd -l [user name] Unlock #usermod -U [user name] or #passwd -u [name user] Delete user #userdel [option] [user name] option: -r : xa lun home directory

To, xa group
To group #groupadd [group name] vd: #groupadd ketoan Xa group *(lu trong linux, ch xa c group rng!) #groupdel [tn group] Hiu chnh group #viqr = vi /etc/group

Phc hi mt khu cho user qun tr


Trong trng hp mt mt khu user root, c nhiu cch phc hi mt khu:
Dng a khi ng Dng boot loader LILO hoc GRUB

Dng boot loader


Khi ng my Khi GRUB Screen hin ra bm e edit boot loader(nu c mk th nhp mk GRUB)

Dng boot loader

Dng boot loader

File Permission
Trong Linux va Unix mi th u l file Tt c cc file trn h thng u co permissions: allow, prevent thc hin Viewing, modifying va executing. Mi file u co s hn ch truy cp vi cc permissions khc nhau, hn ch user vi owner/group. Cc permission c tham chiu bng cc bits.

File Permission
Mi 1 i tng gn vi 3 loi quyn: read ,write v execute .V mi 1 quyn ny li c ch nh cho 3 loi user: owner: ch s hu ca i tng mc nh ban u l user to ra i tng group: 1 nhm cc user chia s chung quyn hn truy cp - mc nh ban u l group m owner trn thuc v other: tt c cc user khng thuc 2 nhm trn

File Permission
chown chgrp chmod *note: : thay i quyn truy cp user : thay i quyn truy cp group : thay i mode : thay i c file con trong th mc

-R

3 kiu access restrictions

Permission read write execute

Action view edit execute

chmod option r4 w2 x1

3 kiu user restrictions


User owner group /s output -rwx-----rwr---

other

---rwx

chmod vi Letters
Usage: chmod {options} filename
option u Definition owner

g o
x w r + =

group other
execute write read Add permission Remove permission Set permission

chmod vi Numbers
Usage: chmod {options} filename
Option # --#-- # Definition owner group other

1
2 4

execute
write read