You are on page 1of 16

Cc lnh cn bn trong linux

Lnh thng k dung lng th mc Linux [root@unix1 webhostings]# du -sh * Bi vit ny khng ni v cch ci t m i vo chi tit, sau khi ci t xong s dng nh th no.u tin bn cn login vo h thng, bn login vo vi user root, mt khu do bn t lc ci t.User root l user c quyn ti cao (hay quyn cao nht i vi mt h thng Unix). x dng dng lnh bn cn bt command shell ln, ci ny tng t nh MS DOS ca windows. [root@hautp ~]# Bn xem thng tin v user mnh ang login bng lnh: id [root@hautp ~]# id uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(di sk),10(wheel) context=root:system_r:unconfined_t:SystemLow-SystemHigh Cc ch s uid v gid s cho bit quyn hn ca bn trn h thng. 0 l quyn cao nht ri.By gi bn mun login vi user mi bn s dng lnh : useradd [root@hautp ~]# useradd quantrihethong vy l bn c user mi l quantrihethong trong h thng.Lnh useradd c rt nhiu tham s khc nhau, xem chi tit bn dng lnh man [quantrihethong@hautp root]$ man useradd Lc trc khi to user quantrihethong chng ta cha to mt khu, by gi to mt khu cho user ny, bng lnh passwd. [root@hautp ~]# passwd quantrihethong Changing password for user quantrihethong . New UNIX password: Sau nhp mt khu vo. chuyn sang user ny bn dng lnh : su [root@hautp ~]# su quantrihethong bn kim tra li bng cch nh lnh : id [quantrihethong@hautp root]$ id uid=501(quantrihethong) gid=501(quantrihethong) groups=501(quantrihethong) context=root:system_r:unconfined_t:SystemLow-SystemHigh Tip theo l cc lnh c bn vi th mc :Bn cn bit hin ti ang th mc no bn dng : pwd [quantrihethong@hautp root]$ pwd /root Vy l user quantrihethong ang th mc /root.Cc lnh v th mc trn unix

tng t nh trn MS DOS ca windows, ch c mt s im khc bit.Lnh ls s tng ng vi dir. rm : xa file rmdir : xa th mc mv : di chuyn hoc i tn file cp : copy file, th mc cd : chuyn th mc Thc hnh : [quantrihethong@hautp root]$ ls ls: .: Permission denied Vy l li ri, user quantrihethong khng c quyn s dng lnh ls. Lc trc khi to user ti cha thm shell cho user nn user s khng c quyn s dng lnh ny. By gi ti s thm shell cho user.Trc tin cn chuyn v user root bng lnh : su root, n s hi mt khu --> nhp mt khu ca root vo.Bn dng lnh : usermod thay i thng tin ngi dng, c php nh sau: SYNTAX usermod [options] [user] Bn cha bit shell nm u, nn cn dng lnh whereis xem v tr ca shell [root@hautp ~]# whereis bash bash: /bin/bash /usr/share/man/man1/bash.1.gz [root@hautp ~]# usermod -s /bin/bash quantrihethong Tip theo li su v user quantrihethong [quantrihethong@hautp root]$ ls ls: .: Permission denied a ah, vn b li. Vy l khng phi ri, lc ny ta hiu sai. Khng phi user quantrihethong khng c quyn dng shell,v vn dng c lnh pwd,... M l user quantrihethong khng c quyn i vi th mc /root y l im rt khc bit vi windows, Unix phn quyn rt cht ch da theo cc quyn : Read - Write - Execute (c - Ghi - Thc thi) Cc quyn ny c th hin bng k hiu : r - w - x hoc 4 - 2 -1 V vi mt th mc quyn s c phn cho : Owner - Group - others (ngi s hu - nhm - ngi khc)

xem quyn ca th mc root ta dng lnh ls vi tham s al: [root@hautp /]# ls -al...drwxr-x--- 20 root root 4096 Nov 28 14:35 root... Nhn vo dng trn ta s nhn c thng tin nh sau : Owner l root Group l root drwxr-x--- : quyn i vi ngi dng, ch d u c ngha y l th mc, tip theo l quyn ca owner : rwx :--> owner c ton quyn trn th mc ny, owner l root nn user root c ton quyn trn th mc ny. r-x :--> group c quyn c v chy file, khng c quyn ghi vo th mc ny. --- :--> others khng c quyn g i vi th mc ny. quantrihethong khng thuc group root nn khng c quyn g. Ni thm v cch th hin quyn i vi th mc, nh trn ci ni v cch th hin cc quyn.drwxr-x--- s tng ng 740, khi th mc quyn t do nht l rwxrwxrwx - 777 tc l bt k ai cng c y cc quyn vi th mc . thay i quyn bn dng lnh CHMOD, thay i owner bn dng lnh chown, thay i group bn dng lnh chgroup.Vic t quyn hn ng s l cc k quan trng i vi mt h thng, khng ch UNIX. H thng th mc trn * NIX, bi ny ti ly v d c th l Fedora 6. Khi th mc gc / bn nh ls s nhn c: [root@hautp ~]# cd / [root@hautp /]# ls bin boot dev etc home lib lost+found media misc mnt net opt proc root sbin selinux srv sys tmp usr var l cc th mc trn mt h thng Unix. * Th mc /bin y l thc mc cc k quan trong ca 1 h thng unix, th mc ny cha gn nh tt c cc lnh ca h thng. * Th mc /etc Th mc ny cha cc cc file con file ca h thng, cng nh cha thng tin v cc service cn khi ng khi h iu hnh chy. i vi h iu hnh Linux th cc service chy lc khi ng s c t trong th mc init.d.

i vi h iu hnh SUN Solaris th cc service chy lc khi ng s c t trong th mc rc2.d. Cc th mc trn s thay i ty h thng. *Th mc /usr Th mc ny cha file v chng trnh ca cc user trn h thng. Mt iu th v trn h thng Unix l tt c u l file, k c ci gi l th mc cng l file.smile_regular *Th mc /dev Khi vo th mc ny nh lnh ls bn s thy rt nhiu file mu vng. chnh l tt c cc thit b phn cng m h iu hnh dng, trn h thng Unix tt c u l file, nh ti ni trn. V d : cng s l /dev/hda, c th c 2 loi cng IDE v SCSI, nh bn dng thng thng l IDE, SCSI thng c dng cho cc my ch v dung lng thng l 36GB, 72GB,... *Th mc /boot Th mc ny cha "li" ca h iu hnh hay cn gi l kernel. V d y l kernel my ca ti : Trn h iu hnh SUN n s khng phi l th mc /boot, n l th mc /platform bit thng tin v kernel bn dng lnh : uname -an [root@hautp /]# uname -an Linux hautp 2.6.17-1.2157_FC5 #1 Tue Jul 11 22:55:46 EDT 2006 i686 i686 i386 GNU/Linux * Th mc root - th mc ca user root Khi bn dng mt user khc truy nhp vo th mc ny, bn s khng c quyn g vi th mc ny, ging nh user quantrihethong phn trc. y chnh l "Users home directory" th mc ring ca user. Trn h thnh Unix khi mt user mi to ra n s to km theo 1 th mc cho user . Thng thng cc th mc ny s nm trong th mc /home. Nhn tin y ti ni lun v th mc /home. Th mc /home l th mc cha cc th mc ca ngi dng: * Th mc /sbin Th mc ny l mt th mc gii hn quyn hn, n cha cc chng trnh kiu nh th mc /bin. Nhng bn khng th lm g n n c. Ch nhng user c quyn root mi c th Shutdown cc chng trnh y.

* Th mc /tmp Th mc ny ng nh tn ca n, n cha cc file tm do h thng sinh ra. V chia s cho bt k chng trnh no nn th mc ny c t quyn hn rt thoi mi : drwxrwxrwt 11 root root 4096 Nov 29 04:05 tmp Chc nng ca n cng ging nh th mc temp ca windows. * Th mc /var Th mc ny cha cc file c th thay i kch thc (variable size), nn thng thng trong th mc ny s cha cc database nh : mysql,.. hay mail server,... * Th mc /lib Lib l vit tt ca library. Th mc /lib cha cc file th vin chng trnh. Mi mt chng trnh s c th vin ring ca mnh. * Cc th mc khc : - /mnt - /cdrom - /floppy Ban u tt c cc th mc ny u rng. Khi bn cm USB vo n s nm trong /mnt hoc bn cn mount n vo trong /mnt (ci ny ni sau happy). Khi cho a CDROM vo th d liu s c t ng mount vo th mc /cdrom. Tng t i vi floppy.

Phn ny ni v cch : tt my nh th no ? khi ng nh th no ?


* Lnh : shutdown S dng lnh : man shutdown xem thng tin v lnh ny SYNTAX shutdown [options] when [message] OPTIONS -c Cancel a shutdown that is in progress. -f Reboot fast, by suppressing the normal call to fsck when rebooting. -h Halt the system when shutdown is complete. -k Print the warning message, but suppress actual shutdown.

-n Perform shutdown without a call to init. -r Reboot the system when shutdown is complete. -t sec V d : Tt ngay lp tc : shutdown -h now Khi ng li ngay lp tc: shutdown -r now Tt my vo lc 8 ti (pm): shutdown -h 20:00 Sau 10 pht th tt my: shutdown -h +10 * Lnh : halt, reboot, poweroff T kernel 2.74 tr v sau ny, lnh halt, reboot khng c gi trc tip m n c tch hp vo trong lnh shutdown nh bn thy trn. Nu bn dng cc kernel c th vn dng c cc lnh ny.

Trnh son tho vn bn.


Trn windows c rt nhiu trnh son tho khc nhau nh office, wordpad, notepad... Trn *nix cng vy, nhng trnh son tho a thch c l l vi. Trnh son tho ny c l l ph bin nht v thng dng nht trn cc h thng Unix cng tng t nh notepad ca windows. truy nhp vi trong ca s terminal bn nh : vi [root@hautp /]# vi Trnh son tho s hin ra. Nh bn Fedora ti ang dng th n thay th vi bi VIM : VIM son tho "thun tay hn" vi happy bn dng th m xem big grin to 1 file mi bn nh : vi <tn file> [root@hautp /]# vi hello Bn nhn phm "i" kch hot ch Insert, sau bn nh "Hello world!" ghi li file bn bm phm "ESC" thot khi ch Insert. Sau nh ":qw" lu li v thot ra khi vi. "hello" [New] 1L, 14C written

[root@hautp /]# more hello hello world ! [root@hautp /]# Chi tit cc lnh ca vi c l phi thc hnh nhiu mt cht mi nh c. Tham kho : http://www.ss64.com/bash/vi.html http://www.eng.hawaii.edu/Tutor/vi.html VI Editor Commands Switch to Text or Insert mode: Open line above cursor O Insert text at beginning of line I Insert text at cursor i Insert text after cursor a Append text at line end A Open line below cursor o Switch to Command mode: Switch to command mode <ESC> Cursor Movement (command mode): Scroll Backward 1 screen <ctrl>b Scroll Up 1/2 screen <ctrl>u Go to beginning of line 0 Go to line n nG Go to end of line $ Scroll Down 1/2 screen <ctrl>d Go to line number ## :##

Scroll Forward 1 screen <ctrl>f Go to last line G Scroll by sentence f/b ( ) Scroll by word f/b w b Move left, down, up, right h j k l Left 6 chars 6h Directional Movement Arrow Keys Go to line #6 6G Deleting text (command mode): Change word cw Replace one character r Delete word dw Delete text at cursor x Delete entire line (to buffer) dd Delete current to end of line D Delete 5 lines (to buffer) 5dd Delete lines 5-10 :5,10d Editing (command mode): Copy line yy Copy n lines nyy Copy lines 1-2/paste after 3 :1,2t 3 Paste above current line P Paste below current line p Move lines 4-5/paste after 6 :4,5m 6 Join previous line J Search backward for string ?string

Search forward for string /string Find next string occurrence n % (entire file) s (search and replace) /old text with new/ c (confirm) g (global - all) :%s/oldstring/newstring/cg Ignore case during search :set ic Repeat last command . Undo previous command u Undo all changes to line U Save and Quit (command mode): Save changes to buffer :w Save changes and quit vi :wq Save file to new file :w file Quit without saving :q! Save lines to new file :10,15w file Shells l g ? Bn c th hiu nm na shell l 1 cch computer giao tip vi ngi dng hay ni cch khc l cch computer nhn lnh t ngi dng. Thng thng trn Linux dng "bash" shell. Shell l gip ngi dng lm vic vi my tnh d dng hn vi nhng cu lnh "thn thin" mang tnh cht gi nh. V d : cn copy tt c cc file trong th mc A vo th mc B cu lnh l : cp /A/* /B * File '.bashrc' Mi mt user khi c to ra s c 1 shell cho n nh ti ni phn trc, nh ngha shell cho user nm trong file .bashrc trong th mc /home/<tn user>, v d y l /home/hautp [root@hautp /]# cd /home/ [root@hautp home]# cd quantrihethong/ [root@hautp quantrihethong]# ls -al total 56 drwxr-xr-x 2 quantrihethong quantrihethong 4096 Nov 29 06:00 . drwxr-xr-x 4 root root 4096 Nov 28 14:49 .. -rw-r--r-- 1 quantrihethong quantrihethong 24 Nov 28 14:49 .bash_logout -rw-r--r-- 1 quantrihethong quantrihethong 191 Nov 28 14:49 .bash_profile -rw-r--r-- 1 quantrihethong quantrihethong 124 Nov 28 14:49 .bashrc

-rw-r--r-- 1 quantrihethong quantrihethong 120 Nov 28 14:49 .gtkrc -rw------- 1 quantrihethong quantrihethong 35 Nov 29 06:00 .lesshst [root@hautp quantrihethong]# more .bashrc # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # User specific aliases and functions [root@hautp quantrihethong]# more .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH unset USERNAME Trong file .bashrc c ni n cc alias do ngi dng nh ngha. # User specific aliases and functions Alias l g ? alias thng thng c hiu l mt ci tn khc. Alias y cng gn nh th. v d ti thm dng : alias rm='rm -i' Th iu ny c ngha l khi ti nh lnh rm trong terminal th lnh ny s c hiu l rm -i y chnh l alias ca rm. V sao li cn n alias ? trn trong lnh rm c tham s -i, i tc l interactive (prompt before any removal) c ngha l khi c tham xa -i th bt c file no b xa h iu hnh s hi xem ta c chc chn xa khng. Nu dng tham s -f : force (ignore nonexistent files, never prompt) th h iu hnh s xa m khng cn hi. Khi bn mun xa nhanh th hy dng tham s ny. Vic dng alias nhm mc ch to 1 "route" cho ngi dng. c bit i vi nhng my tnh quan trng nh my ch chng hn vic xa file cn phi ht sc thn trng, nu nh bn dng lnh sau : rm -R -f * m khng to alias nh trn s khin

ton b s file trong th mc hin ti ca bn bin mt ngay lp tc --> mt di nh ci bm smile_confused Bn c th to alias tm thi bng cch dng lnh alias hoc xa 1 alias bng lnh unalias: SYNTAX alias [-p] [name[=value] ...] unalias [-a] [name ... ] Vy l bn hiu s qua shell l g ?

Cc lnh cn bn trong linux ( tt )


Cc lnh thng dng trn h thng Unix Ti ch a ra cc lnh km chc nng, cc tham s ca n th bn cn xem thm. 1. Lnh man, info v apropos : thng tin v lnh 2. Lnh cd : chuyn th mc 3. Lnh ls : lit k file thng thng hay dng ls -al hoc ls -l 4. Lnh file : xem thng tin loi file ca 1 file file <tn file> 5. Lnh more v less : xem ni dung file more <tn file> 6. Lnh cat v tail : xem ni dung file cat <tn file> 7. Lnh cp : lnh copy 8. Lnh mv : lnh di chuyn hoc i tn file, th mc 9. Lnh mkdir : to th mc mi 10. Lnh rm v rmdir : xa file v xa th mc rng 11. Lnh dir : bng vi ls -l 12. Lnh pwd : xem v tr th mc hin thi 13. Lnh date : xem ngy 14. Lnh cal : xem lich, v d cal 2006 15. Lnh exit : thot khi terminal Cn sau y l mt s lnh yu thch : 1. 2. 3. 4. 5. Lnh Lnh Lnh Lnh Lnh touch : to file find : tm kim (s c 1 bi vit ring v lnh ny) grep : tm kim ni dung file h tr regular expression who, whoami, whatis, whereis, which : ng nh ngha ca cc t ny echo : hin th ni dung 1 bin ,...

Cc lnh dnh cho qun tr h thng : 1. Lnh last : hin th cc user login gn y 2. Lnh df : xem thng tin a, thng thng hay dng df -h 3. Lnh du : xem thng tin dung lng file, th mc 4. Lnh top : ci ging nh taskmanager ca windows, n s hin th thng tin v cc processes 5. Lnh free : xem tnh hnh b nh 6. Lnh ps : xem thng tin processes

7. Lnh kill : tt process 8. Lnh mount v unmount : 9. Lnh chmod : thay i permissions i vi file 10. Lnh chown : thay i ngi s hu i vi file 11. Lnh chgrp : thay i group i vi file 12. Lnh chroot Ngoi cc lnh trn cn c rt nhiu lnh khc, c th tham kho ti y : http://www.ss64.com/bash/ Backup sao lu gi liu vi UNIX Cc lnh cn dng : tar, gzip, gunzip V d vi tar : tar -czvf MyArchive Source_file hoc tar --create --gzip --verbose --file=MyArchive Source_file tar -xzvf MyArchive Source_file hoc tar --extract --gunzip --verbose --file=MyArchive Source_file gzip l mt phn ca tar, tuy nhin gzip v gunzip vn dng c c lp. Vic backup v sao lu nn vit thnh cc job h thng t ng lm. V d ti cn backup d liu ca mysql hng tun vo Ch nht. #!/bin/bash Date=`date '+%a'` Day=`date '+%m%d'` if [ $Date == 'Sun' ] then cd /mysqldata for ix in * do if [ -d $ix ] then tar -czvf /quantrihethong/backup/database_$Day.$ix.tar $ix fi done fi on script bn cho vo 1 file, v d : db_backup.sh Sau chmod +x cho file db_backup.sh c ngha l cho file ny c quyn chy sau to schedule cho file ny. Ci ny n tng t nh schedule task ca windows.

to schedule trn Linux bn dng crontab. [root@hautp etc]# ls -l| grep cron -rw-r--r-- 1 root root 329 Feb 11 2006 anacrontab drwxr-xr-x 2 root root 4096 Feb 16 2006 cron.d drwxr-xr-x 2 root root 4096 Aug 3 13:43 cron.daily -rw-r--r-- 1 root root 0 Aug 3 13:21 cron.deny drwxr-xr-x 2 root root 4096 Dec 11 2005 cron.hourly drwxr-xr-x 2 root root 4096 Aug 3 13:18 cron.monthly -rw-r--r-- 1 root root 255 Dec 11 2005 crontab drwxr-xr-x 2 root root 4096 Aug 3 13:21 cron.weekly [root@hautp etc]# more crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly Cc job ny s c t trong cc file trn hoc trong th mc : /var/spool/cron/crontabs S dng crontab - l bit cc jobs ca user hin ti. Nu cha c dng lnh crontab -e to jobs. C php 1 dng trong crontab : Minute(0-59) Hour (0-23) Day of Month (1-31) Month (1-12 or Jan-Dec) Day of Week (0-6 or Sun-Sat) Command 0 2 12 * 0,6 /usr/bin/find Tham kho : Cron and Crontab usage and examples. Ci t v cp nht phn mm trong Linux Trn windows cc file ci t c nh dng .exe, .msi, .vis,... vic ci t rt d dng bng vic chy cc file ny. Trn unix cng tng t nh th. Tuy nhin trong cc bi vit ny ti ch dng terminal, khng dng ch giao din. Vy vic ci t trn trn linux nh th no ? * RPM : Red Hat Package Manager Cc chng trnh s c ui .rpm, c php nh sau : rpm -i new_program.rpm --> ci t chng trnh mi (-i l vit tt ca install) rpm -q program_name --> kim tra xem 1 chng trnh c ci hay cha ?

V d vi Fedora 5/6: [root@hautp sysconfig]# rpm -q mysql mysql-5.0.18-2.1 [root@hautp sysconfig]# rpm -q firefox firefox-1.5.0.1-9 By gi cn nng cp nn Firefox 2.0 Tham kho : http://fedoraproject.org/wiki/Firefox2 Chy lnh : yum -y install firefox Cc lnh c bn vi yum: Ci t : yum -y install <tn phn mm(gi)> G b : yum -y remove <tn phn mm> Xem cc gi ci : yum list <tn phn mm> V d : xem cc gi ci ca php : [root@web ~]# yum list php* Loading "installonlyn" plugin Setting up repositories core 100% |=========================| 1.1 kB 00:00 updates 100% |=========================| 1.2 kB 00:00 extras 100% |=========================| 1.1 kB 00:00 Reading repository metadata in from local files primary.xml.gz 100% |=========================| 306 kB 00:01 ################################################## 1072/1072 Installed Packages php.i386 5.1.6-3.1.fc6 installed php-Smarty.noarch 2.6.13-1.fc6 installed php-bcmath.i386 5.1.6-3.1.fc6 installed php-cli.i386 5.1.6-3.1.fc6 installed php-common.i386 5.1.6-3.1.fc6 installed php-gd.i386 5.1.6-3.1.fc6 installed php-mbstring.i386 5.1.6-3.1.fc6 installed Chi tit cc lnh vi yum : yum -h Khi ht swap th ta cn thm swap file, v d cn thm 1G swap : /usr/sbin/mkfile 1024m /swapfile --> to swap file /usr/sbin/swap -a /swapfile --> cho h thng bit swap file mi nm u n v tnh : kilobytes (k), blocks (b), or megabytes (m)

Kim tra swap file mi c thm hay cha ? swap -l

Ngun : Some examples of using UNIX find command. Introduction The find command allows the Unix user to process a set of files and/or directories in a file subtree. You can specify the following: * * * * * where to search (pathname) what type of file to search for (-type: directories, data files, links) how to process the files (-exec: run a process against a selected file) the name of the file(s) (-name) perform logical operations on selections (-o and -a)

Search for file with a specific name in a set of files (-name) find . -name "rc.conf" -print This command will search in the current directory and all sub directories for a file named rc.conf. Note: The -print option will print out the path of any file that is found with that name. In general -print wil print out the path of any file that meets the find criteria. How to apply a unix command to a set of file (-exec). find . -name "rc.conf" -exec chmod o+r '{}' \; This command will search in the current directory and all sub directories. All files named rc.conf will be processed by the chmod -o+r command. The argument '{}' inserts each found file into the chmod command line. The \; argument indicates the exec command line has ended. The end results of this command is all rc.conf files have the other permissions set to read access (if the operator is the owner of the file). How to apply a complex selection of files (-o and -a). find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print This command will search in the /usr/src directory and all sub directories. All files that are of the form '*,v' and '.*,v' are excluded. Important arguments to note are: * -not means the negation of the expression that follows * \( means the start of a complex expression. * \) means the end of a complex expression.

* -o means a logical or of a complex expression. In this case the complex expression is all files like '*,v' or '.*,v' The above example is shows how to select all file that are not part of the RCS system. This is important when you want go through a source tree and modify all the source files... but ... you don't want to affect the RCS version control files. How to search for a string in a selection of files (-exec grep ...). find . -exec grep "www.ajaxviet.com" '{}' \; -print This command will search in the current directory and all sub directories. All files that contain the string will have their path printed to standard output. If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files. find . -exec grep -q "www.ajaxviet.com" '{}' \; -print This command is very important for process a series of files that contain a specific string. You can then process each file appropriately. An example is find all html files with the string "www.athabascau.ca". You can then process the files with a sed script to change those occurrances of "www.athabascau.ca" with "intra.athabascau.ca".

You might also like