<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Kokono0204&#039;s Blog</title>
	<atom:link href="http://kokono0204.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://kokono0204.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Wed, 09 Nov 2011 01:10:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='kokono0204.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Kokono0204&#039;s Blog</title>
		<link>http://kokono0204.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://kokono0204.wordpress.com/osd.xml" title="Kokono0204&#039;s Blog" />
	<atom:link rel='hub' href='http://kokono0204.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Học PHP Như Thế Nào?</title>
		<link>http://kokono0204.wordpress.com/2010/05/23/h%e1%bb%8dc-php-nh%c6%b0-th%e1%ba%bf-nao/</link>
		<comments>http://kokono0204.wordpress.com/2010/05/23/h%e1%bb%8dc-php-nh%c6%b0-th%e1%ba%bf-nao/#comments</comments>
		<pubDate>Sun, 23 May 2010 17:01:00 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/?p=57</guid>
		<description><![CDATA[Đã Khi nào bạn tự hỏi học PHP sẽ như thế nào , bắt đầu từ đâu. Câu trả lời sẽ nằm dưới đây Để học bất cứ một ngôn ngữ lập trình nào những người mới bao giờ cũng phải trải qua 6 giai đoạn. 1. Quan niệm 2. Thử nghiệm 3. Trải nghiệm [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=57&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p>Đã Khi nào bạn tự hỏi học PHP sẽ như thế  nào , bắt đầu từ đâu. Câu trả lời sẽ nằm dưới đây</p>
<p>Để học bất cứ một ngôn ngữ lập trình nào những người mới bao giờ cũng  phải trải qua 6 giai đoạn.</p>
<p>1. Quan niệm<br />
2. Thử nghiệm<br />
3. Trải nghiệm</p>
<p>4. Kiểm nghiệm<br />
5. Chiêm nghiệm<br />
6. Sáng tạo</p>
<p>Quan niệm<br />
Hãy xác định nhiệm vụ học cho các bạn. Không có định hướng, không còn  tầm nhìn bao quát về thế giới phần mềm và không có cá tính, bạn chắc rồi  cũng rời bỏ sớm ngành phần mềm nếu không thì cũng chỉ là một coder hay  tester ba phải, người mà lúc nào cũng chỉ được coi là một nhân vật bình  thường về mọi mặt, không có sự nổi bật và luôn đứng đầu trong đề cử danh  sách về hưu non.</p>
<p>Tiêu chuẩn số 1 của một nhà phát triển phần mềm giỏi là sự đề cao tự  học. Trường lớp hay trung tâm không phải là nơi đáng tin cậy để học vì  người giỏi ko bao giờ chịu mài đũng quần trên ghế bục giảng để giảng đi  giảng lại một thứ kiến thức liên tục cũ đi sau vài tháng. Nếu người giỏi  thích nói chuyện thì conference là chỗ của họ vì họ thích trao đổi với  các industry expert chứ không phải là dọa dẫm học viên.</p>
<p>Diễn đàn này cũng có thể là nơi để học hỏi nhưng đừng nghĩ là người  có hiểu biết sẽ trả lời bạn một cách tường tận và chi tiết như thể họ là  ông nội của bạn vậy. Muốn họ trả lời hãy học cách hỏi, trước khi hỏi  hãy search. Sau khi nhận được câu trả lời hãy search.</p>
<p>Thử nghiệm<br />
PHP là một công nghệ bao gồm có 6 bộ phận:<br />
+ Bộ thông dịch<br />
+ Ngôn ngữ<br />
+ Cấu trúc dữ liệu<br />
+ Các thuật toán trên dữ liệu hay còn gọi là các API<br />
+ Các công nghệ đi kèm hoặc có thể giao tiếp được với PHP.<br />
+ Các best practice đặc thù của riêng công nghệ đó.</p>
<p>Hãy tập cài đặt bộ thông dịch PHP, Apache, MySQL và chạy triển khai  các ứng dụng PHP có sẵn.<br />
Hãy thử nghiệm ngôn ngữ PHP với các vòng lặp, điều kiện, in, nhúng HTML,  làm quen function, method, class<br />
Cấu trúc dữ liệu là một phần khác của PHP. Ở công nghệ này Hash, Array  và Map đều được gộp vào kiểu array. Rất dễ chịu cho bạn khi bạn đã từng  làm quen với các cấu trúc dữ liệu của C# hay Java.<br />
Cấu trúc dữ liệu không phải là một cấu trúc chết. PHP cung cấp khoảng  hơn 1000 function (API) xử lý nó. Array là một kiểu như vậy. DOM là một  kiểu khác. Làm bài tập trên các API dạng này là một cách tốt để làm quen  với ngôn ngữ và bộ thông dịch. Các bài tập có thể chỉ là in ra một cái  resume, tạo một cái layout đơn giản nhất từ việc sử dụng include, tạo  một form nhập liệu đơn giản, tạo một form upload đơn giản…</p>
<p>Tài liệu sách vở thì có rất nhiều. Bạn nên đọc các chương trong PHP  Manual. Đọc các tutorial trên mạng và luyện tiếng Anh. Các ebook đáng  chú ý:</p>
<p>++ Apress Beginning PHP and MySQL 5 From Novice to Professional 2nd  Edition (2006)<br />
++ Apress Beginning PHP and PostgreSQL E.Commerce (2006)<br />
++ Prentice Hall PHP 5 Power Programming (2004)<br />
++ O’reilly Programming PHP 2nd Edition (2006)</p>
<p>Các công nghệ đi kèm với PHP là một thế giới cực kì phức tạp. Bạn có  thể sẽ làm quen với các công nghệ opcode hay biên dịch mã PHP ra mã nhị  phân để chạy thay vì nhìn nó dưới dạng text. Nó là một extension nguồn  mở của công nghệ PHP. Bạn cũng có thể quan tâm đến việc sử dụng PHP để  giao tiếp với các hệ thống con trong Linux như shared memory, gettext,  socket, stream… Socket và stream là các công cụ vạn năng để PHP giao  tiếp với các hệ thống phần cứng và phần mềm. Bạn cũng có thể quan tâm  đến sử dụng PHP và C trên cùng một file PHP thông thường hay nhúng mã  dll của các thư viện trên Windows vào PHP và sử dụng lại. PHP làm việc  này thông qua việc sử dụng một ext. mã nguồn mở do Wez viết có thể tải  về PECL. Bạn cũng có thể quan tâm đến việc sử dụng PHP để viết các ứng  dụng desktop mà bạn có lẽ vẫn dùng VB để viết bằng cách sử dụng thư viện  GTK.</p>
<p>Tuy nhiên điều bạn có thể sẽ quan tâm hơn cả là liệu PHP được hỗ trợ  như thế nào để giao tiếp với các hệ thống khác. PHP có thư viện để giao  tiếp với Active Directory, LDAP, các hệ thống hỗ trợ  SMTP/POP3/IMAP/FTP/SSL, SMS Gateway, GIS server, Router… PHP được Java  hỗ trợ tốt để trở thành một công nghệ trên Java platform và nhờ đó bạn  có thể gọi các gói Java để sử dụng lại hay sử dụng các application  server của Java như Sun One Webserver, Glassfish application server thay  cho Apache server hoặc sử dụng các dịch vụ Java khác như OpenSSO. JSON  và XML được hỗ trợ buit-in trong PHP cũng là phần rất đáng tìm hiểu vì  các nhà công nghiệp phần mềm ở các tổ chức chuẩn công nghiệp như OASIS,  ECMA hay W3C sử dụng các gọi này để giúp PHP hỗ trợ kiến trúc  Webservice/SOA, SDA, MDA (phần này do IBM đóng góp cho cộng đồng PHP)…..</p>
<p>Phần PHP giao tốt tốt nhất có lẽ là database. OCI do Oracle đóng góp,  DB2 do IBM đóng góp, libmysql do MySQL AB đóng góp và pgsql do cộng  đồng PostgreSQL đóng góp và luôn hỗ trợ các bản database mới nhất. Ngoài  ra PHP có sẵn một database server SQLite nhúng đi kèm với mọi bản PHP  5.0+ có thể giúp bạn tiện trong việc xử lý các nhóm data nhỏ nhưng đủ  mạnh mẽ như Access.</p>
<p>PHP là một công nghệ đặc biệt và nó có các đặc trưng riêng như share  nothing. Điều này làm cho PHP có tính scalability rất tốt và được các  mạng xã hội rất chuộng. Không phải ngẫu nhiên mà Tim Bray, giám đốc các  công nghệ Web của Sun lại cho là PHP có tính scalable hơn Java trên tầng  presentation (<a href="http://www.tbray.org/talks/php.de.pdf" target="_blank">http://www.tbray.org/talks/php.de.pdf</a>) trong khi từ  trước đến nay họ vẫn tự hào là công nghệ Java cho họ thống trị thế giới  web doanh nghiệp, các phần mềm ngân hàng và thị trường viễn thông, vượt  trội cho với công nghệ .NET của Microsoft. Đặc điểm này của PHP cũng là  tâm điểm tạo ra các best practice mà bạn cần khai thác khi lập trình với  PHP.</p>
<p>Trải nghiệm<br />
Là giai đoạn bạn bắt đầu quan tâm đến việc dùng PHP để code các ứng dụng  sản xuất bằng việc tự tin sử dụng các API. Giai đoạn này hoàn toàn phù  hợp với việc lấy một chứng chỉ Zend nếu bạn muốn. Tuy nhiên Zend chỉ  chứng nhận bạn là có kĩ năng để nhớ API, một điều khá cần để code API  với notepad Đặc trưng của giai đoạn này là thực hành để có các cảm nhận  chung về kĩ năng code. Giai đoạn này sẽ đem lại cho bạn một niềm tự hào  trẻ con nhưng rất cần thiết. Giai đoạn này bạn sẽ phải kết hợp PHP với  CSS/XHTML, graphic design, SQL, web authoring, Javascript, web hosting,  system scripting… Là một lập trình PHP, bạn tự biết mình phải đa tài hơn  các lập trình viên ASP.NET, nơi tooling thống trị kĩ năng handcode, hơn  Java, nơi mà kĩ năng viết business logic được chú trọng hơn</p>
<p>Kiểm nghiệm<br />
Nếu như giai đoạn trải nghiệm là giai doạn code bung bét, bug tùm lum,  bảo mật bằng không thì đây chính là lúc bạn quan tâm đến việc refactor  lại code. Bạn có thể tạo ra nhiều function và class hơn trong một nỗ lực  cố gắng làm cho chúng modular hơn. Bạn sẽ cố áp dụng các thư viện như  PEAR hay Smarty, PHPSavant vào ứng dụng của bạn để làm cho cách code của  bạn đi vào chiều hướng ổn định. Lúc này bạn đã thấy được sự cần thiết  phải có một coding convention nhất quán  ([url=http://pcdinh.googlepages.com/phpvietnamcodingstandards]Reference[/url).  Bạn sẽ quan tâm đến bảo mật hơn. Giai đoạn này sẽ giúp bạn có một lối  tư duy chín chắn hơn nhưng nhiều lo ngại hơn vì bạn cho rằng bạn vẫn  chưa thực sự hiểu PHP.</p>
<p>Chiêm nghiệm<br />
Đây là giai đoạn bạn đã tiến đến mức cao. Bạn đã nắm được bản chất của  PHP. Bạn quan tâm đến cơ chế PHP vận hành ở mức compiler hơn. Bạn bắt  đầu học UML, design pattern, tìm hiểu về các hệ thống lớn. Bạn đã vững  vàng trong việc tạo ra các lớp và muốn tổ chức các lớp đó cho khoa học  hơn. Bạn quan tâm đến các software engineering processes và tự hỏi làm  sao để áp dụng XP, Scrum hay các agile methodology khác. Bạn sẽ quan tâm  đến performance, object oriented engineering, database design, system  architect, code review. Bạn sẽ bắt đầu chán Pear và ghét Smarty. Việc  tương tác giữa PHP và các hệ thống khác như Python, Ruby, Jaav, Erlang  .. trở nên quan trọng hơn với bạn lúc này vì cuối cùng thì bạn đã nhận  ra PHP có nhiều điểm yếu nhưng không tới mức phải thay thế PHP bằng 1  giải pháp khác. Các công nghệ mã nguồn mở và tự do sử dụng phân phối như  Java, Perl, Python, Ruby, JRuby, Groovy, Erlang, Lua… cho phép chúng  tận dụng các thư viện của nhau cũng như các điểm mạnh riêng của nhau. Đó  là thời gian bạn thấy sung sướng vì đã chọn 1 công nghệ nguồn mở như  Java hay PHP hay Python.</p>
<p>Sáng tạo<br />
Là giai đoạn cao nhất và cũng phức tạp nhất vì lúc này bạn đã hiểu công  nghệ đến mức có thể triển khai các hệ thống lớn, customize và migrate  các ứng dụng phức tạp. Có đủ tư duy và kiến trúc cũng như độ trải nghiệm  thực tế để thiết kế hệ thống . Lúc này bạn đã có thể tự đặt cho mình  mục tiêu làm các ứng dụng lớn với PHP kiểu như<br />
+ Flickr: 60 000 LOC<br />
+ Digg: 200 000 LOC<br />
Bạn cũng có thể hoàn toàn tự tin dùng PHP để tạo ra các ứng dụng ERP,  CRM, Forecast System vốn thường được code bằng Java, ASP.NET hay Delphi  có truyền thống đắt đỏ, kén server, đòi hỏi nhiều tài nguyên. Lúc đó,  PHP trong tay bạn đã vượt ra khỏi phạm vi của một công nghệ web vì bạn  đã biết đủ PHP và mối quan hệ với các công nghệ bổ sung cũng như kinh  nghiệm triển khai các công nghệ đó.</p>
<p>Bạn đột nhiên quan tâm nhiều đến các cuộc hội thảo. Bạn thấy kinh  nghiệm của các industry expert thật cần thiết và gần gũi với khả năng  của bạn. Có thể một lúc nào đó, bạn thấy mình gần như là một thought  leader. Một lúc khác, bạn lại muốn nhập vai một C hacker.</p>
<p>Lúc này bạn đã lớn rồi. Ai dám khuyên bạn nữa chứ Cẩn thận, bạn có  thể là một PHP Dictator đấy.</p>
</div>
<br />Filed under: <a href='http://kokono0204.wordpress.com/category/php/'>php</a>, <a href='http://kokono0204.wordpress.com/category/uncategorized/'>Uncategorized</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=57&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2010/05/23/h%e1%bb%8dc-php-nh%c6%b0-th%e1%ba%bf-nao/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>
	</item>
		<item>
		<title>PHP Vietnam Coding Converition</title>
		<link>http://kokono0204.wordpress.com/2010/05/23/php-vietnam-coding-converition/</link>
		<comments>http://kokono0204.wordpress.com/2010/05/23/php-vietnam-coding-converition/#comments</comments>
		<pubDate>Sun, 23 May 2010 16:58:08 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/?p=55</guid>
		<description><![CDATA[Chuẩn phong cách viết mã PHPVietnam là một nỗ lực thống nhất phong cách viết mã cho các lập trình viên thuộc nhóm PHPVietnam. Nó là một ràng buộc về các quy tắc viết mã PHP mà các lập trình viên của nhóm PHPVietnam cần tuân thủ nhằm mục đích tạo sự thống nhất chung [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=55&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p><strong>Chuẩn phong cách viết mã PHPVietnam  là một nỗ lực thống nhất phong cách viết mã cho các lập trình viên  thuộc nhóm PHPVietnam. Nó là một ràng buộc về các quy tắc viết mã PHP mà  các lập trình viên của nhóm PHPVietnam cần tuân thủ nhằm mục đích tạo  sự thống nhất chung giữa các đoạn mã dễ theo dõi, sử dụng lại, phát hiện  lỗi, bảo trì và kế thừa chương trình. Việc tuân thủ quy ước này sẽ giúp  duy trì được khả năng làm việc nhóm và khả năng kế thừa lại của người  đi sau. </strong></p>
<p>Chuẩn phong cách viết mã này được dùng như một tài liệu tham khảo như  các tài liệu kĩ thuật khác.</p>
<p>Các quy tắc của Chuẩn:</p>
<ul>
<li>Nên: không bắt buộc</li>
<li>Phải: bắt buộc</li>
<li>Có thể: giới thiệu giải pháp, khuyến cáo (không bắt buộc)</li>
</ul>
<p>Lịch sử:</p>
<ul>
<li>19/7/2008: Một số quy tắc đặt tên biến đã thay đổi do trải nghiệm từ  việc nghiên cứu các framework Zend, Symfony cũng như kinh nghiệm từ  việc viết PONE framework và áp dụng nó vào các dự án tại WVB. Ngoài ra  chuẩn này cũng được sửa đổi để phù hợp với sự ra mắt của PHP 5.3 trong  tháng 9/2008 tới</li>
<li>6/2/2007: Quy định cách khai báo câu truy vấn SQL trong code PHP.</li>
<li>10/10/2006: Chuẩn hóa cách đặt tên class và id trong CSS.</li>
<li>15/6/2006: Chuẩn hóa cách viết SQL.</li>
<li>28/5/2006: Chuẩn hóa cách khai báo mảng và khai báo lược đồ dữ liệu</li>
<li>14/4/2006: Chuẩn viết mã PHP chuyển về GooglePages. Đây sẽ là nơi  lưu trữ bản PHPVietnam Coding Standard cập nhật nhất.</li>
<li>13/1/2006: Rút chuẩn viết mã PHP khỏi Wikipedia và nó trở thành một  chuẩn riêng của nhóm PHPVietnam</li>
<li>14/9/2004: công bố bản đầu tiên trên Wikipedia tiếng Việt</li>
<li>14/9/2004: Chuẩn viết mã này được đề xuất năm 2004 bởi pcdinh</li>
</ul>
<p>Last updated: 19/7/2008</p>
<h2><a name="TOC-Gi-i-thi-u"></a>Giới thiệu</h2>
<h3><a name="TOC-T-i-sao-c-n-c-quy-c-vi-t-m-"></a>Tại sao cần có quy ước  viết mã</h3>
<p>Các quy ước viết mã có ý nghĩa quan trọng đối với các lập trình viên  vì một số lý do:</p>
<ul>
<li>80% trong tổng số chi phí duy trì một gói phần mềm phát sinh trong  giai đoạn bảo trì.</li>
<li>Hầu như không có bất cứ một phần mềm nào được bảo trì trong suốt cả  thời gian tồn tại của nó bằng chính tác giả đầu tiên của nó.</li>
<li>Các quy ước viết mã nâng cao tính dễ theo dõi của phần mềm, cho phép  các kĩ sư hiểu được các dòng mã mới nhanh hơn và sâu sắc hơn.</li>
<li>Một mã nguồn được đưa ra thị trường như là một sản phẩm cần phải  được đảm bảo là nó được đóng góp chuẩn và gọn ghẽ như bất cứ sản phẩm  nào khác.</li>
</ul>
<p>Để các quy ước này có hiệu lực, tất cả những ai tham gia viết phần  mềm thuộc nhóm PHPVietnam đều <strong>phải</strong> tuân thủ theo các  quy ước viết mã chung.</p>
<h3><a name="TOC-K-th-a-v-ng-g-p"></a>Kế thừa và đóng góp</h3>
<p>Tài liệu này phản ánh các tiêu chuẩn viết mã trong ngôn ngữ lập trình  PHP được tổng hợp từ các tài liệu về Quy ước viết mã trong PEAR, Java  do Sun Microsystems và PHP Team cung cấp. Nhóm PHPVietnam kế thừa và  chọn lọc các điểm phù hợp với nhóm vào thời điểm lựa chọn.</p>
<h2><a name="TOC-T-n-file"></a>Tên file</h2>
<h3><a name="TOC-Ph-n-m-r-ng-c-a-t-p-tin"></a>Phần mở rộng của tập tin</h3>
<p>Các tệp tin chứa các đoạn mã PHP đều phải có phần mở rộng là <tt>.php</tt>.  Cách cách đặt đuôi file như .inc ở Drupal là bad practice và cần tuyệt  đối loại bỏ trừ khi có các quy định khác trong framework và phải có cơ  chế bảo vệ tệp tin đáng tin cậy. Các tệp tin chứa định nghĩa lớp khi  không dùng thư viện lớp hay framework nào nên có phần mở rộng là <tt>.class.php,</tt> các tệp tin chứa định nghĩa các interface thuần túy nên có phần mở rộng  là <tt>.interface.php </tt>và các tệp tin chứa mã templates thì nên có  phần mở rộng là <tt>.tpl.php</tt></p>
<h3><a name="TOC-C-ch-t-t-n-file"></a>Cách đặt tên file</h3>
<h4><a name="TOC-C-c-t-n-file-th-ng-g-p-trong-c-c-ng"></a><em>Các tên  file thường gặp trong các ứng dụng PHP</em></h4>
<p>Đối với tất cả các tên file, chuẩn này khuyến cáo chỉ dùng các kí tự  alphanumeric characters, dấu gạch dưới và dấu gạch ngang (“-”). Tuyệt  đối không dùng các khoảng trắng. Tên file tốt hơn nên dùng tiếng Anh và  có ý nghĩa gợi tả nội dung file.</p>
<p><em>Cách đặt tên file chứa lớp</em></p>
<p>Tất cả các tập tin chứa các lớp (bao gồm cả các thư viện của  framework) nên có tên file trùng lớp tên lớp. Khi làm việc với một lớp  độc lập khỏi framework hoặc định nghĩa một file lớp riêng lẻ giữa các  file scripting thì tên file tên có phần mở rộng là <tt>.class.php</tt>.  Tuy nhiên, không nên áp dụng cách này khi viết một framework hoặc tệp  tin chứa lớp đó đã nằm trong thư mục có tên là classes. Khi lập trình  hay cố gắng đưa phần thư viện vào thư mục hay namespace riêng nhằm tránh  gây hiểu nhầm và dễ quản lý hơn</p>
<p>Ví dụ: <tt>Action.class.php</tt></p>
<h2><a name="TOC-C-ch-t-ch-c-file"></a>Cách tổ chức file</h2>
<h3><a name="TOC-B-c-c-n-i-dung-trong-file"></a>Bố cục nội dung trong  file</h3>
<h4><a name="TOC-D-li-u-m-t-u-file-v-l-p"></a>Dữ liệu mô tả đầu file và  lớp</h4>
<p>Tất cả các file mã nguồn nên có một khối dữ liệu mô tả ở cấp trang ở  ngày đầu file và một khối dữ liệu mô tả ở cấp lớp nằm ngày trên phần mã  cho mỗi lớp. Phần này nên viết bằng tiếng Anh. Ví dụ:</p>
<pre> &lt;?php
 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 /**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * PHP versions 4 and 5
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category CategoryName
 * @package PackageName
 * @author Original Author &lt;author@example.com&gt;
 * @author Another Author &lt;another@example.com&gt;
 * @copyright 1997-2004 The PHP Group
 * @license http://www.php.net/license/3_0.txt PHP License
 * @version CVS: $Id:$
 * @link http://pear.php.net/package/PackageName
 * @see NetOther, Net_Sample::Net_Sample()
 * @since File available since Release 1.2.0
 * @deprecated File deprecated in Release 2.0.0
 */
 // Place includes, constant defines and $_GLOBAL settings here.&lt;br /&gt;
 /**
 * Short description for class
 *
 * Long description for class (if any)...
 *
 * @author Original Author &lt;author@example.com&gt;
 * @author Another Author &lt;another@example.com&gt;
 * @copyright 1997-2004 The PHP Group
 * @license http://www.php.net/license/3_0.txt PHP License
 * @version Release: @package_version@
 * @link http://pear.php.net/package/PackageName
 * @see NetOther, Net_Sample::Net_Sample()
 * @since Class available since Release 1.2.0
 * @deprecated Class deprecated in Release 2.0.0
 */
 class MyClassName
 {
 }
 ?&gt;
</pre>
<h4><a name="TOC-Th-nh-d-u-m-PHP"></a>Thẻ đánh dấu mã PHP</h4>
<p>Mã PHP nên được đánh dấu bằng cặp thẻ <tt>&lt;?php</tt> và <tt>?&gt;</tt>.  Chỉ được loại bỏ thẻ kết thúc ?&gt; trong trường hợp không kiểm soát  được vấn đề header_sent. Các hình thức khác như &lt;?  và ?&gt; hay &lt;% và %&gt;<br />
không được khuyến cáo.</p>
<h4><a name="TOC-C-ch-th-t-l-i-d-ng-v-chi-u-d-i-c-a-"></a>Cách thụt lùi  dòng và chiều dài của dòng</h4>
<p>Phần mã nằm trong khối điều khiển nên được thụt lùi 4 khoảng trắng so  với hàng chứa điều khiển vòng gần nhất. Ví dụ:</p>
<p>&lt;?php<br />
if ($index &gt; 0)<br />
{<br />
(4 space)$index =  $index + 1;<br />
(4 space)if ($index &gt; 72)<br />
(4 space){<br />
(4 space)(4 space)Validator::parse($index);<br />
(4 space)(4 space)break;<br />
}<br />
}<br />
?&gt;</p>
<h3><a name="TOC-Ch-th-ch-m-"></a>Chú thích mã</h3>
<p>Nên chú thích mã như thế nào. Áp dụng các thẻ meta (param) như thế  nào. Áp dụng <tt>/*</tt> và <tt>*/</tt> cùng với <tt>//</tt> như thế  nào. (<em>to be updated</em>)</p>
<h3><a name="TOC-nh-d-ng-file"></a>Định dạng file</h3>
<p>Phần này tuân thủ các nguyên tắc sau:</p>
<ul>
<li>Định dạng file là dạng văn bản UTF-8</li>
<li>Sử dụng mã hóa kí tự UTF-8</li>
<li>Được định dạng cho Unix</li>
<li>“Định dạng theo Unix” nghĩa là:
<ul>
<li>Các dòng phải được kết thúc chỉ với một kí hiệu line feed (LF). Các  kí hiệu bắt đầu hàng mới như vậy được được biểu diễn như là các kí hiệu  hệ thập phân (10), hệ bát phân (012) và hệ thập lục phân (0A) thông  thường. Không dùng các kí hiệu về đầu hàng carriage returns (CR) như các  máy Macintosh hay kết hợp carriage return/line feed (CR/LF) như các máy  Windows.</li>
<li>Nên có một line feed sau thẻ PHP đóng (<tt>?&gt;</tt>). Điều đó có  nghĩa là khi con trỏ ở tận cùng của file, thì nó nên có một hàng bên  dưới thẻ PHP đóng.</li>
</ul>
</li>
</ul>
<h2><a name="TOC-Quy-c-t-t-n-bi-n-h-ng-h-m-ph-ng-th-"></a>Quy ước đặt  tên biến, hằng, hàm, phương thức, lớp</h2>
<h2><a name="TOC-Khai-b-o"></a>Khai báo</h2>
<h3><a name="TOC-Khai-b-o-v-kh-i-t-o-bi-n"></a>Khai báo và khởi tạo biến</h3>
<h4><a name="TOC-Bi-n-to-n-c-c"></a>Biến toàn cục</h4>
<p>Chuẩn này khuyến cáo hạn chế tối đa dùng các biến toàn cục với từ  khóa sửa đổi global.</p>
<h4><a name="TOC-Bi-n-th-ng-th-ng"></a>Biến thông thường</h4>
<p>Tên biến nên bắt đầu bằng chữ viết thường. Tên biến nên là danh từ có  ý nghĩa mô tả nó chứa cái gì hay nó làm nhiệm vụ gì. Nếu biến gồm có  nhiều từ thì ghép các từ đó lại: từ đầu tiên viết thường, từ thứ hai và  thứ ba viết hoa chữ cái đầu tiên. Tuyệt nhiên không dùng dấu gạch dưới  trong tên biến. Ví dụ:</p>
<ul>
<li><tt>somePrivateProperty</tt></li>
<li><tt>counter</tt></li>
<li><tt>counter_for_this</tt> (Không khuyến cáo)</li>
</ul>
<p>Các cái tên như “foo” và “tmp” nên tránh dùng vì nó không mô tả cái  gì cả. Tên biến không nên chứa các con số. Các con số nên được mô tả  bằng các chữ cái trừ khi có lý do thiết thực để không làm như vậy.</p>
<p>Tuyệt đối không dùng Hungarian notation để đặt tên cho biến vì nó là  dấu hiệu của abstraction leaks, code smells và overloaded code blocks.  Ví dụ cách đặt dưới đây là không khuyến cáo.</p>
<ul>
<li>$m_myVariable</li>
<li>$arrColors</li>
<li>$iCounter</li>
</ul>
<p>Cách đặt mong muốn:</p>
<ul>
<li><tt>$valueOne</tt></li>
<li><tt>$_colors</tt></li>
<li><tt>$_processesLevelTwo</tt></li>
</ul>
<h3><a name="TOC-T-n-l-p-khai-b-o-l-p-v-giao-di-n"></a>Tên lớp, khai báo  lớp và giao diện</h3>
<h4><a name="TOC-Ch-th-ch-cho-l-p"></a><strong><em>Chú thích cho lớp</em></strong></h4>
<p>Trước khi khai báo lớp, lập trình viên nên thực hiện chú thích cho  lớp. Chú thích nên viết bằng tiếng Anh để các công cụ sinh tài liệu tự  động có thể đọc được chuẩn theo nguyên gốc. PHP6 có thể cải tiến hỗ trợ  Unicode cho nên quy tắc sử dụng ngôn ngữ có thể thay đổi. Dù vậy, nếu mã  nguồn là mở cho cộng đồng thế giới, tiếng Anh vẫn là sự lựa chọn tốt  hơn.</p>
<p>Ví dụ:</p>
<pre> /**
 * A class that allows services to be globally registered, so that they
 * can be accessed by any class that needs them. Also allows Mock Objects
 * to be easily used as replacements for classes during testing.
 *
 * @package Max
 * @author Luis Correa d'Almeida &lt;luis@m3.net&gt;
 * @author Andrew Hill &lt;andrew@m3.net&gt;
 */
</pre>
<p>hoặc</p>
<pre> /**
 * Utility class for common functions.
 *
 * @package Zend
 * @copyright Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
 * @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
 */
</pre>
<p>Các thẻ @xxx nên đặt theo các thẻ chuẩn của PHPDocumentor.</p>
<h4><a name="TOC-t-t-n-l-p"></a><strong><em>Đặt tên lớp</em></strong></h4>
<p>Tên lớp nên là một danh từ mô tả, xác định rõ lớp đó nó là cái gì.  Tránh dùng từ viết tắt. Tên lớp nên bắt đầu bằng từ <strong>viết hoa</strong>.  Khi áp dụng kĩ thuật hướng đối tượng để viết mã thì cần chú ý tên lớp  cũng nên phản ảnh cây phân cấp lớp, mỗi bậc của cây phân cấp được cách  nhau thông qua một dấu gạch dưới. Điều này là để tránh xung đột tên lớp  giữa các gói (lớp đặt trong các thư mục) vì với PHP 4.x và PHP 5.x, tên  một lớp phải là duy nhất trong toàn bộ mẫu của PHP Parser. Nếu không làm  theo nguyên tắc này, bạn sẽ gặp lỗi Fatal error: Cannot redeclare class post in  C:\phpvietnam\www\MyCart\app\plugins\blog\models\post.php on line 13.  Quy tắc này được đặt ra do namespace chưa được thực hiện trong PHP4,  PHP5. PHP6 có thể sẽ làm thay đổi điều này.Ví dụ:</p>
<ul>
<li>Log</li>
<li>Net_Finger</li>
<li>HTML_Upload_Error</li>
</ul>
<p>Trong một số trường hợp, người lập trình có thể đặt lớp Email nằm  trong file Email.class.php và file này nằm trong thư mục Validator, khi  đó lớp Email sẽ có tên là Validator_Email.</p>
<h3><a name="TOC-nh-ngh-a-h-m-v-g-i-h-m"></a>Định nghĩa hàm và gọi hàm</h3>
<h4><a name="TOC-T-n-g-i-c-a-h-m"></a><strong><em>Tên gọi của hàm</em></strong></h4>
<p>Trong PHP, khái niệm hàm và phương thức thay thế được cho nhau. Chúng  ta gọi khái niệm hàm cho lập trình thủ tục và dùng khái niệm phương  thức khi lập trình hướng đối tượng.</p>
<p>Hàm và phương thức đều chỉ mặt hoạt động, thao tác, vì vậy tên chúng  nên gọi tả hành động tương ứng.</p>
<p>Hàm và phương thức nên được đặt tên theo quy tắc “studly caps” (còn  được gọi là “bumpy case” hay “camel caps”). Chữ cái đầu tiên của tên  (sau tiền tố) nên viết thường, và mỗi một chữ bắt đầu một từ mới sẽ viết  hoa. Ví dụ:</p>
<ul>
<li><tt>connect()</tt></li>
<li><tt>getData()</tt></li>
<li><tt>buildSomeWidget()</tt></li>
</ul>
<p>Các thành viên lớp dạng <tt>private</tt> (nghĩa là thành viên đó được  tạo ra chỉ nhằm để tiếp xúc với bên trong lớp mà chúng được khai báo mà  thôi) thì được bắt đầu bằng một dấu gạch dưới. Ví dụ:</p>
<ul>
<li><tt>_sort()</tt></li>
<li><tt>_initTree()</tt></li>
<li><tt>$this-&gt;_status</tt></li>
</ul>
<p>Quy tắc tương tương tự cũng sẽ áp dụng cho các thành viên lớp có điều  khiển truy cập là protected. Ví dụ:</p>
<ul>
<li><tt>protected $_somevar</tt></li>
<li><tt>protected function _initTree()</tt></li>
</ul>
<p>Lý do để áp dụng cách làm này là PHP sẽ không bao giờ hỗ trợ method  overloading cho nên cần tiết kiệm các tên gọi trong khai báo lớp. Vì  biến hay method protected và private sẽ chiếm dụng số lượng tên mà  public API có thể có cho nên cách tốt nhất là dùng _ để giảm khả năng  này.</p>
<h4><a name="TOC-nh-ngh-a-h-m"></a><strong><em>Định nghĩa hàm</em></strong></h4>
<p>PHP là một ngôn ngữ curly braces cho nên sự có mặt của { và } là hết  sức quan trọng trong việc xác định các block mã. Theo chuẩn này, lập  trình viên được khuyến cáo đặt { và } trên các hàng riêng rẽ. Nên  tránh đặt dấu ngoặc nhòn mở {  trên cùng dòng với khai báo method hay  class hay các language contruct khác như if, while … bởi vì khi khai báo  này quá dài, nó sẽ làm hạn chế lợi ích của việc sử dụng tính năng curly  matching có trong các IDE hiện đại.</p>
<pre> &lt;?php
 function fooFunction($arg1, $arg2 = '')
 {
     if (condition)
     {
         statement;
     }
     return $val;
 }
 ?&gt;
</pre>
<p>Các đối số có giá trị mặc định được đặt ở cuối danh sách đối số. Cố  gắng trả lại một giá trị có nghĩa từ một hàm, nếu có giá trị thích hợp.  Ví dụ:</p>
<pre> &lt;?php
 function connect(&amp;$dsn, $persistent = false)
 {
     if (is_array($dsn))
     {
         $dsninfo = &amp;$dsn;
     }
     else
     {
         $dsninfo = DB::parseDSN($dsn);
     }

     if (!$dsninfo || !$dsninfo['phptype'])
     {
         return $this-&gt;raiseError();
     }
     return true;
 }
 ?&gt;
</pre>
<h4><a name="TOC-L-i-g-i-h-m-ph-ng-th-c"></a><strong><em>Lời gọi hàm,  phương thức</em></strong></h4>
<p>Khi gọi hàm, không nên dùng dấu cách giữa tên hàm, ngoặc đơn mở, và  tham số thứ nhất; nên dùng một dấu cách giữa dấu phẩy và tham số, và  không dùng dấu cách giữa tham số cuối cùng, dấu ngoặc đơn đóng và dấu  chấm phẩy. Ví dụ:</p>
<pre> &lt;?php
 $var = getMessage($bar, $baz, $quux);
 ?&gt;
</pre>
<p>Như thấy ở trên, nên để một dấu cách ở hai bên dấu bằng dùng đến gán  giá trị trả lại của một hàm vào một biến. Trong trường hợp viết nhiều  phép gán có liên quan thì có thể dùng thêm nhiều dấu cách để làm tăng  thêm tính dễ đọc:</p>
<pre> &lt;?php
 $short = foo($bar);
 $longVariable = foo($baz);
 ?&gt;
</pre>
<p>Lời gọi phương thức tuân theo những quy tắc cũng tương tự như lời gọi  hàm. Phương thức là thuật ngữ dùng để chỉ hàm khai báo bên trong các  lớp. Chuẩn này khuyến cáo không dùng dấu cách để tách phần dấu ngoặc đơn  với đối số đặt ở giữa cặp ngoặc đơn này. Sau đây là ví dụ để bạn phân  biệt cách gọi nào là hợp chuẩn:</p>
<pre> // Khuyến cáo theo Chuẩn này
 $object-&gt;method($a);
 $array[10] = 'foo';

 // Không khuyến cáo
 $object-&gt;method( $a );
 $array[ 10 ] = 'foo';
</pre>
<h3><a name="TOC-Khai-b-o-h-ng"></a>Khai báo hằng</h3>
<p>Các hằng phải luôn viết tất cả dưới dạng chữ hoa, dùng gạch dưới để  tách các từ. Phần tên hằng làm tiền tố thì dùng tên chữ hoa của lớp hoặc  gói mà chúng ta định dùng chúng trong đó. Ví dụ, các hằng dùng trong  gói <tt>DB::</tt> tất cả đều bắt đầu bằng <tt>DB_</tt>.</p>
<p>Xin chú ý: Các hằng true, false và null là những trường hợp đặc biệt  không bị chi phối bởi quy ước viết hoa này cho nên luôn viết chúng ra  dưới dạng chữ thường.</p>
<h2><a name="TOC-t-t-n-cho-m-ng-v-ch-m-c-m-ng"></a>Đặt tên cho mảng và  chỉ mục mảng</h2>
<p>Trong PHP, mảng là một cấu trúc ngôn ngữ. Việc đặt tên cho mảng tuân  thủ nguyên tắc đặt tên cho biến. Cặp ngoặc tròn dùng cho mảng phải đặt  sát cạnh khai báo array. Chỉ mục mảng đầu tiên đặt sát với ngoặc tròn mở  đầu. Các chỉ mục tiếp theo đặt cách một khoảng trắng so với dấu phẩy.  Chỉ mục cuối cùng đặt nằm sát với dấu ngoặc tròn đóng mảng. Chỉ mục mảng  được đặt tên theo danh từ hoặc dùng số nếu là mảng kết hợp. Chỉ mục  mảng phải đặt viết thường. Nếu chỉ mục mảng gồm nhiều từ thì dùng dấu  gạch dưới để cách cách từ. Nguyên tắc này phải phù hợp với nguyên tắc  đặt tên cho bảng và tên cột. Khi khai báo mảng gồm cả cặp khóa và giá  trị thì toán tử liên kết (=&gt;) phải cách dấu trích đơn kết thúc khóa  một khoảng trắng và cách dấu trích đơn mở đầu giá trị (nếu giá trị là  chuỗi) hoặc giá trị đó (nếu giá trị là số hay hằng hoặc biến) một khoảng  trắng.</p>
<pre> // Khuyến cáo theo Chuẩn này
 $anArrayOfData = array();
 $listOfNews = array('one', 'two', 'three');
 $listOfNews = array('one_table', 'two_apples', 'three_clocks');
 $listOfNews = array('one_table' =&gt; $table, 1 =&gt; DB_ERROR, 'three_clocks' =&gt; 2);

 // Không khuyến cáo
 $anArryOfData = array ();
 $listOfNews = array( 'one', 'two', 'three' );
 $anArryOfData = array( );
 $listOfNews = array('one', 'two','three');
</pre>
<h2><a name="TOC-C-u-l-nh-i-u-khi-n"></a>Câu lệnh điều khiển</h2>
<h3><a name="TOC-C-u-l-nh-n-gi-n"></a>Câu lệnh đơn giản</h3>
<h3><a name="TOC-L-nh-t-h-p"></a>Lệnh tổ hợp</h3>
<h3><a name="TOC-L-nh-return"></a>Lênh <tt>return</tt></h3>
<h3><a name="TOC-L-nh-i-u-ki-n-v-c-u-tr-c-i-u-khi-n"></a>Lệnh điều kiện  và cấu trúc điều khiển</h3>
<p>Cấu trúc điều khiển trong PHP bao gồm có if, for, while, switch,  foreach.</p>
<p>Các cấu trúc điều khiển nên dùng một khoảng trống giữa từ khóa điều  khiển và ngoặc đơn mở, để phân biệt chúng với lời gọi hàm.</p>
<p>Nên dùng các ngoặc nhọn ngay cả trong các tình huống chúng chỉ được  xem là các tùy chọn kĩ thuật. Các dấu ngoặc nhọn sẽ làm cho mã dễ đọc  hơn và làm giảm khả năng xảy ra các lỗi logic khi viết thêm các dòng mã  mới.</p>
<h4><a name="TOC-L-nh-if-else"></a>Lệnh <tt>if</tt>/<tt>else</tt></h4>
<p>Dưới đây là cách dùng hàm <tt>if</tt>/<tt>else</tt> tuân thủ quy ước  chung. Chú ý: luôn dùng các ngoặc nhọn { } để bao các câu lệnh của if chứ không được  bỏ. Luôn có dấu cách giữa 2 đầu của dấu =, ==, ===, &gt;, &lt; khi so  sánh hay gán</p>
<p>Chuẩn này cũng khuyến cáo không dùng else nếu bạn đã trả lại giá trị  với return hoặc  đã dùng break;  bên trong khối.</p>
<pre> &lt;?php
 if ((condition1) || (condition2))
 {
     action1();
 }
 elseif ((condition3) &amp;&amp; (condition4))
 {
     action2();
 }
 else
 {
     defaultAction();
 }
 ?&gt;
</pre>
<p>Như thế này là tồi.</p>
<pre> &lt;?php
 if ((condition1) || (condition2))
 action1;
 ?&gt;
</pre>
<h4><a name="TOC-L-nh-for"></a>Lệnh <tt>for</tt></h4>
<pre> for ($i = 0; $i &lt; 100; $i++)
 {
     if (!$x)
     {
         break;
     }

     if ($x &lt; 10)
     {
         statement;
	 statement;
         break;
     }
     ......
 }
</pre>
<p>Xin lưu ý, việc dùng các biến ngắn như trên không được khuyến cáo.  Các biến dùng trong for nên có các cái tên gợi tả ý nghĩa như $counter  hay $index</p>
<h4><a name="TOC-L-nh-while"></a>Lệnh <tt>while</tt></h4>
<pre>while ($data = getData($requestId))
{   
    $menu   .= '&lt;option value="'.$data['id'].'"';   
    $menu   .= ($data[ 'id'] == $_GET['id'] ? ' selected&gt;' :'&gt;');   
    $menu   .= $data[ 'name'].'&lt;/option&gt;';
}</pre>
<h4><a name="TOC-L-nh-do-while"></a>Lệnh <tt>do</tt>-<tt>while</tt></h4>
<pre>do
{   
    echo 'It is PHPVietnam compliant';
} while ($error == 0);
</pre>
<h4><a name="TOC-L-nh-switch"></a>Lệnh <tt>switch</tt></h4>
<p>Chú ý không dùng return trong từng case</p>
<pre>&lt;?php
switch (condition)
{
    case 1:
        action1();
        break;

    case 2:
        action2();
        break;

    default:
        defaultAction();
        break;
}
?&gt;
</pre>
<h4><a name="TOC-L-nh-try-catch"></a>Lệnh <tt>try</tt>-<tt>catch</tt></h4>
<pre> try
 {
     // Get rotation length field and convert to integer.
     $rotLength = Integer::parseInt($rotationLengthField-&gt;getText()-&gt;trim());
     ....
 }
 catch(Exception $ex)
 {
     // Report the exception to the user.
     ErrorHandler::log($ex, "Invalid rotation length: ");
 }
</pre>
<h2><a name="TOC-L-m-vi-c-v-i-SQL-v-c-s-d-li-u-datab"></a>Làm việc với  SQL và cơ sở dữ liệu (database)</h2>
<p>Đặt tên cho bảng</p>
<p>Cách đặt tên bảng và column tuân thủ theo chuẩn ISO-11179, mô tả  trong cuốn book “SQL Programming Style” của Joe Celko. Tên bảng là danh  từ <strong>số nhiều</strong> <strong>viết thường</strong> bằng tiếng  Anh. Tên bảng có thể đặt bằng danh từ không đếm được. Nếu tên bảng gồm  nhiều từ kết hợp thì dùng <strong>dấu gạch dưới</strong> để tách các từ.  Tuy nhiên, rất hạn chế dùng dấu cách để tách các từ như thế này, tên  bảng nên là một từ đơn.</p>
<p>Ví dụ:</p>
<ol>
<li>news</li>
<li>employees, approved_patents;</li>
</ol>
<h4><a name="TOC-t-t-n-cho-c-t"></a>Đặt tên cho cột</h4>
<p>Tên cột là danh từ <strong>số ít</strong> hoặc tính từ, <strong>viết  thường</strong>. Nếu tên cột gồm nhiều từ kết hợp thì dùng <strong>dấu  gạch</strong> dưới để tách các từ. Điều này phù hợp với nguyên tắc đặt  tên cho các chỉ mục mảng.</p>
<h4><a name="TOC-Vi-t-c-u-l-nh-SQL"></a>Viết câu lệnh SQL</h4>
<p>Các câu lệnh SQL sẽ được viết hoa những từ, mệnh đề là từ khóa được  bảo lưu. Tên bảng, tên cột, alias sẽ được viết thường theo quy tắc trên.</p>
<p>Ví dụ:</p>
<pre>SELECT
 SUM(sales.units_sold)
FROM
 sales, customer, time, store, product
WHERE
 sales.customer_id = customer.customer_id AND
 sales.date_id = time.date_id AND
 sales.store_id = store.store_id AND
 sales.product_id = product.product_id AND
 time.year_id = 2006 AND
 product.category_id = "tv"
GROUP BY
 product.brand, store.country_iso_id</pre>
<p>Khi khai báo các câu SQL trong mã  PHP, không khuyến cáo dùng dấu (+) để kết hợp các câu truy vấn nằm trên  nhiều hàng. PHP là một ngôn ngữ multiline, không giống như Java, và việc  làm như vậy sẽ khiến tốc độ xử lý chậm và khó đọc. Ngoài ra nó còn làm  giảm các rủi ro gặp lỗi khi làm việc trên SQL Server do SQL Server có  thể báo lỗi câu SQL là không hợp lệ khi trước và sau câu truy vấn không  có dấu cách.</p>
<p>Ví dụ:</p>
<p>Không khuyến cáo</p>
<pre>$sql = "SELECT" .
" first_name," .
" last_name" .
" FROM employee" .
" WHERE city = 'London'";</pre>
<p>Khuyến cáo</p>
<pre>$sql = "
 SELECT
 first_name,
 last_name
 FROM employee
 WHERE city = 'London'
";</pre>
<h2><a name="TOC-L-m-vi-c-v-i-CSS"></a>Làm việc với CSS</h2>
<p>Cách đặt tên cho class và id trong CSS</p>
<p>Khi tên class và id gồm 2 từ trở lên thì dùng dấu gạch ngang (-) để  ngăn cách chúng. Tránh dùng dấu gạch dưới (_):  http://developer.mozilla.org/en/docs/Underscores_in_class_and_ID_Names.  Một cách khác là dùng camelCase để đặt tên.</p>
<p>Ví dụ:</p>
<ul>
<li>div#controlPanel</li>
<li>p.introductionText</li>
</ul>
<p>Tên lớp và id nên viết toàn bộ là chữ thường nếu như không dùng  camelCase. Chú ý là trong CSS, tên của class và id là phân biệt chữ hoa  chữ thường.</p>
<h2><a name="TOC-Kinh-nghi-m-l-p-tr-nh"></a>Kinh nghiệm lập trình</h2>
<p>Phần này nói về các kinh nghiệm sử dụng các từ khóa điều khiển truy  cập.</p>
<h3><a name="TOC-C-ch-d-ng-include_once-v-require_on"></a>Cách dùng  include_once() và require_once()</h3>
<p><tt>include_once()</tt> được khuyến cáo dùng khi bạn sử dụng một thư  viện bên ngoài nhưng sự tồn tại của nó không gây ảnh hưởng dẫn đến sự  gián đoạn của chương trình. Những tài nguyên gây gián đoạn chương trình  có thể kể đến như các bộ lọc bảo mật, kết nối cơ sở dữ liệu, lời gọi  dịch vụ web… Khi đó bạn nên dùng <tt>require_once</tt> vì khi nó không  load được tài nguyên sẽ dẫn đến lỗi fatal error làm ngừng ứng dụng.</p>
<p>Cú pháp của require_once và include_once khai báo như sau</p>
<ul>
<li><tt>require_once 'fileName.class.php';</tt></li>
<li><tt>include_once 'fileName.class.php';</tt></li>
</ul>
<p>Không dùng () để bao tên file.</p>
<h3><a name="TOC-Kinh-nghi-m-t-i-u-h-a-t-c-x-l-chu-i"></a>Kinh nghiệm  tối ưu hóa tốc độ xử lý chuỗi</h3>
<h3><a name="TOC-Tr-nh-v-h-n-ch-n-m-c-t-i-a-d-ng-c-c"></a>Tránh và hạn  chế đến mức tối đa dùng các biến toàn cục</h3>
<h3><a name="TOC-D-ng-single-quotes-v-double-quotes"></a>Dùng single  quotes và double quotes</h3>
<p>Nên luôn dùng single quote (‘) để bao các chuỗi không chứa biến. Nếu  chuỗi có chứa biến thì dùng double quote (“).</p>
<p>( Tham Khảo và sư tầm )</p>
</div>
<br />Filed under: <a href='http://kokono0204.wordpress.com/category/php/'>php</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=55&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2010/05/23/php-vietnam-coding-converition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>
	</item>
		<item>
		<title>Tracert</title>
		<link>http://kokono0204.wordpress.com/2009/11/04/tracert/</link>
		<comments>http://kokono0204.wordpress.com/2009/11/04/tracert/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 17:35:25 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[Network]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/2009/11/04/tracert/</guid>
		<description><![CDATA[Traceroute là gì? Traceroute là một chương trình cho phép bạn xác định được đường đi của các gói packets từ máy bạn đến hệ thống đích trên mạng Internet. Một ví dụ về Traceroute! Traceroute có thể làm được gì? Bạn hãy xem ví dụ sau sẽ rõ! Code: C:\windows&#62;tracert 203.94.12.54 Tracing route to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=52&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Traceroute là gì?</strong></p>
<p>Traceroute là một chương trình cho phép bạn xác định được đường đi của các gói packets từ máy bạn đến hệ thống đích trên mạng Internet.</p>
<p>Một ví dụ về Traceroute!</p>
<p>Traceroute có thể làm được gì? Bạn hãy xem ví dụ sau sẽ rõ!</p>
<div>
<div>Code:</div>
<pre>C:\windows&gt;tracert 203.94.12.54
Tracing route to 203.94.12.54 over a maximum of 30 hops
 1 abc.netzero.com (232.61.41.251) 2 ms 1 ms 1 ms
2 xyz.Netzero.com (232.61.41.0) 5 ms 5 ms 5 ms
3 232.61.41.10 (232.61.41.251) 9 ms 11 ms 13 ms
4 we21.spectranet.com (196.01.83.12) 535 ms 549 ms 513 ms
5 isp.net.ny (196.23.0.0) 562 ms 596 ms 600 ms
6 196.23.0.25 (196.23.0.25) 1195 ms1204 ms
7 backbone.isp.ny (198.87.12.11) 1208 ms1216 ms1233 ms
8 asianet.com (202.12.32.10) 1210 ms1239 ms1211 ms
9 south.asinet.com (202.10.10.10) 1069 ms1087 ms1122 ms
10 backbone.vsnl.net.in (203.98.46.01) 1064 ms1109 ms1061 ms
11 newdelhi-01.backbone.vsnl.net.in (203.102.46.01) 1185 ms 1146 ms 1203 ms
12 newdelhi-00.backbone.vsnl.net.in (203.102.46.02) ms1159 ms1073 ms
13 mtnl.net.in (203.194.56.00) 1052 ms 642 ms 658 ms</pre>
</div>
<p>Tôi cần biết đường đi từ máy tôi đến một host trên mạng Internet có địa chỉ ip là 203.94.12.54. Tôi cần phải tracert đến nó! Như bạn thấy ở trên, các gói packets từ máy tôi muốn đến được 203.94.12.54 phải đi qua 13 hops(mắt xích) trên mạng. Đây là đưòng đi của các gói packets:</p>
<p>Netzero(ISP đã gởi dữ liệu đi) -&gt; Spectranet (một nhà công cấp mạng xương sống &#8211; Backbone Provider) -&gt; New York ISP -&gt; New York Backbone -&gt; Asia -&gt; South Asia -&gt; India Backbone -&gt; New Delhi Backbone -&gt; một router khác trong New Delhi Backbone -&gt; New Delhi ISP</p>
<p>Như vậy, host có địa chỉ ip 203.94.12.54 nằm ở New Delhi, India, South Asia! Bạn cũng có thể telnet đến 203.94.12.54 trên cổng 13(datetime) để xác định giờ GMT qua đó bạn có thể biết được vị trí của host này(yêu cầu là host 203.94.12.54 phải chạy daemon datetime và được định cấu hình đúng về thời gian)!</p>
<p><strong>Traceroute hoạt động như thế nào</strong>?</p>
<p>Trước hết, bạn cần biết về ICMP, TTL và cách làm việc của các routers(bộ định tuyến)!</p>
<p><strong>Những kiến thức cơ bản</strong><br />
ICMP &#8211; Internet Control Message Protocol. ICMP được dùng để thông báo các lỗi xảy ra trong quá trình truyền đi của các gói dữ liệu trên mạng. ICMP thuộc tầng vận huyển &#8211; Transpoort Layer!</p>
<p>Tất cả các ICMP messages đều được chuyển đi cùng với các IP datagrams. Mỗi ICMP message được gói trong IP datagram sẽ có dạng như sau:</p>
<p>+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
| IP Header(20 bytes) | ICMP message (32 bytes) |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+</p>
<p>Sau đây là cấu trúc của một IMCP message: (tham khảo RFC792 để biết thêm!)</p>
<p>0 7 8 15 16 31<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| Type (0 or <img src='http://s0.wp.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> | Code (0) | 16-bit Checksum |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| Indentifier | sequence number |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| |<br />
| Optional Data (nội dung tùy thuộc vào Type và Code) |<br />
| |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</p>
<p>* trường type có 15 giá trị khác nhau, tùy thuộc vào từng loại ICMP error message cụ thể. Ví dụ type=3 để chỉ định cho thông báo lỗi &#8220;Không đến được đích&#8221; &#8211; &#8220;Destination unreachable&#8221; error message!<br />
* trường code = sub-error dùng để xác định chính xác lỗi đã xảy ra. Ví dụ, type=3 và code=0 nghĩa là &#8220;Network Unreachable&#8221;(không đến được mạng); nếu type=3, code=1 nghĩa là &#8220;Host Unreachable&#8221;(không đến được host)&#8230;</p>
<p>TTL &#8211; Time to Live. TTL là một trường 8 bit trong IP header(bạn hãy xem lại cấu trúc của IP header!). TTL là thời gian gói dữ liệu tồn tại trên mạng trước khi nó bị bỏ qua. Người gởi dữ liệu đi sẽ xác định một giá trị TTL trước, thường là từ 32 -&gt; 64. Giá trị này sẽ được giảm đi một khi một khi được chuyển qua một bộ định tuyến trên mạng. Khi giá trị này bằng 0, datagram này sẽ bị bỏ qua và giao thức ICMP sẽ báo lỗi về cho người gởi. Điều này sẽ tránh cho datagram này đi vào một vòng lặp vô tận qua các bộ định tuyến.</p>
<p>Mỗi bộ định tuyến khi nhận được IP datagram sẽ giảm giá trị TTL của datagram này đi một. Hầu hết các bộ định tuyến đều không giữ lại datagram này trong thời gian quá 1 giây trước khi chuyển datagram này đi. Nên giá trị TTL có thể coi bằng hop(counter) = số bộ định tuyến mà datagram này vừa vượt qua.</p>
<p>Khi bộ định tuyến nhận được một datagram có trường TTL bằng 0 hoặc 1, nó sẽ không chuyển datagram này đi tiếp. Thay vào đó, nó sẽ bỏ qua datagram này và gởi một ICMP message &#8220;Time Exceeded&#8221;(quá thời gian) trở lại cho người đã gởi datagram này! Vì ICMP message mà bộ định tuyến gởi trở lại cho người gởi có địa chỉ nguồn &#8211; source address là địa chỉ ip của bộ định tuyến này nên người gởi có thể biết được địa chỉ ip của router này!</p>
<p><strong>Cách làm việc của traceroute!</strong></p>
<p>Traceroute gởi một IP datagram có TTL=1 đến hệ thống đích. Router đầu tiên nhận được datagram này sẽ giảm giá trị TTL đi một -&gt; TTL=0 và router này sẽ bỏ qua datagram này(không gởi nó đi tiếp!) và gởi một ICMP error message với địa chỉ ip nguồn là địa chỉ của nó đến máy bạn. Như vậy router có thể xác định địa chỉ ip của router thứ nhất! Sau đó, traceroute sẽ gởi một datagram mới đi với giá trị TTL=2(1+1=2) đến hệ thống đích. Router đầu tiên sẽ giảm giá trị của TTL đi một -&gt; TTL=1(2-1=1) và chuyển datagram này sang router thứ 2. Router thứ 2 nhận được datagram có TTL=1 sẽ giảm TTL=0. Rounter 2 nhận thấy TTL=0 nên nó sẽ không chuyển datagram này đi tiếp. Router 2 sẽ gởi trở lại máy bạn một ICMP error message với địa chỉ ip nguồn là địa chỉ ip của nó(router 2). Như vậy trình traceroute trên máy bạn sẽ biết được router thứ 2 mà datagram đã đi qua. Traceroute sẽ tiếp tục gởi một datagram khác có TTL=3(2+1=3) đi và lặp lại quá trình trên cho đến khi datagram đến được hệ thống đích!</p>
<p>Nếu bây giờ IP datagram đã đến được đích, TTL=1. Host đích sẽ bỏ qua datagram này và nó cũng sẽ không gởi &#8220;Time Exceeded&#8221; ICMP error message. Như vậy thì bạn sẽ không thể nào biết được là mình đã đến đích chưa?! Traceroute dùng một cơ chế khác như sau:</p>
<p>Traceroute gởi UDP datagrams đến host đích trên các cổng UDP có số hiệu lớn(&gt;30000). Sở dĩ nó chọn các cổng có giá trị lớn vì thường không có ứng dụng nào đang lắng nghe ở các cổng này. Khi host đích nhận được UDP datagram này, nó sẽ gởi trả lại một ICMP error message &#8220;Port Unreachable&#8221;(không đến được cổng) cho traceroute. Bây giờ thì traceroute có thể phân biệt được sự khác nhau giữa ICMP error message &#8220;Time Exceeded&#8221; với &#8220;Port Unreachable&#8221; để biết được đã đến được đích hay chưa?!</p>
<p>Ghi chú: ICMP error message &#8220;Time Exceeded&#8221; có type=1 và code=0; ICMP eror message &#8220;Port Unreachable&#8221; có type=3 và code=3</p>
<p>Tổng kết: traceroute gởi UDP datagrams đến host đích với giá trị TTL=1 và được tăng sau mỗi lần để xác định các routers mà datagrams đã đi qua. Mỗi router sẽ gởi trở về một ICMP message &#8220;Time Exceeded&#8221;. Riêng hệ thống đích sẽ gởi trở lại cho traceroute một ICMP message &#8220;Port Unreachable&#8221;. Traceroute dựa vào sự khác biệt này để xác định xem đã đến được đích chưa?!</p>
<p>Ví dụ cuối cùng!</p>
<div>
<div>Code:</div>
<pre>host2 # traceroute xyz.com
traceroute to xyz.com (202.xx.12.34), 30 hops max, 40 byte packets
1 isp.net (202.xy.34.12) 20ms 10ms 10ms
2 xyz.com (202.xx.12.34) 130ms 130ms 130ms</pre>
</div>
<p>Dòng đầu tiên cho biết hostname và địa chỉ IP của hệ thống đích. Dòng này còn cho chúng ta biết thêm giá trị TTL&lt;=30 và kích thước của datagram là 40 bytes(20-bytes IP Header + 8-bytes UDP Header + 12-bytes user data).</p>
<p>Dòng thứ 2 cho biết router đầu tiên nhận được datagram là 202.xy.34.12, giá trị của TTL khi gởi đến router này là 1. Router này sẽ gởi trở lại cho chương trình traceroute một ICMP message error &#8220;Time Exceeded&#8221;. Traceroute sẽ gởi tiếp một datagram đến hệ thống đích.</p>
<p>Dòng thứ 3, xyz.com(202.xx.12.34) nhận được datagram có TTL=1(router thứ nhất đã giảm một trước đó &#8211; TTL=2-1=1). Tuy nhiên, xyz.com không phải là một router, nó sẽ gởi trở lại cho traceroute một ICMP error message &#8220;Port Unreachable&#8221;. Khi nhận được ICMP message này, traceroute sẽ biết được đã đến được hệ thống đích xyz.com và kết thúc nhiệm vụ tại đây.</p>
<p>Trong trường hợp router không trả lời sau 5 giây, traceroute sẽ in ra một dấu sao &#8220;*&#8221;(không biết) và tiếp tục gởi datagram khác đến host đích!</p>
<br />Posted in Network  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=52&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/11/04/tracert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>
	</item>
		<item>
		<title>IDS trong bảo mật hệ thống mạng</title>
		<link>http://kokono0204.wordpress.com/2009/10/25/ids-trong-b%e1%ba%a3o-m%e1%ba%adt-h%e1%bb%87-th%e1%bb%91ng-m%e1%ba%a1ng/</link>
		<comments>http://kokono0204.wordpress.com/2009/10/25/ids-trong-b%e1%ba%a3o-m%e1%ba%adt-h%e1%bb%87-th%e1%bb%91ng-m%e1%ba%a1ng/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 06:26:16 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[NetWork Administrator]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/2009/10/25/ids-trong-b%e1%ba%a3o-m%e1%ba%adt-h%e1%bb%87-th%e1%bb%91ng-m%e1%ba%a1ng/</guid>
		<description><![CDATA[Bài viết giúp các bạn biết thế nào là một IDS vai trò của nó trong mạng. Các dạng của IDS, phương thức phát hiện ra các tấn công, các hành động khi phát hiện ra tấn công, cách giả lập 1 mạng đánh lừa kẻ tấn công, cách giúp 1 hệ thống hoạt động [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=50&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Bài  viết giúp các bạn biết thế nào là một IDS vai trò của nó trong mạng. Các dạng  của IDS, phương thức phát hiện ra các tấn công, các hành động khi phát hiện ra  tấn công, cách giả lập 1 mạng đánh lừa kẻ tấn công, cách giúp 1 hệ thống hoạt  động ổn định và có những giải pháp khắc phục sự cố 1 cách nhanh nhất.</p>
<p>Trong bài viết này tôi trình bày với các bạn sự khác nhau  giữa các dạng Intrusion Detection, các khái niệm về các dạng đó, hiểu cách triển  khai và cấu hình mỗi dạng Intrusion Detection System. Intrusion Detection có khả  năng phát hiện các nguy cơ bảo mật xảy ra trong cả hệ thống mạng hay trong một  hệ thống cụ thể.</p>
<p><strong>Thông tin trọng yếu</strong></p>
<p>Intrusion Detection là thiết bị bảo mật vô cùng quan trọng.  Intrusion Detection Systems (IDS) là giải pháp bảo mật được bổ sung cho  Firewalls (hình dưới đây thể hiện điều đó). Một IDS có khả năng phát hiện ra các  đoạn mã độc hại hoạt động trong hệ thống mạng và có khả năng vượt qua được  Firewall.</p>
<p>Hầu hết các vấn đề liên quan tới IDS đó là cấu hình sai, đó  là việc thiết lập các thống số bị lỗi. Đó là những giao tiếp hợp lệ nhưng lại bị  thiết bị IDS cảnh báo là các giao tiếp đó là các đoạn mã nguy hiểm …</p>
<p>Có hai dạng chính của IDS đó là: Network Based và Host Based</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids1.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>Network Based</strong></p>
<p>Một Network-Based IDS (hình dưới đây) sẽ kiểm tra các giao  tiếp trên mạng với thời gian thực (real-time). Nó kiểm tra các giao tiếp, quét  header của các gói tin, và có thể kiểm tra nội dung của các gói đó để phát hiện  ra các đoạn mã nguy hiểm hay các dạng tấn công khác nhau. Một Network-Based IDS  hoạt động tin cậy trong việc kiểm tra, phát hiện các dạng tấn công trên mạng, ví  dụ như dựa vào băng thông (bandwidth-based) của tấn công Denied of Service (DoS).</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids2.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>Host Based</strong></p>
<p>Một Host-Based IDS hiển thị dưới hình dưới chỉ làm nhiệm vụ  giám sát và gi lại log cho một máy chủ (host-system). Đây là dạng IDS với giới  hạn chỉ giám sát và ghi lại toàn bộ những khả năng của host-system (nó bao gồm  cả hệ điều hành và các ứng dụng cũng như toàn bộ service của máy chủ đó). A  Host-Based IDS có khả năng phát hiện các vấn đề nếu các thông tin về máy chủ đó  được giám sát và ghi lại. Là thiết bị bảo mật cho phát hiện các tấn công trực  tiếp tới một máy chủ,</p>
<p>Hình dưới đây thể hiện một host-based IDS được hoạt động nhằm  nâng cao bảo mật cho hệ điều hành.</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids3.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>Active Detection and  Passive Detection</strong></p>
<p>IDS là một hệ thống tự động giám sát trong thời gian thực  (Network-Based IDS) hay xem sét lại các thiết lập giám sát (audit log) nhằm phát  hiện ra các lỗi bảo mật và các tấn công trực tiếp tới hệ thống mạng hay tới một  máy chủ. Có hai phương thưc cơ bản để IDS phát hiện ra các tấn công hay các nguy  cơ bảo mật là: Signature Detection và Anomaly Detection. Signature Detection  được hiển thị trong hình dưới đây sẽ so sánh các tình huống thực tế với các dạng  tấn công (signatures) được lưu trữ trong dữ liệu của IDS. Anomaly Detection thể  hiện hình bên dưới sẽ hoạt động tùy thuộc vào môi trường và có thể phát hiện ra  những biến cố bất thường. Anomaly-detection dựa vào những hoạt động bình thường  của hệ thống để tự động phát hiện ra những điều không bình thường và phân tích  xem đó là dạng tấn công nào.</p>
<p>Hiển thị: Một signature-detection IDS</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids4.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Một anomaly-detection IDS sử dụng công nghệ đỉnh cao để phân  tích dựa trên các thuật toán cao cấp.</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids5.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>Một IDS active detection: phát hiện và trả lời được thiết kế  để có hành động nhanh nhất nhằm giảm thiểu các nguy hiểm có thể sảy ra với hệ  thống. Việc trả lời có thể như tắt máy chủ hay tắt các dịch vụ, ngắt các kết nối  (được hiển thị dưới hình dưới đây).</p>
<p>Một IDS với passive detection sẽ trả lời nhưng không có các  hành động trực tiếp chống lại các tấn công. Nó có thể ghi lại log của toàn bộ hệ  thống và cảnh báo cho người quản trị hệ thống. IDS là thiết bị phát hiện tấn  công DoS rất tốt; phát hiện các bugs, flaws hoặc các tính năng ẩn, và quét  ports. Nhưng nó không có khả năng phát hiện các tấn công dựa trên các email chứa  các đoạn mã nguy hiểm.</p>
<p>Các thành phần của IDS hoạt động để giám sát mạng</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids6.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>IDS instructing TCP reset tất cả các kết nối.</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids7.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p>IDS yêu cầu Firewall chặn port 80 trong 60 giây để chống lại  các tấn công vào máy chủ Web cài IIS.</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids8.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>Honey Pots</strong></p>
<p>Một honey-pot được thể hiện trong hình dưới là một môi trường  giả lập được thiết kế để dụ dỗ và đánh lừa các kẻ tấn công và những kẻ gây dối  từ hệ thống mạng bên trong. Honey-pot thường được phát triển như một lớp đệm của  hệ thống mạng với những người dùng bình thường, phân chia thành các vùng như:  Internet, DMZ, Internal.</p>
<p>Honey-pot hoạt động như một hệ thống mạng thật, với các thông  số về dữ liệu và tài nguyên nhưng thật ra nó lại được thiết kế để đánh lừa các  kẻ tấn công. Những kẻ tấn công sẽ scan và phát hiện những lỗ hổng bảo mật tại  honey-pot nhưng đó chỉ là môi trường ảo còn mạng thật vẫn được ngụy trang rất kỹ.  Trong một hệ thống mạng yêu cầu độ an toàn cao thì sẽ có rất nhiều honey-pot với  thiết lập và nội dung giống hệt nhưng hệ thống mạng thực tế, do đó kẻ tấn công  sẽ rất khó khăn trong việc xác định mạng nào là mạng thật.</p>
<p>Việc tạo ra honey-pot nhằm cung cấp một lớp bảo vệ thông minh  cho mạng của bạn chống lại các kẻ tấn công nguy hiểm. Ngoài ra nó còn tạo một  môi trường giúp các nhà quản trị thực tập các bài test bảo mật nhưng vẫn không  ảnh hưởng tới hệ thống thật.</p>
<p>Một mạng honey pot đánh lừa những kẻ tấn công một cách thông  minh.</p>
<table border="0" align="center">
<tbody>
<tr>
<td><img style="cursor:pointer;" src="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids9.jpg" alt="" /></td>
</tr>
</tbody>
</table>
<p><strong>Incident Response.</strong></p>
<p>Khi một vấn đề bảo mật được phát hiện, incident response sẽ  phải được thiết lập. Với tác dụng là lập kế hoạch và các văn bản giảm thiểu sự  bất ngờ về mức độ nguy hiểm, tạo các bản recovery cho dữ liệu một cách nhanh  chóng, nếu sự cố xảy ra có thể giảm thiểu được thiệt hại và bất ngờ. Với thời  gian khắc phục sự cố một cách nhanh nhất.</p>
<p>Nhằm giảm thiểu bạn phải có kế hoạch:</p>
<p>- Ghi lại toàn bộ các vấn đề được phát hiện đã xử lý và nguy  cơ tiềm ẩn.</p>
<p>- Tạo các bản backup toàn bộ dữ liệu và thường xuyên kiểm tra  các ổ đĩa, có giải pháp phòng sự cố khi ổ đĩa bị hỏng.</p>
<p>- Tổng hợp toàn bộ dữ liệu log được ghi lại đôi khi cũng phải  thực hiện tạo ra các bản copy của dữ liệu đó, mặt khác bảo mật dữ liệu log cũng  rấ quan trọng.</p>
<p>- Ngoài ra bạn phải có các chính sách nhằm nâng cao độ ổn  định của hệ thống như tạo ra các kết nối dư thừa trong tình huống các kết nối  chính bị ngắt do một nguyên nhân nào đó thì không ảnh hưởng tới các dịch vụ mạng.</p>
<p><strong>Tổng kết</strong></p>
<p>Trong bài viết này các bạn cần phải nắm được thế nào là một  IDS vai trò của nó ra sao trong hệ thống mạng. Các dạng của IDS, phương thức  phát hiện ra các tấn công, các hành động khi đã phát hiện ra tấn công. Giải pháp  giả lập một mạng nhằm đánh lừa các kẻ tấn công ra sao. Cách giúp một hệ thống  hoạt động ổn định và có những giải pháp khắc phục sự cố một cách nhanh nhất.</p>
<p>Theo 3c</p>
<br />Posted in NetWork Administrator  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=50&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/10/25/ids-trong-b%e1%ba%a3o-m%e1%ba%adt-h%e1%bb%87-th%e1%bb%91ng-m%e1%ba%a1ng/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids1.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids2.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids3.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids4.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids5.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids6.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids7.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids8.jpg" medium="image" />

		<media:content url="http://3c.com.vn/Uploaded/hoangtv/08-10-007/ids9.jpg" medium="image" />
	</item>
		<item>
		<title>Cấu hình cho Iptables firewall</title>
		<link>http://kokono0204.wordpress.com/2009/10/22/c%e1%ba%a5u-hinh-cho-iptables-firewall/</link>
		<comments>http://kokono0204.wordpress.com/2009/10/22/c%e1%ba%a5u-hinh-cho-iptables-firewall/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 14:49:17 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/2009/10/22/c%e1%ba%a5u-hinh-cho-iptables-firewall/</guid>
		<description><![CDATA[Cấu hình cho Iptables firewall Trong môi trường Linux phần mềm firewall phổ biến và cơ bản nhất là iptables, thông qua nó bạn có thể dễ dàng hiểu được nguyên lý hoạt động của một hệ thống firewall nói chung. Sau đây là những kiến thức cơ bản nhất mà tôi tóm tắt về [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=48&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><span style="font-family:Arial;"><span style="font-size:x-large;"><span style="color:red;">Cấu hình cho Iptables firewall </span></span></span></strong><br />
<span style="font-family:Arial;"><span style="font-size:small;"><span style="color:sienna;"><img src="http://www.3c.com.vn/Uploaded/hoangtv/24-09/linux.gif" border="0" alt="" /></p>
<p>Trong môi trường Linux phần mềm firewall phổ biến và cơ bản nhất là iptables, thông qua nó bạn có thể dễ dàng hiểu được nguyên lý hoạt động của một hệ thống firewall nói chung. Sau đây là những kiến thức cơ bản nhất mà tôi tóm tắt về iptables</span></span></span> <span style="font-family:Arial;"><span style="font-size:small;"><span style="color:sienna;"><br />
</span></span></span> <span style="font-family:Arial;"><span style="font-size:small;"><span style="color:sienna;"><br />
</span></span></span> <span style="font-family:Arial;"><span style="font-size:small;"><span style="color:sienna;">iptables cơ bản gồm ba bảng FILTER, MANGLE, NAT và các chain trong mỗi bảng, với chúng người quản trị có thể tạo ra các rules cho phép các gói tin vào ra hệ thống (được bảo vệ bằng iptables) tuỳ theo ý muốn của mình. Chức năng cụ thể của chúng như sau.<br />
Mangle: dùng để chỉnh sửa QOS(qulity of service) bit trong phần TCP Header của gói tin<br />
Filter: đúng như tên gọi nó dùng để lọc các gói tin gồm các build-in chain<br />
+ Forward chain: lọc những gói tin đi qua hệ thống (đi vào một hệ thống khác).<br />
+ Input chain: lọc những gói tin đi vào hệ thống.<br />
+ Output chain: những gói tin đi ra từ hệ thống.</p>
<p>Nat: sửa địa chỉ gói tin gồm các build-in chain<br />
+ Pre-routing: sửa địa chỉ đích của gói tin trước khi nó được routing bởi bảng routing của hệ thống (destination NAT hay DNAT).<br />
+ Post-routing: ngược lại với Pre-routing, nó sửa địa chỉ nguồn của gói tin sau khi gói tin đã được routing bởi hệ thống (SNAT).<br />
Mỗi rule mà bạn tạo ra phải tương ứng với một chain, table nào đấy. Nếu bạn không xác định tables nào thì iptables coi mặc định là cho bảng FILTER.</p>
<p>Có thể tóm tắt trình tự xử lý gói tin của iptables bằng hình vẽ sau<br />
các gói tin từ ngoài đi vào sẽ được kiểm tra bởi các Pre-routing chain đầu tiên xem xem nó có cần DNAT không sau đó gói tin được routing. Nếu gói tin cần đi tới một hệ thống khác ( protected network ) nó sẽ được lọc bởi các FORWARD chain của bảng FILTER và nếu cần nó có thể được SNAT bởi các Post-routing chain trước khi đến được hệ thống đích.<br />
Tương tự khi hệ thống đích cần trả lời, gói tin sẽ đi theo thứ tự như vậy nhưng theo chiều ngược lại. Lưu ý trong hình vẽ những FORWARD và Post-routing chain của bảng mangle chỉ tác động vào đặc điểm QOS ( Quality of Service ) của gói tin.<br />
Nếu gói tin được gửi tới hệ thống ( hệ thống chứa iptables ) nó sẽ được xử lý bởi các INPUT chain và nếu không bị lọc bỏ nó sẽ được xử lý bởi một dịch vụ ( System Service ) nào đó chạy trên hệ thống. Khi hệ thống gửi trả lời, gói tin mà nó gửi đi được xử lý bởi các OUTPUT chain và có thể được xử ký bởi các Post-routing chain của bảng FILTER và bảng MANGLE nếu nó cần SNAT hay QoS.</p>
<div><img src="http://www.3c.com.vn/Uploaded/hoangtv/24-09/iptable.jpg" border="0" alt="" /></div>
<p>Targets và Jumps<br />
những iptables rules kiểm tra những gói ip và cố gắng xác định nó sẽ được xử lý theo kiểu nào (target), khi đã được xác định gói ip sẽ được xử lý theo kiểu đó. Sau đây là một số build-in targets thường được sử dụng.<br />
-ACCEPT: iptables chấp nhận gói tin, đưa nó qua hệ thống mà không tiếp tục kiểm tra nó nữa.<br />
- DROP: iptables loại bỏ gói tin, không tiếp tục xử lý nó nữa.<br />
- LOG: thông tin của gói tin sẽ được ghi lại bởi syslog hệ thống, iptables tiếp tục xử lý gói tin bằng những rules tiếp theo.<br />
- REJECT: chức năng của nó cũng giống như DROP tuy nhiên nó sẽ gửi một error message tới host đã gửi gói tin.<br />
- DNAT: dùng để sửa lại địa chỉ đích của gói tin.<br />
- SNAT: dùng để sửa lại địa chỉ nguồn của gói tin<br />
- MASQUERADE: cũng là một kiểu dùng để sửa địa chỉ nguồn của gói tin.</p>
<p>để xây dựng các rules bạn còn phải sử dụng các tuỳ chọn để tạo điều kiện so sánh.Sau đây là một số tuỳ chọn thường dùng.<br />
-t : chỉ ra tên của bảng mà rule của bạn sẽ dược ghi vào (mặc định là FILTER ).<br />
-j : nhẩy đến một kiểu xử lý (target) tương ứng như đã định nghĩa ở trên nếu điều kiện so sánh thoả mãn.<br />
- A : ghi nối tiếp rule vào đuôi một chain<br />
- p : so sánh protocol gói tin.<br />
- s : so sánh địa chỉ nguồn của gói tin.<br />
- d : so sánh địa chỉ đích của gói tin<br />
- i : so sánh tên card mạng mà gói tin đi vào hệ thống qua đó<br />
- o : so sánh tên card mạng mà gói tin từ hệ thống đi ra qua đó.<br />
-p tcp &#8211;sport : xác định port nguồn của gói tin TCP.<br />
-p tcp &#8211;dport : xác định port đích của gói tin TCP<br />
-p udp &#8211;sport : xác định port nguồn của gói tin UDP<br />
-p udp &#8211;dport : xác định port đích của gói tin UDP<br />
&#8211;syn : xác định gói tin có phải là một yêu cầu tạo một kết nối TCP mới không.<br />
&#8211;icmp-type : xác định loại gói icmp (echo-reply hay echo-request).<br />
-m multiport &#8211;sport &lt; port, port &gt;: xác định một loạt các giá trị port nguồn<br />
-m multiport &#8211;dport &lt; port, port &gt;: xác định một loạt các giá trị port đích.<br />
-m multiport &#8211;port &lt; port, port &gt;: xác định một loạt các giá trị port ( không phân biệt nguồn hay đích ).<br />
-m &#8211;state &lt; state &gt;: xác định trạng thái kết nối mà gói tin thể hiện<br />
ESTABLISHED: gói tin thuộc một kết nối đã được thiết lập.<br />
NEW: gói tin thể hiện một yêu cầu kết nối.<br />
RELATED : gói tin thể hiện một yêu cầu kết nối thứ hai (có liên quan đến kết nối thứ nhất, thường xuất hiện ở những giao thức FPT hay ICMP)<br />
INVALID : thể hiện một gói tin không hợp lệ</p>
<p>sau đây là một số rule ví dụ minh hoạ.</p>
<p>iptables -A INPUT -s 0/0 -i eth0 -d 192.168.1.1 -p TCP -j ACCEPT<br />
chấp nhận những gói tin từ mọi địa chỉ ip (-s 0/0) đi vào từ eth0, gửi đến hệ thống có địa chỉ 192.168.1.1</p>
<p>iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP \<br />
&#8211;sport 1024:65535 &#8211;dport 80 -j ACCEPT<br />
cho phép những gói tin từ mọi địa chỉ ip đi vào từ eth0 đi ra eth1 để đến hệ thống có địa chỉ 192.168.1.58, giá trị port nguồn của các gói tin trong phạm vi 1024 đến 65535 và giá trị port đích là 80 ( www/http).</p>
<p>iptables -A OUTPUT -p icmp &#8211;icmp-type echo-request -j ACCEPT<br />
iptables -A INPUT -p icmp &#8211;icmp-type echo-reply -j ACCEPT<br />
hệ thống được phép thực hiện lệnh ping ra ngoài.</p>
<p>iptables -A INPUT -p icmp &#8211;icmp-type echo-request \<br />
-m limit &#8211;limit 1/s -i eth0 -j ACCEPT<br />
giới hạn chỉ nhận một gói tin ping trong một giây.</p>
<p>iptables -A INPUT -p tcp &#8211;syn -m limit &#8211;limit 5/s -i eth0 -j ACCEPT<br />
giới hạn trong một giây chỉ nhận 5 yêu cầu kết nối TCP (chống kiểu tấn công syn flood)</p>
<p>iptables -A FORWARD -s 0/0 -i eth0 -d 192.168.1.58 -o eth1 -p TCP \<br />
&#8211;sport 1024:65535 -m multiport &#8211;dports 80,443 -j ACCEPT</p>
<p>iptables -A FORWARD -d 0/0 -o eth0 -s 192.168.1.58 -i eth1 -p TCP \<br />
-m state &#8211;state ESTABLISHED -j ACCEPT<br />
Mở dịch vụ web trên hệ thống 192.168.1.58.<br />
chỉ cho phép những gói tin có giá trị port nguồn trong phạm vi 1024:65535, giá trị port đích 80,443 (http,https) từ mọi địa chỉ ip đi vào card mạng eth0 đi ra card mạng eth1 đến hệ thống 192.168.1.58.<br />
cho phép hệ thống có địa chỉ 192.168.1.58 gửi ( gửi trả lời ) những gói tin TCP qua card mạng eth1 đi ra card mạng eth0.</p>
<p>Thông thường iptables lưu trữ cấu hình hiện tại của nó tại /etc/sysconfig/iptables, mỗi lần hệ thống reboot nó sẽ dùng lệnh iptable-restore để đọc lại cấu hình từ file này và active nó. Để xem cấu hình iptables hiện tại bạn có thể dùng lệnh<br />
cat /etc/sysconfig/iptables hoặc iptables-save<br />
# Generated by iptables-save v1.2.9 on Mon Nov 8 11:00:07 2004<br />
*filter<br />
:INPUT ACCEPT [0:0]<br />
:FORWARD ACCEPT [0:0]<br />
:OUTPUT ACCEPT [144:12748]<br />
:RH-Firewall-1-INPUT &#8211; [0:0]<br />
-A INPUT -j RH-Firewall-1-INPUT<br />
-A FORWARD -j RH-Firewall-1-INPUT<br />
-A RH-Firewall-1-INPUT -i lo -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p icmp -m icmp &#8211;icmp-type 255 -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p esp -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p ah -j ACCEPT<br />
-A RH-Firewall-1-INPUT -m state &#8211;state RELATED,ESTABLISHED -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p tcp -m state &#8211;state NEW -m tcp &#8211;dport 22 -j ACCEPT<br />
-A RH-Firewall-1-INPUT -j REJECT &#8211;reject-with icmp-host-prohibited<br />
COMMIT<br />
# Completed on Mon Nov 8 11:00:07 2004</p>
<p>để sửa cấu hình iptables bạn không nên sửa trực tiếp trên file /etc/sysconfig/iptables này thay vào đó bạn nên up cấu hình hiện tại ra một file tạm thời nào đó như sau<br />
iptables-save &gt; firewall-config<br />
cat firewall-config</p>
<p># Generated by iptables-save v1.2.9 on Mon Nov 8 11:00:07 2004<br />
*filter<br />
:INPUT ACCEPT [0:0]<br />
:FORWARD ACCEPT [0:0]<br />
:OUTPUT ACCEPT [144:12748]<br />
:RH-Firewall-1-INPUT &#8211; [0:0]<br />
-A INPUT -j RH-Firewall-1-INPUT<br />
-A FORWARD -j RH-Firewall-1-INPUT<br />
-A RH-Firewall-1-INPUT -i lo -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p icmp -m icmp &#8211;icmp-type 255 -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p esp -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p ah -j ACCEPT<br />
-A RH-Firewall-1-INPUT -m state &#8211;state RELATED,ESTABLISHED -j ACCEPT<br />
-A RH-Firewall-1-INPUT -p tcp -m state &#8211;state NEW -m tcp &#8211;dport 22 -j ACCEPT<br />
-A RH-Firewall-1-INPUT -j REJECT &#8211;reject-with icmp-host-prohibited<br />
COMMIT<br />
# Completed on Mon Nov 8 11:00:07 2004</p>
<p>chỉnh sửa file firewall-config theo ý bạn (thêm sửa xoá các rules)<br />
sau đó reload nó để cho iptables sử dụng xem có hoạt động đúng với ý mình không bằng lệnh<br />
iptables-restore &lt; firewall-config</p>
<p>nếu thấy hệ thống hoạt động tốt bạn có thể yên tâm save file cấu hình đó bằng lệnh<br />
service iptables save.</p>
<p><strong><em>Nguồn : VnExperts</em></strong></span></span></span></p>
<br />Posted in Linux  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=48&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/10/22/c%e1%ba%a5u-hinh-cho-iptables-firewall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>

		<media:content url="http://www.3c.com.vn/Uploaded/hoangtv/24-09/linux.gif" medium="image" />

		<media:content url="http://www.3c.com.vn/Uploaded/hoangtv/24-09/iptable.jpg" medium="image" />
	</item>
		<item>
		<title>Ứng dung .htaccess</title>
		<link>http://kokono0204.wordpress.com/2009/10/21/%e1%bb%a9ng-dung-htaccess/</link>
		<comments>http://kokono0204.wordpress.com/2009/10/21/%e1%bb%a9ng-dung-htaccess/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 15:56:53 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/?p=44</guid>
		<description><![CDATA[Posted in Uncategorized<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=44&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[Posted in Uncategorized<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=44&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/10/21/%e1%bb%a9ng-dung-htaccess/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>
	</item>
		<item>
		<title>.htacess and .htpasswd</title>
		<link>http://kokono0204.wordpress.com/2009/10/21/htacess-and-htpasswd/</link>
		<comments>http://kokono0204.wordpress.com/2009/10/21/htacess-and-htpasswd/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 15:11:40 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[NetWork Administrator]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/?p=45</guid>
		<description><![CDATA[Authentication, Authorization, and Access Control Introduction Apache has three distinct ways of dealing with the question of whether a particular request for a resource will result in that resource actually be returned. These criteria are called Authorization, Authentication, and Access control. Authentication is any process by which you verify that someone is who they claim [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=45&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1>
Authentication, Authorization, and Access Control</h1>
<h1><a name="intro">Introduction</a></h1>
<p>Apache has three distinct ways of dealing with the question     of whether a particular request for a resource will result in     that resource actually be returned. These criteria are called     <em>Authorization</em>, <em>Authentication</em>, and <em>Access     control</em>.</p>
<p>Authentication is any process by which you verify that     someone is who they claim they are. This usually involves a     username and a password, but can include any other method of     demonstrating identity, such as a smart card, retina scan,     voice recognition, or fingerprints. Authentication is     equivalent to showing your drivers license at the ticket     counter at the airport.</p>
<p>Authorization is finding out if the person, once identified,     is permitted to have the resource. This is usually determined     by finding out if that person is a part of a particular group,     if that person has paid admission, or has a particular level of     security clearance. Authorization is equivalent to checking the     guest list at an exclusive party, or checking for your ticket     when you go to the opera.</p>
<p>Finally, access control is a much more general way of     talking about controlling access to a web resource. Access can     be granted or denied based on a wide variety of criteria, such     as the network address of the client, the time of day, the     phase of the moon, or the browser which the visitor is using.     Access control is analogous to locking the gate at closing     time, or only letting people onto the ride who are more than 48     inches tall &#8211; it&#8217;s controlling entrance by some arbitrary     condition which may or may not have anything to do with the     attributes of the particular visitor.</p>
<p>Because these three techniques are so closely related in     most real applications, it is difficult to talk about them     separate from one another. In particular, authentication and     authorization are, in most actual implementations,     inextricable.</p>
<p>If you have information on your web site that is sensitive,     or intended for only a small group of people, the techniques in     this tutorial will help you make sure that the people that see     those pages are the people that you wanted to see them.</p>
<h1><a name="basic"></a>Basic authentication</h1>
<p>As the name implies, basic authentication is the simplest     method of authentication, and for a long time was the most     common authentication method used. However, other methods of     authentication have recently passed basic in common usage, due     to usability issues that will be discussed in a minute.</p>
<h2><a name="basicworks"></a><br />
How basic authentication works</h2>
<p>When a particular resource has been protected using basic     authentication, Apache sends a <tt>401 Authentication     Required</tt> header with the response to the request, in order     to notify the client that user credentials must be supplied in     order for the resource to be returned as requested.</p>
<p>Upon receiving a <tt>401</tt> response header, the client&#8217;s     browser, if it supports basic authentication, will ask the user     to supply a username and password to be sent to the server. If     you are using a graphical browser, such as Netscape or Internet     Explorer, what you will see is a box which pops up and gives     you a place to type in your username and password, to be sent     back to the server. If the username is in the approved list,     and if the password supplied is correct, the resource will be     returned to the client.</p>
<p>Because the HTTP protocol is stateless, each request will be     treated in the same way, even though they are from the same     client. That is, every resource which is requested from the     server will have to supply authentication credentials over     again in order to receive the resource.</p>
<p>Fortunately, the browser takes care of the details here, so     that you only have to type in your username and password one     time per browser session &#8211; that is, you might have to type it     in again the next time you open up your browser and visit the     same web site.</p>
<p>Along with the <tt>401</tt> response, certain other     information will be passed back to the client. In particular,     it sends a name which is associated with the protected area of     the web site. This is called the <em>realm</em>, or just the     authentication name. The client     browser caches the username and password that you supplied, and     stores it along with the authentication realm, so that if other     resources are requested from the same realm, the same username     and password can be returned to authenticate that request     without requiring the user to type them in again. This caching     is usually just for the current browser session, but some     browsers allow you to store them permanently, so that you never     have to type in your password again.</p>
<p>The authentication name, or realm, will appear in the pop-up     box, in order to identify what the username and password are     being requested for.</p>
<h2><a name="basicconfig"></a> Configuration: Protecting content with basic     authentication</h2>
<p>There are two configuration steps which you must complete in     order to protect a resource using basic authentication. Or     three, depending on what you are trying to do.</p>
<ol>
<li>Create a password file</li>
<li>Set the configuration to use this password file</li>
<li>Optionally, create a group file</li>
</ol>
<h3><a name="htpasswd"></a><br />
Create a password file</h3>
<p>In order to determine whether a particular username/password     combination is valid, the username and password supplied by the     user will need to be compared to some authoritative listing of     usernames and password. This is the password file, which you     will need to create on the server side, and populate with valid     users and their passwords.</p>
<p>Because this file contains sensitive information, it should     be stored outside of the document directory. Although, as you     will see in a moment, the passwords are encrypted in the file,     if a cracker were to gain access to the file, it would be an     aid in their attempt to figure out the passwords. And, because     people tend to be sloppy with the passwords that they choose,     and use the same password for web site authentication as for     their bank account, this potentially be a very serious breach     of security, even if the content on your web site is not     particularly sensitive.</p>
<p><strong>Caution:</strong> Encourage your users to use a different     password for your web site than for other more essential     things. For example, many people tend to use two passwords &#8211;     one for all of their extremely important things, such as the     login to their desktop computer, and for their bank account,     and another for less sensitive things, the compromise of which     would be less serious.</p>
<p>To create the password file, use the <tt>htpasswd</tt> utility that came with Apache. This will be located in the     <tt>bin</tt> directory of wherever you installed Apache. For     example, it will probably be located at     <tt>/usr/local/apache/bin/htpasswd</tt> if you installed Apache     from source.</p>
<p>To create the file, type:</p>
<pre>htpasswd -c /usr/local/apache/passwd/passwords username
</pre>
<p><tt>htpasswd</tt> will ask you for the password, and then     ask you to type it again to confirm it:</p>
<pre># htpasswd -c /usr/local/apache/passwd/passwords rbowen
New password: mypassword
Re-type new password: mypassword
Adding password for user rbowen
</pre>
<p>Note that in the example shown, a password file is being     created containing a user called <tt>rbowen</tt>, and this     password file is being placed in the location     <tt>/usr/local/apache/passwd/passwords</tt>. You will     substitute the location, and the username, which you want to     use to start your password file.</p>
<p>If <tt>htpasswd</tt> is not in your path, you will have to     type the full path to the file to get it to run. That is, in     the example above, you would replace <tt>htpasswd</tt> with     <tt>/usr/local/apache/bin/htpasswd</tt></p>
<p>The <tt>-c</tt> flag is used only when you are creating a     new file. After the first time, you will omit the <tt>-c</tt> flag, when you are adding new users to an already-existing     password file.</p>
<pre>htpasswd /usr/local/apache/passwd/passwords sungo
</pre>
<p>The example just shown will add a user named <tt>sungo</tt> to a password file which has already been created earlier. As     before, you will be asked for the password at the command line,     and then will be asked to confirm the password by typing it     again.</p>
<p><strong>Caution:</strong> Be very careful when you add new users to an     existing password file that you don&#8217;t use the <tt>-c</tt> flag     by mistake. Using the <tt>-c</tt> flag will create a new     password file, even if you already have an existing file of     that name. That is, it will remove the contents of the file     that is there, and replace it with a new file containing only     the one username which you were adding.</p>
<p>The password is stored in the password file in encrypted     form, so that users on the system will not be able to read the     file and immediately determine the passwords of all the users.     Nevertheless, you should store the file in as secure a location     as possible, with whatever minimum permissions on the file so     that the web server itself can read the file. For example, if     your server is configured to run as user <tt>nobody</tt> and     group <tt>nogroup</tt>, then you should set permissions on the     file so that only the webserver can read the file and only     root can write to it:</p>
<pre>chown root.nogroup /usr/local/apache/passwd/passwords
chmod 640 /usr/local/apache/passwd/passwords
</pre>
<p>On Windows, a similar precaution should be taken, changing     the ownership of the password file to the web server user, so     that other users cannot read the file.</p>
<h3><a name="htpasswdconfig"></a><br />
Set the configuration to use this password file</h3>
<p>Once you have created the password file, you need to tell     Apache about it, and tell Apache to use this file in order to     require user credentials for admission. This configuration is     done with the following directives:</p>
<table border="0" cellpadding="3">
<tbody>
<tr>
<td align="left">AuthType</td>
<td width="360" align="left" valign="top">Authentication         type being used. In this case, it will be set to         <tt>Basic</tt></td>
</tr>
<tr>
<td align="left">AuthName</td>
<td width="360" align="left" valign="top">The         authentication realm or name</td>
</tr>
<tr>
<td align="left">AuthUserFile</td>
<td width="360" align="left" valign="top">The location of         the password file</td>
</tr>
<tr>
<td align="left">AuthGroupFile</td>
<td width="360" align="left" valign="top">The location of         the group file, if any</td>
</tr>
<tr>
<td align="left">Require</td>
<td width="360" align="left" valign="top">The         requirement(s) which must be satisfied in order to grant         admission</td>
</tr>
</tbody>
</table>
<p>These directives may be placed in a <tt>.htaccess</tt> file     in the particular directory being protected, or may go in the     main server configuration file, in a <tt>&lt;Directory&gt;</tt> section, or other scope container.</p>
<p>The example shown below defines an authentication realm     called &#8220;By Invitation Only&#8221;. The password file located at     <tt>/usr/local/apache/passwd/passwords</tt> will be used to     verify the user&#8217;s identity. Only users named <tt>rbowen</tt> or     <tt>sungo</tt> will be granted access, and even then only if     they provide a password which matches the password stored in     the password file.</p>
<pre>AuthType Basic
AuthName "By Invitation Only"
AuthUserFile /usr/local/apache/passwd/passwords
Require user rbowen sungo
</pre>
<p>The phrase &#8220;By Invitation Only&#8221; will be displayed in the     password pop-up box, where the user will have to type their     credentials.</p>
<p>You will need to restart your Apache server in order for the     new configuration to take effect, if these directives were put     in the main server configuration file. Directives placed in     <tt>.htaccess</tt> files take effect immediately, since     <tt>.htaccess</tt> files are parsed each time files are     served.</p>
<p>The next time that you load a file from that directory, you     will see the familiar username/password dialog box pop up,     requiring that you type the username and password before you     are permitted to proceed.</p>
<p>Note that in addition to specifically listing the users to     whom you want to grant access, you can specify that any valid     user should be let in. This is done with the     <tt>valid-user</tt> keyword:</p>
<pre>Require valid-user
</pre>
<h3><a name="basicgroupfile"></a><br />
Optionally, create a group file</h3>
<p>Most of the time, you will want more than one, or two, or     even a dozen, people to have access to a resource. You want to     be able to define a group of people that have access to that     resource, and be able to manage that group of people, adding     and removing members, without having to edit the server     configuration file, and restart Apache, each time.</p>
<p>This is handled using authentication groups. An     authentication group is, as you would expect, a group name     associated with a list of members. This list is stored in a     group file, which should be stored in the same location as the     password file, so that you are able to keep track of these     things.</p>
<p>The format of the group file is exceedingly simple. A group     name appears first on a line, followed by a colon, and then a     list of the members of the group, separated by spaces. For     example:</p>
<pre>authors: rich daniel allan
</pre>
<p>Once this file has been created, you can <tt>Require</tt> that someone be in a particular group in order to get the     requested resource. This is done with the     <tt>AuthGroupFile</tt> directive, as shown in the following     example.</p>
<pre>AuthType Basic
AuthName "Apache Admin Guide Authors"
AuthUserFile /usr/local/apache/passwd/passwords
AuthGroupFile /usr/local/apache/passwd/groups
Require group authors
</pre>
<p>The authentication process is now one step more involved.     When a request is received, and the requested username and     password are supplied, the group file is first checked to see     if the supplied username is even in the required group. If it     is, then the password file will be checked to see if the     username is in there, and if the supplied password matches the     password stored in that file. If any of these steps fail,     access will be forbidden.</p>
<h2><a name="basicfaq"></a><br />
Frequently asked questions about basic auth</h2>
<p>The following questions tend to get asked very frequently     with regard to basic authentication. It should be understood     that basic authentication is very basic, and so is limited to     the set of features that has been presented above. Most of the     more interesting things that people tend to want, need to be     implemented using some alternate authentication scheme.</p>
<h3><a name="logout"></a><br />
How do I log out?</h3>
<p>Since browsers first started implementing basic     authentication, website administrators have wanted to know how     to let the user log out. Since the browser caches the username     and password with the authentication realm, as described     earlier in this tutorial, this is not a function of the server     configuration, but is a question of getting the browser to     forget the credential information, so that the next time the     resource is requested, the username and password must be     supplied again. There are numerous situations in which this is     desirable, such as when using a browser in a public location,     and not wishing to leave the browser logged in, so that the     next person can get into your bank account.</p>
<p>However, although this is perhaps the most frequently asked     question about basic authentication, thus far none of the major     browser manufacturers have seen this as being a desirable     feature to put into their products.</p>
<p>Consequently, the answer to this question is, you can&#8217;t.     Sorry.</p>
<h3><a name="passworddialog"></a><br />
How can I change what the password box looks like?</h3>
<p>The dialog that pops up for the user to enter their username     and password is ugly. It contains text that you did not     indicate that you wanted in there. It looks different in     Internet Explorer and Netscape, and contains different text.     And it asks for fields that the user might not understand &#8211;     for example, Netscape asks the user to type in their &#8220;User     ID&#8221;, and they might not know what that means. Or, you might     want to provide additional explanatory text so that the user     has a better idea what is going on.</p>
<p>Unfortunately, these things are features of the browser, and     cannot be controlled from the server side. If you want the     login to look different, then you will need to implement your     own authentication scheme. There is no way to change what this     login box looks like if you are using basic authentication.</p>
<h3><a name="persistpass"></a><br />
How to I make it not ask me for my password the next     time?</h3>
<p>Because most browsers store your password information only     for the current browser session, when you close your browser it     forgets your username and password. So, when you visit the same     web site again, you will need to re-enter your username and     password.</p>
<p>There is nothing that can be done about this on the server     side.</p>
<p>However, the most recent versions of the major browsers     contain the ability to remember your password forever, so that     you never have to log in again. While it is debatable whether     this is a good idea, since it effectively overrides the entire     point of having security in the first place, it is certainly     convenient for the user, and simplifies the user     experience.</p>
<h3><a name="passwordtwice"></a><br />
Why does it sometimes ask me for my password twice?</h3>
<p>When entering a password-protected web site for the first     time, you will occasionally notice that you are asked for your     password twice. This may happen immediately after you entered     the password the first time, or it may happen when you click on     the first link after authenticating the first time.</p>
<p>This happens for a very simple, but nonetheless confusing,     reason, again having to do with the way that the browser caches     the login information.</p>
<p>Login information is stored on the browser based on the     authentication realm, specified by the <tt>AuthName</tt> directive, and by the server name. In this way, the browser can     distinguish between the <tt>Private</tt> authentication realm     on one site and on another. So, if you go to a site using one     name for the server, and internal links on the server refer to     that server by a different name, the browser has no way to know     that they are in fact the same server.</p>
<p>For example, if you were to visit the URL     <tt>http://example.com/private/</tt>, which required     authentication, your browser would remember the supplied     username and password, associated with the hostname     <tt>example.com</tt>. If, by virtue of an internal redirect, or     fully-qualified HTML links in pages, you are then sent to the     URL <tt>http://www.example.com/private/</tt>, even though this     is really exactly the same URL, the browser does not know this     for sure, and is forced to request the authentication     information again, since <tt>example.com</tt> and     <tt>www.example.com</tt> are not exactly the same hostname.     Your browser has no particular way to know that these are the     same web site.</p>
<h2><a name="basiccaveat"></a><br />
Security caveat</h2>
<p>Basic authentication should not be considered secure for any     particularly rigorous definition of secure.</p>
<p>Although the password is stored on the server in encrypted     format, it is passed from the client to the server in plain     text across the network. Anyone listening with any variety of     packet sniffer will be able to read the username and password     in the clear as it goes across.</p>
<p>Not only that, but remember that the username and password     are passed with every request, not just when the user first     types them in. So the packet sniffer need not be listening at a     particularly strategic time, but just for long enough to see     any single request come across the wire.</p>
<p>And, in addition to that, the content itself is also going     across the network in the clear, and so if the web site     contains sensitive information, the same packet sniffer would     have access to that information as it went past, even if the     username and password were not used to gain direct access to     the web site.</p>
<p>Don&#8217;t use basic authentication for anything that requires     real security. It is a detriment for most users, since very few     people will take the trouble, or have the necessary software     and/or equipment, to find out passwords. However, if someone     had a desire to get in, it would take very little for them to     do so.</p>
<p>Basic authentication across an SSL connection, however, will be     secure, since everything is going to be encrypted, including the     username and password.</p>
<h1><a name="digest"></a>Digest authentication</h1>
<p>Addressing one of the security caveats of basic     authentication, digest authentication provides an alternate     method for protecting your web content. However, it to has a     few caveats.</p>
<h2><a name="digestworks">How digest auth works</a></h2>
<p>Digest authentication is implemented by the module     <tt>mod_auth_digest</tt>. There is an older module,     <tt>mod_digest</tt>, which implemented an older version of the     digest authentication specification, but which will probably     not work with newer browsers.</p>
<p>Using digest authentication, your password is never sent     across the network in the clear, but is always transmitted as     an MD5 digest of the user&#8217;s password. In this way, the password     cannot be determined by sniffing network traffic.</p>
<p>The full specification of digest authentication can be seen     in the internet standards document RFC 2617, which you can see     at <tt>http://www1.ics.uci.edu/pub/ietf/http/rfc2617.txt</tt>.     Additional information and resources about MD5 can be found at     <tt>http://userpages.umbc.edu/ mabzug1/cs/md5/md5.html</tt></p>
<h2><a name="digestconfig"></a>Configuration:     Protecting content with digest authentication</h2>
<p>The steps for configuring your server for digest     authentication are very similar for those for basic     authentication.</p>
<ol>
<li>Create the password file</li>
<li>Set the configuration to use this password file</li>
<li>Optionally, create a group file</li>
</ol>
<h3><a name="htdigest"></a>Creating a password file</h3>
<p>As with basic authentication, a simple utility is provided     to create and maintain the password file which will be used to     determine whether a particular user&#8217;s name and password are     valid. This utility is called <tt>htdigest</tt>, and will be     located in the <tt>bin</tt> directory of wherever you installed     Apache. If you installed Apache from some variety of package     manager, <tt>htdigest</tt> is likely to have been placed     somewhere in your path.</p>
<p>To create a new digest password file, type:</p>
<pre>htdigest -c /usr/local/apache/passwd/digest realm username
</pre>
<p><tt>htdigest</tt> will ask you for the desired password, and     then ask you to type it again to confirm it.</p>
<p>Note that the realm for which the authentication will be     required is part of the argument list.</p>
<p>Once again, as with basic authentication, you are encouraged     to place the generated file somewhere outside of the document     directory.</p>
<p>And, as with the <tt>htpasswd</tt> utility, the <tt>-c</tt> flag creates a new file, or, if a file of that name already     exists, deletes the contents of that file and generates a new     file in its place. Omit the <tt>-c</tt> flag in order to add     new user information to an existing password file.</p>
<h3><a name="htdigestconfig"></a>Set the configuration     to use this password file</h3>
<p>Once you have created a password file, you need to tell     Apache about it in order to start using it as a source of     authenticated user information. This configuration is done with     the following directives:</p>
<table border="0" cellpadding="3">
<tbody>
<tr>
<td align="left">AuthType</td>
<td width="360" align="left" valign="top">Authentication         type being used. In this case, it will be set to         <tt>Digest</tt></td>
</tr>
<tr>
<td align="left">AuthName</td>
<td width="360" align="left" valign="top">The         authentication realm or name</td>
</tr>
<tr>
<td align="left">AuthDigestFile</td>
<td width="360" align="left" valign="top">The location of         the password file</td>
</tr>
<tr>
<td align="left">AuthDigestGroupFile</td>
<td width="360" align="left" valign="top">Location of the         group file, if any</td>
</tr>
<tr>
<td align="left">Require</td>
<td width="360" align="left" valign="top">The         requirement(s) which must be satisfied in order to grant         admission</td>
</tr>
</tbody>
</table>
<p>These directives may be placed in a <tt>.htaccess</tt> file     in the particular directory being protected, or may go in the     main server configuration file, in a <tt>&lt;Directory&gt;</tt> section, or another scope container.</p>
<p>The following example defines an authentication realm called     &#8220;Private&#8221;. The password file located at     <tt>/usr/local/apache/passwd/digest</tt> will be used to verify     the user&#8217;s identity. Only users named <tt>drbacchus</tt> or     <tt>dorfl</tt> will be granted access, if they provide a     password that patches the password stored in the password     file.</p>
<pre>AuthType Digest
AuthName "Private"
AuthDigestFile /usr/local/apache/passwd/digest
Require user drbacchus dorfl
</pre>
<p>The phrase &#8220;Private&#8221; will be displayed in the password     pop-up box, where the user will have to type their     credentials.</p>
<h3><a name="digestgroup"></a>Optionally, create a group file</h3>
<p>As you have observed, there are not many differences between     this configuration process and that required by basic     authentication, described in the previous section. This is true     also of group functionality. The group file used for digest     authentication is exactly the same as that used for basic     authentication. That is to say, lines in the group file consist     the name of the group, a colon, and a list of the members of     that group. For example:</p>
<pre>admins: jim roy ed anne
</pre>
<p>Once this file has been created, you can <tt>Require</tt> that someone be in a particular group in order to get the     requested resource. This is done with the     <tt>AuthDigestGroupFile</tt> directive, as shown in the     following example.</p>
<pre>AuthType Digest
AuthName "Private"
AuthDigestFile /usr/local/apache/passwd/digest
AuthDigestGroupFile /usr/local/apache/passwd/digest.groups
Require group admins
</pre>
<p>The authentication process is the same as that used by basic     authentication. It is first verified that the user is in the     required group, and, if this is true, then the password is     verified.</p>
<h2><a name="digestcaveat">Caveats</a></h2>
<p>Before you leap into using digest authentication instead of     basic authentication, there are a few things that you should     know about.</p>
<p>Most importantly, you need to know that, although digest     authentication has this great advantage that you don&#8217;t send     your password across the network in the clear, it is not     supported by all major browsers in use today, and so you should     not use it on a web site on which you cannot control the     browsers that people will be using, such as on your intranet     site. In particular, Opera 4.0 or later, Microsoft Internet     Explorer 5.0 or later, Mozilla 1.0.1 and Netscape 7 or later     as well as Amaya support digest authentication, while various     other browsers do not.</p>
<p>Next, with regard to security considerations, you should     understand two things. Although your password is not passed in     the clear, all of your data is, and so this is a rather small     measure of security. And, although your password is not really     sent at all, but a digest form of it, someone very familiar     with the workings of HTTP could use that information &#8211; just     your digested password &#8211; and use that to gain access to the     content, since that digested password is really all the     information required to access the web site.</p>
<p>The moral of this is that if you have content that really     needs to be kept secure, use SSL.</p>
<h1><a name="database">Database authentication     modules</a></h1>
<p>Basic authentication and digest authentication both suffer     from the same major flaw. They use text files to store the     authentication information. The problem with this is that     looking something up in a text file is very slow. It&#8217;s rather     like trying to find something in a book that has no index. You     have to start at the beginning, and work through it one page at     a time until you find what you are looking for. Now imagine     that the next time you need to find the same thing, you don&#8217;t     remember where it was before, so you have to start at the     beginning again, and work through one page at a time until you     find it again. And the next time. And the time after that.</p>
<p>Since HTTP is stateless, authentication has to be verified     every time that content is requested. And so every time a     document is accessed which is secured with basic or digest     authentication, Apache has to open up those text password files     and look through them one line at a time, until it finds the     user that is trying to log in, and verifies their password. In     the worst case, if the username supplied is not in there at     all, every line in the file will need to be checked. On     average, half of the file will need to be read before the user     is found. This is very slow.</p>
<p>While this is not a big problem for small sets of users,     when you get into larger numbers of users (where &#8220;larger&#8221; means     a few hundred) this becomes prohibitively slow. In many cases,     in fact, valid username/password combinations will get rejected     because the authentication module just had to spend so much     time looking for the username in the file that Apache will just     get tired of waiting and return a failed authentication.</p>
<p>In these cases, you need an alternative, and that     alternative is to use some variety of database. Databases are     optimized for looking for a particular piece of information in     a very large data set. It builds indexes in order to rapidly     locate a particular record, and they have query languages for     swiftly locating records that match particular criteria.</p>
<p>There are numerous modules available for Apache to     authenticate using a variety of different databases. In this     section, we&#8217;ll just look at two modules which ship with Apache.</p>
<h2><a name="modauthdb"></a>mod_auth_db and mod_auth_dbm</h2>
<p><tt>mod_auth_db</tt> and <tt>mod_auth_dbm</tt> are modules     which lets you keep your usernames and passwords in DB or DBM     files. There are few practical differences between DB files and     DBM files. And, on some operating systems, such as various     BSDs, and Linux, they are exactly the same thing. You should     pick whichever of the two modules makes the most sense on your     particular platform of choice. If you do not have DB support on     your platform, you may need to install it. You download an     implementation of DB at <tt>http://www.sleepycat.com/</tt>.</p>
<h2><a name="dbfiles"></a>Berkeley DB files</h2>
<p>DB files, also known as Berkeley database files, are the     simplest form of database, and are rather ideally suited for     the sort of data that needs to be stored for HTTP     authentication. DB files store key/value pairs. That is, the     name of a variable, and the value of that variable. While other     databases allow the storage of many fields in a given record, a     DB file allows only this pairing of key and value.<a name="foot1_return" href="http://httpd.apache.org/docs/1.3/howto/auth.html#foot1"><sup>1</sup></a> This is ideal for     authentication, which requires only the pair of a username and     password.</p>
<h2><a name="installauthdb">Installing mod_auth_db</a></h2>
<p>For the purposes of this tutorial, we&#8217;ll talk about     installing and configuring <tt>mod_auth_db</tt>. However,     everything that is said here can be directly applied to     <tt>mod_auth_dbm</tt> by simply replacing &#8216;db&#8217; with &#8216;dbm&#8217; and     &#8216;DB&#8217; with &#8216;DBM&#8217; in the various commands, file names, and     directives.</p>
<p>Since <tt>mod_auth_db</tt> is not compiled in by default,     you will need to rebuild Apache in order to get the     functionality, unless you built in everything when we started.     Note that if you installed Apache with shared object     support, you may be able to just build the module and load it     in to Apache.</p>
<p>To build Apache from scratch with <tt>mod_auth_db</tt> built     in, use the following <tt>./configure</tt> line in your apache     source code directory.</p>
<pre>./configure --enable-module=auth_db
</pre>
<p>Or, if you had a more complex <tt>configure</tt> command     line, you can just add the <tt>-enable-module=auth_db</tt> option to that command line, and you&#8217;ll get     <tt>mod_auth_db</tt> built into your server.</p>
<h2><a name="authdbconfig">Protecting a directory with     mod_auth_db</a></h2>
<p>Once you have compiled the <tt>mod_auth_db</tt> module, and     loaded it into your web server, you&#8217;ll find that there&#8217;s very     little difference between using regular authentication and     using <tt>mod_auth_db</tt> authentication. The procedure is the     same as that we went through with basic and digest     authentication:</p>
<ol>
<li>Create the user file.</li>
<li>Configure Apache to use that file for       authentication.</li>
<li>Optionally, create a group file.</li>
</ol>
<h3><a name="dbmmanage"></a>Create the user file</h3>
<p>The user file for authentication is, this time, not a flat     text file, but is a DB file<a name="foot2_return" href="http://httpd.apache.org/docs/1.3/howto/auth.html#foot2"><sup>2</sup></a>. Fortunately, once again,     Apache provides us with a simple utility for the purpose of     managing this user file. This time, the utility is called     <tt>dbmmanage</tt>, and will be located in the <tt>bin</tt> subdirectory of wherever you installed Apache.</p>
<p><tt>dbmmanage</tt> is somewhat more complicated to use than     <tt>htpasswd</tt> or <tt>htdigest</tt>, but it is still fairly     simple. The syntax which you will usually be using is as     follows:</p>
<pre>dbmmanage passwords.dat adduser montressor
</pre>
<p>As with <tt>htpasswd</tt>, you will at this point be     prompted for a password, and then asked to confirm that     password by typing it again. The main difference here is that     rather than a text file being created, you are creating a     binary file containing the information that you have     supplied.</p>
<p>Type <tt>dbmmanage</tt> with no arguments to get the full     list of options available with this utility.</p>
<h3><a name="perl_dbfile">Creating your user file with     Perl</a></h3>
<p>Note that, if you are so inclined, you can manage your user     file with Perl, or any other language which has a DB-file     module, for interfacing with this type of database. This covers     a number of popular programming languages.</p>
<p>The following Perl code, for example, will add a user     &#8216;rbowen&#8217;, with password &#8216;mypassword&#8217;, to your password     file:</p>
<pre>use DB_File;
tie %database, 'DB_File', "passwords.dat"
    or die "Can't initialize database: $!\n";

$username = 'rbowen';
$password = 'mypassword';
@chars=(0..9,'a'..'z');
$salt = $chars[int rand @chars] . $chars[int rand @chars];

$crypt = crypt($password, $salt);
$database{$username} = $crypt;

untie %database;
</pre>
<p>As you can imagine, this makes it very simple to write tools     to manage the user and password information stored in these     files.</p>
<p>Passwords are stored in Unix <tt>crypt</tt> format, just as     they were in the &#8220;regular&#8221; password files. The &#8216;salt&#8217; that is     created in the middle there is part of the process, generating     a random starting point for that encryption. The technique     being used is called a &#8216;tied hash&#8217;. The idea is to tie a     built-in data structure to the contents of the file, such that     when the data structure is changed, the file is automatically     modified at the same time.</p>
<h3><a name="authdbuserfile"></a>Configuring Apache     to use this password file</h3>
<p>Once you have created the password file, you need to tell     Apache about it, and tell Apache to use this file to verify     user credentials. This configuration will look almost the same     as that for basic authentication. This configuration can go in     a <tt>.htaccess</tt> file in the directory to be protected, or     can go in the main server configuration, in a     <tt>&lt;Directory&gt;</tt> section, or other scope container     directive.</p>
<p>The configuration will look something like the     following:</p>
<pre>AuthName "Members Only"
AuthType Basic
AuthDBUserFile /usr/local/apache/passwd/passwords.dat
require user rbowen
</pre>
<p>Now, users accessing the directory will be required to     authenticate against the list of valid users who are in     <tt>/usr/local/apache/passwd/passwords.dat</tt>.</p>
<h3><a name="authdbgroupfile"></a><br />
Optionally, create a group file</h3>
<p>As mentioned earlier, DB files store a key/value pair. In     the case of group files, the key is the name of the user, and     the value is a comma-separated list of the groups to which the     user belongs.</p>
<p>While this is the opposite of the way that group files are     stored elsewhere, note that we will primarily be looking up     records based on the username, so it is more efficient to index     the file by username, rather than by the group name.</p>
<p>Groups can be added to your group file using     <tt>dbmmanage</tt> and the <tt>add</tt> command:</p>
<pre>dbmmanage add groupfile rbowen one,two,three
</pre>
<p>In the above example, <tt>groupfile</tt> is the literal name     of the group file, <tt>rbowen</tt> is the user being added, and     <tt>one</tt>, <tt>two</tt>, and <tt>three</tt> are names of     three groups to which this user belongs.</p>
<p>Once you have your groups in the file, you can require a     group in the regular way:</p>
<pre>AuthName "Members Only"
AuthType Basic
AuthDBUserFile /usr/local/apache/passwd/passwords.dat
AuthDBGroupFile /usr/local/apache/passwd/groups.dat
require group three
</pre>
<p>Note that if you want to use the same file for both password     and group information, you can do so, but this is a little more     complicated to manage, as you have to encrypt the password     yourself before you feed it to the <tt>dbmmanage</tt> utility.</p>
<h1><a name="access"></a>Access control</h1>
<p>Authentication by username and password is only part of the     story. Frequently you want to let people in based on something     other than who they are. Something such as where they are     coming from. Restricting access based on something other than     the identity of the user is generally referred to as <em>Access     Control</em>.</p>
<h2><a name="allowdeny"></a>Allow and Deny</h2>
<p>The <tt>Allow</tt> and <tt>Deny</tt> directives let you     allow and deny access based on the host name, or host address,     of the machine requesting a document. The directive goes     hand-in-hand with these is the <tt>Order</tt> directive, which     tells Apache in which order to apply the filters.</p>
<p>The usage of these directives is:</p>
<pre>allow from address
</pre>
<p>where <em>address</em> is an IP address (or a partial IP     address) or a fully qualified domain name (or a partial domain     name); you may provide multiple addresses or domain names, if     desired.</p>
<p>For example, if you have someone spamming your message     board, and you want to keep them out, you could do the     following:</p>
<pre>deny from 11.22.33.44
</pre>
<p>Visitors coming from that address will not be able to see     the content behind this directive. If, instead, you have a     machine name, rather than an IP address, you can use that.</p>
<pre>deny from hostname.example.com
</pre>
<p>And, if you&#8217;d like to block access from an entire domain,     or even from an entire tld (top level domain, such as .com or .gov)     you can specify just part of an address or domain name:</p>
<pre>deny from 192.101.205
deny from exampleone.com exampletwo.com
deny from tld
</pre>
<p>Using <tt>Order</tt> will let you be sure that you are     actually restricting things to the group that you want to let     in, by combining a <tt>deny</tt> and an <tt>allow</tt> directive:</p>
<pre>Order Deny,Allow
Deny from all
Allow from hostname.example.com
</pre>
<p>Listing just the <tt>allow</tt> directive would not do what     you want, because it will let users from that host in, in     addition to letting everyone in. What you want is to let in     <em>only</em> users from that host.</p>
<h2><a name="satisfy"></a>Satisfy</h2>
<p>The <tt>Satisfy</tt> directive can be used to specify that     several criteria may be considered when trying to decide if a     particular user will be granted admission. <tt>Satisfy</tt> can     take as an argument one of two options &#8211; <tt>all</tt> or     <tt>any</tt>. By default, it is assumed that the value is     <tt>all</tt>. This means that if several criteria are     specified, then all of them must be met in order for someone to     get in. However, if set to <tt>any</tt>, then several criteria     may be specified, but if the user satisfies any of these, then     they will be granted entrance.</p>
<p>A very good example of this is using access control to     assure that, although a resource is password protected from     outside your network, all hosts inside the network will be     given free access to the resource. This would be accomplished     by using the <tt>Satisfy</tt> directive, as shown below.</p>
<pre>&lt;Directory /usr/local/apache/htdocs/sekrit&gt;
  AuthType Basic
  AuthName intranet
  AuthUserFile /www/passwd/users
  AuthGroupFile /www/passwd/groups
  Require group customers
  Order allow,deny
  Allow from internal.com
  Satisfy any
&lt;/Directory&gt;
</pre>
<p>In this scenario, users will be let in if they either have a     password, or if they are in the internal network.</p>
<h1><a name="summary">Summary</a></h1>
<p>The various authentication modules provide a number of ways     to restrict access to your host based on the identity of the     user. They offer a somewhat standard interface to this     functionality, but provide different back-end mechanisms for     actually authenticating the user.</p>
<p>And the access control mechanism allows you to restrict     access based on criteria unrelated to the identity of the     user.</p>
<br />Posted in Linux, NetWork Administrator  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=45&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/10/21/htacess-and-htpasswd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>
	</item>
		<item>
		<title>Sử dụng bộ soạn thảo vi trong UNIX và Linux</title>
		<link>http://kokono0204.wordpress.com/2009/10/20/s%e1%bb%ad-d%e1%bb%a5ng-b%e1%bb%99-so%e1%ba%a1n-th%e1%ba%a3o-vi-trong-unix-va-linux/</link>
		<comments>http://kokono0204.wordpress.com/2009/10/20/s%e1%bb%ad-d%e1%bb%a5ng-b%e1%bb%99-so%e1%ba%a1n-th%e1%ba%a3o-vi-trong-unix-va-linux/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 09:11:02 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[Guide software]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/?p=40</guid>
		<description><![CDATA[Sử dụng bộ soạn thảo vi trong UNIX và Linux Phần I: Giới thiệu vi: Có rất nhiều trình soạn thảo trong UNIX cũng như Linux. Nhưng tình soạn thảo vi là trình soạn thảo được nhiều chuyên gia UNIX và Linux sử dụng. Có lẽ một phần là nó chiếm ít tài nguyên, nhưng [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=40&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;"><strong>Sử dụng bộ soạn thảo vi trong UNIX và Linux</strong></span></p>
<p><strong>Phần I: Giới thiệu vi:</strong></p>
<p> Có rất nhiều trình soạn thảo trong UNIX cũng như Linux. Nhưng tình soạn thảo vi là trình soạn thảo được nhiều chuyên gia UNIX và Linux sử dụng. Có lẽ một phần là nó chiếm ít tài nguyên, nhưng cũng có thể là nó tồn tại trong UNIX và Linux từ ngày bắt đầu và không có một hệ UNIX hay Linux nào mà không chứa nó như một trình soạn thảo mặc định.</p>
<p>Ngồi vào máy bạn tạo, bạn tạo ra dữ liệu, thư điện tử, danh sách, bản ghi nhớ, ghi chú, báo cáo&#8230;vv. Bạn sẽ dùng một bộ soạn thảo(còn gọi là bộ chỉnh sửa, hoặc hiệu chỉnh) văn bản nào đó để làm công việc vừa kể. Linux có sẵn nhiều chương trình xử lý hoặc hiệu chỉnh văn bản để giúp bạn như: nano, cat, touch&#8230; Tuy nhiên để đưa các câu lệnh và trình shell vào một tập tin, bạn cần có một bộ soạn thảo (editor) có khả năng lưu trữ công việc của mình vào một tập tin văn bản dưới dạng mã ASCII. Linux giới thiệu bộ soạn thảo văn bản mang tên vi , mà bạn có thể dùng để hiệu chỉnh hầu như tất cả mọi thứ. vi rất hữu ích cho quản trị hệ thống vì chương trình này có sẵn trên mọi nền UNIX. Do đó khi đã quen tay với vi rồi, bạn tha hồ thao tác mọi hệ thống chạy UNIX. Ngoài ra vi cũng có lợi vì chiếm rất ít tài nguyên khi thi hành lệnh. Trong khi một số chương trình khác không thể chạy được vì xung đột phần cứng hoặc là vì hệ thống, vi vẫn hoạt động bình thường.</p>
<p> Hai bộ vi và ex đi kèm với bản phát hành Red Hat thực ra là danh xưng khác nhau của bộ soạn thảo vim (VI iMproved). Hai tên vi và ex gắn liền với vim, do đó khi chạy vi , thực ra bạn đang chạy vim. Mời bạn xem tập tin /usr/share/vim/vim61/doc/vi_diff.txt hay /usr/share/doc/vim-common-6.1/doc/vi_diff.txt để biết tóm tắt những điểm khác nhau giữa vi và vim.</p>
<div><img src="http://forum.pnet.vn/images/imagespnet/soanthao_vi.gif" border="0" alt="" />
<p>Hình 1.1 Soạn thảo với vi</p>
</div>
<p>Linux có những bộ soạn thảo văn bản khác: một chương trình dạng đồ họa để dùng với XFree86 và hai bộ chuẩn không đồ họa mang tên ed và ex. Cả hai đều mang tính line-oriented, nghĩa là bạn chỉ có thể làm việc từng hàng một. Một chương trình khác mang tên emacs cũng có mặt trong các bản phát hành Linux. vi và emacs là những bộ soạn thảo toàn màn hình. Thông tin xuất hiện theo từng màn hình một để bạn có thể chỉnh sửa, thêm theo ngữ cảnh.</p>
<p> Trong bài này không bàn nhiều về ed hay ex, bởi vì bạn sẽ thấy vi rất dễ dùng và có sẵn trên mọi máy chạy UNIX, bao gồm cả Linux.</p>
<p>Muốn hiểu vi, bạn cần tìm hiểu vài nét về vi trong thế giới UNIX. Cho dù nhiều hệ điều hành ngày nay đã có những bộ soạn thảo mạnh mẽ và thân thiện với người sử dụng ví dụ như: Open Offices&#8230;, nhưng bạn vẫn nên tìm hiểu về vi bởi vì bất kỳ bản UNIX nào cũng có sẵn bộ vi. Đôi lúc vi là bộ soạn thảo duy nhất sẵn có vào lúc cần thiết, vì vậy bạn cần biết vài thao tác căn bản. Hơn nữa giờ đây với những phiên bản mới (v.6.x) vi đã trở nên thân thiện hơn và đã có nhiều phiên bản chạy trên Windows.</p>
<p>Trước đây UNIX được triển khai trong hoàn cảnh terminal của người sử dụng là máy teletype hoặc một dạng máy in kết quả ra giấy chạy chậm rì. Vào thời đó màn hình chưa được dùng nhiều. Bộ soạn thảo văn bản dùng cho môi trường như thế thường là line-oriented, mà người sử dụng chỉ thấy và chỉ làm việc trên một dòng lệnh duy nhất mà thôi.</p>
<p>Hiện nay các hệ UNIX còn dùng hai bộ soạn thảo line-oriented: ed và ex.</p>
<p> Khi UNIX bắt đầu ra đời, sinh viên đại học được dùng UNIX không mất tiền và cùng góp sức triển khai hệ điều hành này. Đại học California và Berkeley có nhiều đóng góp, trong đó có bộ soạn thảo toàn màn hình, thay thế việc xử lý từng dòng lệnh một. Bộ soạn thảo toàn màn hình này được gọi là vi, viết tắt của chữ &#8220;Visual&#8221;.</p>
<p>Đã đến lúc chuyển sang loại hình công việc screen-oriented, nhìn cả màn hình thay vì từng dòng một. Người sử dụng làm việc với các terminal nhìn trực tiếp vào màn hình, thay vì làm việc với các thiết bị in ra giấy.</p>
<p>Trong phần này chỉ giới thiệu đôi chút về vi, muốn biết vi hoạt động và sử dụng như thế nào mời các bạn đón đọc tiếp phần II &#8211; vi là gì?</p>
<p><strong>II &#8211; Vi là gì ?:</strong></p>
<p> Trong phần trước chúng ta đã được giới thiệu về VI và biết được sự ra đời cũng như tầm quan trọng và sự hữu ích của VI. Phần này các bạn sẽ hiểu được sự hoạt động của VI.</p>
<p> Cũng như hàng triệu người khác đang sử dụng vi, bạn sẽ thấy ứng dụng này khởi động nhanh và có thể dùng cho việc đơn giản cũng như phức tạp. Bạn dùng vi để nhập văn bản, chỉnh sửa hoặc xóa bỏ văn bản. Ngoài ra bạn còn tìm kiếm và thay thế, cắt, chép, và dán từng khối văn bản.</p>
<p>Vi cũng giúp bạn cắt xén tỉa gọt văn bản cho đúng ý mình. Bạn tùy nghi di chuyển con trỏ đến bất kỳ chỗ nào trên màn hình cũng như trên phạm vi văn bản đang được xử lý. Bộ soạn thảo vi không phải là phần mềm xử lý văn bản, cũng không là ứng dụng chế bản văn phòng, do đó sẽ không có trình đơn (menu) và cũng không có các tiện ích trợ giúp.</p>
<p>Các ứng dụng xử lý văn bản thường có hai định dạng, một loại trên màn hình và một dùng khi in ra, chẳng hạn như trình bày văn bản dạng in đậm, in nghiêng, hoặc gạch dưới. Vi lại không như thế. Các lệnh khác của Linux có thể thực thi vài điều vừa kể, chẳng hạn như lệnh lp thực hành in và lệnh nroff có khả năng định dạng văn bản. Một vài chương trình xử lý văn bản như TeX và LaTeX, có thể xử lý các câu lệnh nhúng trong văn bản, chẳng hạn như in đậm hoặc gạch dưới.</p>
<p>Bộ soạn thảo vi chạy ở hai chế độ khác nhau:</p>
<p>- Ở chế độ câu lệnh, những gì bạn gõ vào sẽ được hiểu như là câu lệnh ra lệnh cho vi. Lệnh sẽ bảo vi lưu tập tin, thoát khỏi vi, chuyển con trỏ đến các vị trí khác nhau trong tập tin, chỉnh sửa, sắp xếp, xóa bỏ, thay thế và tìm kiếm đoạn văn bản.</p>
<p> &#8211; Ở chế độ nhập liệu hoặc còn gọi là nhập văn bản (chế độ INSERT), những gì bạn gõ vào được máy hiểu là nội dung của tập tin mà bạn đang chỉnh sửa. Theo chế độ này, vi hành động như là chiếc máy đánh chữ đơn thuần.</p>
<p>Trong một phiên chỉnh sửa văn bản, bạn có thể chuyển từ chế độ này sang chế độ khác. Bạn phải nhớ mình đang ở chế độ nào và biết cách chuyển đổi. Chương này sẽ giới thiệu tùy chọn showmode giúp bạn biết xem vi đang thuộc chế độ nào. Chỉ cần tập luyện ít lâu là bạn sẽ thấy vi rất tiện lợi để xử lý các tập tin ASCII Linux, đặc biệt là các tập tin cấu hình và shell script.</p>
<p>Để biết được trình soạn thảo vi làm việc như nào và bạn có thể gỡ lỗi (debug error) xảy ra trong quá trình dùng vi thì mời các bạn đón đọc phần tiếp theo: Tìm hiểu tiến trình soạn thảo Vi &#8211; Phần III.</p>
<p><strong>III &#8211; Tìm hiểu tiến trình soạn thảo và cách sử dụng Vi:</strong></p>
<p> Bạn soạn thảo bằng cách tạo ra văn bản mới hoặc chỉnh sửa văn bản đã có sẵn. Tạo ra văn bản mới tức là viết chữ vào một tập tin được đặt tên theo dạng Linux. Khi chỉnh sửa, bạn dùng tên tập tin có sẵn để gọi bản sao của tập tin ấy ra trình diện tại phiên soạn thảo. Trong cả hai trường hợp, khi sử dụng bộ soạn thảo tức là bạn đã lưu giữ văn bản tại bộ nhớ của máy ở một vùng được gọi là vùng đệm.</p>
<p> Vùng đệm sẽ không thay đổi gì ở nội dung của tập tin cho đến khi bạn ra lệnh lưu nội dung vùng đệm. Chỉ khi nào bạn ra lệnh này xong, máy sẽ ghi những điều chỉnh sửa vào đĩa cứng. Lúc ấy những gì bạn vừa thay đổi mới có hiệu lực thường trực. Do đó nếu sau khi thao tác một lúc, bạn quyết định là không nên thay đổi gì ở nội dung cũ thì cứ để y nguyên như thế. Trong một phiên làm việc, bạn muốn lưu những thay đổi ấy bao nhiêu lần cũng được, và bạn nên ra lệnh lưu một cách đều đặn, phòng khi mất nguồn đột xuất hoặc treo máy. Khi ra lệnh lưu, bạn không phải thoát khỏi bộ soạn thảo. Chương này sẽ chỉ bạn nhiều cách thoát ra khỏi bộ soạn thảo.</p>
<p>Bộ soạn thảo vi được coi là có tính tương tác bởi vì chương trình sẽ giao lưu qua lại với bạn trong phiên làm việc. Vi liên lạc với bạn bằng cách hiển thị tình trạng phiên làm viêc, hoặc giả hiển thị những thông báo lỗi, hoặc đôi lúc trên màn hình không xuất hiện điều gì cả (đúng theo kiểu của Linux). Dòng cuối cùng trên màn hình, gọi là dòng trạng thái, mang những lời nhắn của Linux.</p>
<p>Bạn sử dụng bộ soạn thảo để chỉnh sửa, sắp xếp lại, xóa bỏ, tìm kiếm và thay thế đoạn văn bản. Các thao tác chỉnh sửa như thế được tiến hành ở chế độ soạn thảo. Trong nhiều trường hợp, lệnh chỉ là một ký tự tương ứng với chữ đầu tiên của tên một hành động. Ví dụ chữ I tương ứng với hành động Insert, và r được dùng để replace( thay thế) một ký tự.</p>
<p>Hầu hết các câu lệnh đều diễn ra trên một dòng hoặc nhiều dòng ký tự. Dòng được đánh số từ 1(dòng đầu tiên) cho đến dòng cuối cùng của vùng đệm. Khi bạn thêm hoặc bớt dòng, số hiệu của dòng sẽ tự động chỉnh lý. Số hiệu của dòng tức là địa chỉ của dòng đó trong vùng đệm. Vùng địa chỉ là hai địa chỉ hoặc hai số hiệu của dòng được cách bởi dấu phẩy. Nếu muốn xác định vùng địa chỉ từ dòng thứ ba đến dòng thứ tám vủa vùng đệm, bạn viết 3, 8.</p>
<p>Vị trí con trỏ luôn chỉ bạn biết vị trí hiện hàn của vùng đệm soạn thảo. Một vài lệnh mà bạn gõ ở chế độ ra lệnh sẽ tác động đến ký tự ngay chỗ con trỏ đang nhấp nháy. Nếu bạn không di chuyển con trỏ, những thay đổi sẽ diễn ra tại vị trí ấy. Bộ soạn thảo vi có nhiều lệnh để di chuyển con trỏ tới lui trong vùng đệm soạn thảo. Vi là bộ soạn thảo toàn màn hình. Bạn ra lệnh cho Vi để di chuyển con trỏ đến nhiều nơi trong tập tin, và bạn trực tiếp nhìn thấy những thay đổi diễn ra khi bạn thao tác. Vì thế Vi phải có khả năng di chuyển và chỉnh sửa văn bản trên terminal của chính bạn cũng như ở các loại terminal khác. Vi biết bạn đang dùng terminal nào, và khả năng màn hình của terminal ấy ra sao bằng cách xem xét biến shell mang tên TERM. Linux dùng biến TERM để xác định xem terminal làm được gì, chẳng hạn như gạch dưới, đảo màn hình, xóa màn hình, gán phím chức năng, và khả năng về màu sắc của terminal.</p>
<p>Bạn chỉ cần gõ Vi tại dấu nhắc shell tại dòng lệnh để kích hoạt Vi. Muốn tạo ra hoặc chỉnh sửa một tập tin nào đó, bạn gõ lệnh Vi chung với tên tập tin. Chẳng hạn muốn tạo tập tin abc với Vi, bạn gõ: Vi abc.</p>
<p>Khi Vi được kích hoạt, màn hình terminal sẽ tự động xóa trống, đồng thời một dấu sóng (~) xuất hiện ở phía bên trái mỗi đầu dòng, trừ dòng đầu tiên hay những dòng đã soạn thảo. Dấu sóng báo hiệu dòng trống của vùng đệm. Con trỏ nằm phía ngoài cùng bên trái của dòng đầu tiên. Bạn sẽ trông thấy từ 20 đến 22 dấu sóng ở phía trái màn hình. Nếu màn hình không đúng như thế, bạn nên kiểm tra lại vị trí của TERM. Còn nếu mọi sự diễn ra như mô tả, bạn đã khởi động được Vi, và Vi đang ở chế độ chờ lệnh.Để có thể thao tác hoặc sử dụng Vi một cách hiệu quả nhất, mời các bạn đón đọc phần tiếp theo: IV &#8211; Tìm hiểu hai chế độ của Vi và tạo ra tập tin Vi đầu tiên.</p>
<p><strong>IV &#8211; Tìm hiểu hai chế độ của Vi và tạo ra tập tin Vi đầu tiên</strong></p>
<p>Vi ( vim ) &#8211; the powerful &amp; simple editor for linux</p>
<p> Linux là một hệ điều hành được điều khiển đa phần qua môi trường dòng lệnh ( console ), thêm vào đó, các file cấu hình dịch vụ trên Linux thường được chỉnh sửa bởi trình editor là vi ( hay vim, bản cải thiện của vi ). Nếu bạn là người thường xuyên quản trị server linux qua console, thì vi là trình tiện ích hay sử dụng nhất. Do vậy, học cách sử dụng vi là một yêu cầu tối thiểu và cần thiết cho một người sử dụng linux.</p>
<p> <strong>1. Cơ bản</strong></p>
<p>- Sử dụng vi kèm theo tên file(s) muốn edit: <span style="color:#0000ff;">vi one.txt two.txt etc.txt</span></p>
<p> &#8211; Có 2 mode: command mode và insert mode. Khi bắt đầu sử dụng lệnh vi, vi mặc định ở command mode. Hoặc ấn Esc để chuyển sang command mode khi người dùng đang ở insert mode.</p>
<p><strong>2. Các lệnh edit cơ bản</strong></p>
<p><span style="color:#0000ff;">- Ấn phím lệnh i hoặc a <span style="color:#000000;">từ chế độ command mode để chuyển sang </span>insert mode. i để thêm ký tự trước con trỏ, a để thêm ký tự sau con trỏ.</p>
<p> &#8211; Sử dụng lệnh h j k l <span style="color:#000000;">hoặc các phím mũi tên tương ứng để di chuyển con trỏ sang trái, xuống, lên, sang phải.</span></p>
<p> &#8211; Sử dụng x xóa 1 ký tự, dw xóa 1 từ, dd xóa cả 1 dòng.</p>
<p> &#8211; <span style="color:#000000;">Sử dụng số N đi trước phím lệnh để lặp lại N lần tác dụng của lệnh.</span> Ví dụ, 3dw sẽ xóa 3 từ tính từ vị trí con trỏ.</p>
<p> &#8211; Sử dụng u ( = undo )<span style="color:#000000;">để khôi phục lại những thay đổi trước đó.</span></p>
<p> &#8211; Sử dụng ZZ hay  hay :wq lưu lại tất cả thay đổi và thoát.</p>
<p> &#8211; Sử dụng :q! <span style="color:#000000;">thoát ra không lưu lại bất kỳ thay đổi nào.</span></span></p>
<p><strong>3. Cắt và dán</strong></p>
<p><span style="color:#0000ff;">- yy sao chép dòng hiện tại vào buffer, Nyy sao chép N dòng.</p>
<p> &#8211; p ( P ) dán nội dung từ buffer vào dưới ( trên ) dòng hiện tại</span></p>
<p><strong>4. Nhảy đến hàng hay cột</strong></p>
<p><span style="color:#0000ff;">- Gõ một số N trước ký tự G để đi đến dòng thứ N, vd 23G sẽ nhảy đến dòng 23.</p>
<p> &#8211; Gõ một số N trước ký tự | (pipe) để nhảy đến cột thứ N.</span></p>
<p><strong>5. </strong></p>
<p>Sử dụng . để lặp lại action gần nhất. Ví dụ người dùng gõ i để insert dòng chữ “hello world”, sau đó chuyển sang chế độ command mode bằng phím Esc, nhảy xuống dòng và gõ . , dòng chữ “hello world” sẽ hiện ra.</p>
<p><strong>6. Tìm kiếm</strong></p>
<p>- Sử dụng <span style="color:#0000ff;">/ (?)</span> đi theo sau là từ muốn tìm để tìm kiếm từ trong phần văn bản sau ( trước ) con trỏ. <span style="color:#0000ff;">ví dụ /foobar hay ?foobar.</span></p>
<p> &#8211; Sau khi kết quả tìm kiếm đầu tiên hiện ra, sử dụng n để tìm kiếm tiếp trong phần văn bản còn lại sau con trỏ, N để tìm kiếm ngược trở lại đầu văn bản trước con trỏ.</p>
<p>7. Các lệnh colon ( đi sau dấu : )</p>
<p>- <span style="color:#0000ff;">:%s/foo/bar/g</span> tìm sự xuất hiện của “foo” trong toàn bộ file và thay thế bằng “bar”,  /foo/bar/g chỉ thay thế ở dòng hiện tại.</p>
<p> &#8211;  et nu hiển thị số dòng trước mỗi dòng,  et nonu để bỏ hiển thị số dòng.</p>
<p> &#8211; :1,8d xóa từ dòng 1 cho đến dòng 8 trong file.</p>
<p> &#8211; Sử dụng ma để đánh dấu dòng hiện tại là a ( có thể là bất cứ ký tự nào từ a-z ). Sau đó dùng ‘a để nhảy đến dòng đã được dánh dấu là a từ bất cứ đâu. Có thể sử dụng với colon, :’a,’b d xóa tất cả các dòng bắt đầu từ dòng được đánh dấu là a cho đến dòng được đánh dấu là b, hoặc ngược lại.</p>
<p> &#8211; <span style="color:#0000ff;">:w newfile.txt</span> để save nội dung của file hiện tại vào một file mới là newfile.txt ( tựa “save as” bên Win Word ).</p>
<p> &#8211; <span style="color:#0000ff;">:8,16 co 32</span> để copy dòng 8 đến 16 đến điểm sau dòng 32.</p>
<p> &#8211; <span style="color:#0000ff;">:3,16 m 32</span> để chuyển rời dòng 8 đến 16 đến điểm sau dòng 32.</p>
<p> &#8211; Nếu dùng vi để mở nhiều file ( vi file1 file2 file3 ), có thể sử dụng :n để nhảy đến file tiếp theo và :rew để nhảy quay ngược lại đến file đầu tiên, :args để hiện thị tất cả các file đang được mở.</p>
<p>8. Vi for Smarties</p>
<p>-<span style="color:#0000ff;"> Sử dụng G để nhảy đến dòng cuối cùng của file.</span></p>
<p> &#8211; Khi xóa nhiều dòng, di chuyển con chuột đến dòng đầu tiên, gõ ma để đánh dấu, sau di đến dòng cuối cùng và gõ d’a để xóa những dòng đó.</p>
<p> &#8211; <span style="color:#0000ff;">$</span> để nhảy xuống cuối dòng, :$ để nhảy đến dòng cuối của file.</p>
<p> <span style="color:#0000ff;">- 0</span> để nhảy đến đầu dòng, :0 để nhảy tới dòng đầu tiên của file.</p>
<p> &#8211; <span style="color:#0000ff;">d$</span> xóa từ vị trí con trỏ hiện tại đến cuối dòng.</p>
<p> &#8211; :<span style="color:#0000ff;">10,$ d</span> xóa từ dòng 10 cho đến dòng cuối cùng của file, hoặc ngược lại.</p>
<p> &#8211; <span style="color:#0000ff;">:10,20 m 0</span> chuyển rời dòng 10 đến 20 lên trên dòng đầu tiên của file.</p>
<p>9. Shell</p>
<p>- Sử dụng :!command để thi hành lệnh command trong môi trường vi.</p>
<p>Trên đây là giới thiệu cách sử dụng vi một cách cơ bản và đủ dùng cho mọi đối tượng người sử dụng linux. Người dùng muốn tham khảo thêm có thể lên website của vim tại<a href="http://vim.wikia.com/wiki/Best_Vim_Tips"> http://vim.wikia.com/wiki/Best_Vim_Tips</a>.</p>
<p>Trích từ Internet</p>
<br />Posted in Guide software, Linux  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=40&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/10/20/s%e1%bb%ad-d%e1%bb%a5ng-b%e1%bb%99-so%e1%ba%a1n-th%e1%ba%a3o-vi-trong-unix-va-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>

		<media:content url="http://forum.pnet.vn/images/imagespnet/soanthao_vi.gif" medium="image" />
	</item>
		<item>
		<title>Understanding Red Hat Run Levels</title>
		<link>http://kokono0204.wordpress.com/2009/10/16/understanding-red-hat-run-levels/</link>
		<comments>http://kokono0204.wordpress.com/2009/10/16/understanding-red-hat-run-levels/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 14:50:00 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/?p=36</guid>
		<description><![CDATA[Với mỗi runlevel các đoạn scripts được chạy để khởi động từng dịch vụ riêng biệt, thay vì một số lượng lớn các file phải chỉnh sửa bằng tay. Những đoạn scripts này được đặt trong thư mục /etc/rc.d/init.d, và phần lớn đều có một tuỳ chọn start hoặc stop. Thiết lập này sẽ bao [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=36&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Với mỗi runlevel các đoạn scripts được chạy để khởi động từng dịch vụ riêng biệt, thay vì một số lượng lớn các file phải chỉnh sửa bằng tay. Những đoạn scripts này được đặt trong thư mục <strong><span style="color:orange;">/etc/rc.d/init.d</span></strong>, và phần lớn đều có một tuỳ chọn <strong><span style="color:orange;">start</span></strong> hoặc <strong><span style="color:orange;">stop</span></strong>. Thiết lập này sẽ bao gồm một chùm các thư mục trong <strong><span style="color:orange;">/etc/rc.d</span></strong>. Đó là:<br />
<strong><span style="color:orange;">rc0.d</span></strong>: chứa đựng những đoạn script được chạy khi hệ thống shutdown<br />
<strong><span style="color:orange;">rc1.d</span></strong> đến <strong><span style="color:orange;">rc3.d</span></strong>: chứa những đoạn scripts được chạy khi hệ thống thay đổi runlevel. Runlevel 1 thường được sử dụng cho chế độ người dùng đơn. Runlevel 2 cho thiết lập đa người dùng không có NFS. Runlevel 3 cho thiết lập đa người dùng đầy đủ và môi trường mạng.<br />
<strong><span style="color:orange;">Runlevel 4</span></strong> thường không được dùng<br />
<strong><span style="color:orange;">Runlevel 5</span></strong> chứa những đoạn script để khởi tạo hệ thống trong chế độ X11, nó gần giống runlevel 3 ngoại trừ là chương trình xdm được khởi động với một màn hình login đồ hoạ<br />
<strong><span style="color:orange;">Runlevel 6</span></strong>: chứa những đoạn scripts được chạy khi hệ thống reboot. Những đoạn scripts này được gọi bởi lệnh reboot<br />
init.d: thường chứa tất cả các scripts. Những file này nằm trong thư mục <strong><span style="color:orange;">rc?.d</span></strong> là những đường link thật sự đến thư mục <strong><span style="color:orange;">init.d</span></strong></p>
<p><span style="color:#ff0000;"><strong>The boot sequence</strong></span></p>
<p>Hãy xem những gì sẽ xảy ra trong một trình tự boot Red Hat thông thường.<br />
Khi hệ thống khởi động, file <strong><span style="color:orange;">/etc/rc.d/rc.sysinit</span></strong> được chạy đầu tiên. Runlevel khởi động nằm trong <strong><span style="color:orange;">/etc/inittab</span></strong> được tìm thấy và các <strong><span style="color:orange;">/etc/rc.d/rc script</span></strong> được chạy. Trong hầu hết các trường hợp đó là Runlevel 3.</p>
<p>Chương trình rc tìm thấy trong thư mục <strong><span style="color:orange;">/etc/rc.d/rc3.d</span></strong> sẽ thi hành bất cứ <strong><span style="color:orange;">K* script nào với tuỳ chọn stop</span></strong>. Sau đó <strong><span style="color:orange;">tất cả các S* scripts được khởi động với tuỳ chọn start</span></strong>. Các script được khởi động theo thứ bậc giá trị, ví dụ <strong><span style="color:orange;">S10network</span></strong> script được khởi động trước <strong><span style="color:orange;">S85httpd</span></strong> script. Điều này cho phép bạn lưạ chọn chính xác khi nào những đoạn scripts của bạn được chạy, mà không cần phải chỉnh sửa file. Điều tương tự cũng đúng với K* script.</p>
<p>Hãy xem những gì xảy ra khi chúng ta thực hiện chuyển runlevels chẳng hạn từ runlevel 3 (networking và chế độ đa người dùng) sang runlevel 1 (chế độ đơn người dùng).<br />
Đầu tiên tất cả các K* scripts thuộc level của hệ thống đang thay đổi, được thi hành. Red Hat 2.0 cài đặt 7 K* script và 1 S* script trong thư mục /etc/rc.d/rc.1. K* script sẽ shutdown nfs, send mail, lpd, inet, cron, và syslog. Sau đó S* script sẽ kills off bất cứ chương trình còn lại nào và thi hành lệnh:<br />
<strong>Code:</strong></p>
<div>
<pre> init -t1 S</pre>
</div>
<p>để đưa hệ thống về chế độ đơn người dùng. Trong chế độ này bạn có thể chuyển ngược trở lại chế độ đa người dùng bằng cách gõ:<br />
<strong>Code:</strong></p>
<div>
<pre> init 3</pre>
</div>
<p><span style="color:#ff0000;"><strong>Side-stepping init</strong></span></p>
<p>Có 2 điểm chính cần bổ sung:<br />
Đầu tiên, bạn có thể lựa chọn script start hoặc stop, thậm chí khi nó không thuộc runlevel của bạn. Các scripts thi hành nằm trong thư mục: <strong><span style="color:orange;">/etc/rc.d/init.d</span></strong> với tuỳ chọn start hoặc stop sẽ khởi động hoặc dừng bất cứ chương trình hoặc dịch vụ nào mà nó kiểm soát. Điều này cho phép bạn tắt NFS từ runlevel 3 trong khi giữ tất cả các hệ thống khác vẫn hoạt động</p>
<p>Dừng NFS trong trường hợp này sẽ yêu cầu dừng 2 hệ thống: nfsfs, nfs. Nfsfs script sẽ mount hoặc unmount bất kỳ hệ thống file NFS-mounted nào trong <strong><span style="color:orange;">/etc/fstab</span></strong> của bạn. Còn sau đó nfs script sẽ tắt các tiến trình liên kết với NFS, trong trường hợp này là mountd và nfsd.<br />
Do đó thủ tục cho việc shutdown NFS sẽ là:<br />
<strong>Code:</strong></p>
<div>
<pre>  # /etc/rc.d/init.d/nfs start

  Starting NFS services: rpc.mountd rpc.nfsd

  # /etc/rc.d/init.d/nfsfs start

  Mounting remote filesystems.

  #
</pre>
</div>
<p>và khởi động NFS sẽ là:<br />
<strong>Code:</strong></p>
<div>
<pre>  # /etc/rc.d/init.d/nfs start

  Starting NFS services: rpc.mountd rpc.nfsd

  # /etc/rc.d/init.d/nfsfs start

  Mounting remote filesystems.

  #
</pre>
</div>
<p><span style="color:#ff0000;"><strong>Managing init Files</strong></span></p>
<p>Chẳng hạn, nếu bạn không muốn khởi động HTTP daemon, ngoài cách xoá file trong thư mục rc3.d, đơn giản bạn hãy đổi tên <strong><span style="color:orange;">/etc/rc.d/rc3.d/S85httpd</span></strong> thành bất cứ tên gì không bắt đầu bằng các chữ cái viết hoa “S” hoặc “K”. Sự lựa chọn tốt nhất cho bạn là đổi tên chúng thành bắt đầu bằng chữ cái viết thường “s” hoặc “k”. Cách này không chỉ làm cho các đoạn script không được khởi động, nó còn làm cho các file này xuất hiện sau khi bạn dùng lệnh “ls”.</p>
<p><strong><span style="color:red;">Một chú ý quan trọng ở đây</span></strong>: Hãy chắc chắn rằng bạn biết những đoạn script nào được chạy khi bạn disable chúng. Nếu chẳng hạn bạn disable S10network thì sẽ không có 1 dịch vụ mạng nào của bạn hoạt động. Đó là bởi vì S10network là một low number, những scripts khác phụ thuộc vào mạng phải được thi hành sau khi dịch vụ mạng khởi động</p>
<p>Nếu bạn muốn tạo những init process của riêng bạn để khởi động hoặc dừng. Đơn giản, hãy tạo một đoạn script với một tuỳ chọn start. Nếu đoạn script của bạn khởi động một tiến trình nền, thì bạn cũng nên có thêm tuỳ chọn stop cho nó.</p>
<p>Viết một lần và được đặt trong thư mục <strong><span style="color:orange;">/etc/rc.d/init.d</span></strong>. Hãy nói về những chương trình kiểm tra thời gian trên mạng 15 phút một lần, chúng được gọi là script “<strong><span style="color:orange;">netdate</span></strong>”. Bạn có thể tạo đường link trong thư mục này khi bạn muốn khởi động chúng. Nếu bạn muốn chương trình của bạn được chạy ở runlevel 3, hãy tạo link đến script của bạn từ <strong><span style="color:orange;">/etc/rc.d/rc3.d/S??netdate</span></strong>. Đưa vào vị trí 2 dấu hỏi chấm một con số không xung đột với phần còn lại của thư mục, chẳng hạn S55netdate.</p>
<p>Nếu bạn muốn dừng một tiến trình trong quá trình shutdown, hãy chắc chắn rằng đoạn script của bạn chấp nhận tuỳ chọn stop, sau đó hãy tạo đường link đến <strong><span style="color:orange;">/etc/rc.d/init.d/netdate</span></strong> từ <strong><span style="color:orange;">/etc/rc.d/rc0.d/K55netdate</span></strong>. Một lần nữa hãy chắc chắn rằng con số bạn sử dụng không được dùng bởi bất kỳ một hệ thống con nào khác để tránh sự hỗn loạn.</p>
<p>Bạn có thể test những thiết lập mới của bạn bằng cách dùng init 3. Từ những hệ thống con đang được chạy, chỉ có một cái sẽ khởi động chính là cái bạn đã thêm vào. Nếu lệnh init 3 bị treo, đoạn script của bạn không exit, bạn phải đặt một ký hiệu ở cuối dòng để đặt tiến trình vấn đề trên background. Bạn cũng có thể chạy những đoạn scripts này bằng tay từ thư mục <strong><span style="color:orange;">/etc/rc.d/init.d/</span></strong> .<br />
Bây giờ bạn đã biết cách thức các hệ thống con làm việc, bạn có thể dễ dàng thêm hoặc chỉnh sửa những hệ thống con đang tồn tại cho thiết lập Linux đặc thù của bạn.</p>
<br />Posted in Linux  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=36&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/10/16/understanding-red-hat-run-levels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>
	</item>
		<item>
		<title>Hướng dẫn sử dụng vmware xây dựng mô hình mạng</title>
		<link>http://kokono0204.wordpress.com/2009/10/16/h%c6%b0%e1%bb%9bng-d%e1%ba%abn-s%e1%bb%ad-d%e1%bb%a5ng-vmware-xay-d%e1%bb%b1ng-mo-hinh-m%e1%ba%a1ng/</link>
		<comments>http://kokono0204.wordpress.com/2009/10/16/h%c6%b0%e1%bb%9bng-d%e1%ba%abn-s%e1%bb%ad-d%e1%bb%a5ng-vmware-xay-d%e1%bb%b1ng-mo-hinh-m%e1%ba%a1ng/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 03:56:24 +0000</pubDate>
		<dc:creator>kokono0204</dc:creator>
				<category><![CDATA[Network]]></category>

		<guid isPermaLink="false">http://kokono0204.wordpress.com/2009/10/16/h%c6%b0%e1%bb%9bng-d%e1%ba%abn-s%e1%bb%ad-d%e1%bb%a5ng-vmware-xay-d%e1%bb%b1ng-mo-hinh-m%e1%ba%a1ng/</guid>
		<description><![CDATA[VMWare (Virtual Machine Software) là một trong những chương trình ào hóa hàng dầu hiện nay. Chương trình này giúp tạo ra máy ảo với phần cứng mô phỏng hoàn toàn các thiết bị phần cứng trên máy tính thật, cung cấp khả năng liên kết máy ảo trực tiếp với các thiết bị ngoại [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=35&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>VMWare (Virtual Machine Software) là một trong những chương trình ào hóa hàng dầu hiện nay. Chương trình này giúp tạo ra máy ảo với phần cứng mô phỏng hoàn toàn các thiết bị phần cứng trên máy tính thật, cung cấp khả năng liên kết máy ảo trực tiếp với các thiết bị ngoại vi, đặc biệt là các thiết bị lưu trữ di động. Chương trình là công cụ hữu ích cho các quản trị viên cần có môi trường ảo để nghiên cứu, thử nghiệm hay thiết kế mô hình mạng</p>
<p>Dưới đây, tôi sẽ hướng dẫn các bạn cài đặt, cấu hình máy ảo và thiết lập hệ thống mạng với phần mềm VMWare</p>
<p><span style="font-size:large;"><strong><span style="color:red;">A. Giới thiệu</span></strong></span></p>
<p>VMWare (Virtual Machine Software) là một trong những chương trình ào hóa hàng dầu hiện nay. Chương trình này giúp tạo ra máy ảo với phần cứng mô phỏng hoàn toàn các thiết bị phần cứng trên máy tính thật, cung cấp khả năng liên kết máy ảo trực tiếp với các thiết bị ngoại vi, đặc biệt là các thiết bị lưu trữ di động. Chương trình là công cụ hữu ích cho các quản trị viên cần có môi trường ảo để nghiên cứu, thử nghiệm hay thiết kế mô hình mạng</p>
<p>Dưới đây, tôi sẽ hướng dẫn các bạn cài đặt, cấu hình máy ảo và thiết lập hệ thống mạng với phần mềm VMWare</p>
<p><span style="font-size:large;"><span style="color:red;"><strong>B. Cài đặt và quản lý một số cấu hình cơ bản của VMWare</strong></span></span></p>
<p>Chương trình máy ảo tôi xài dưới đây là VMWare Workstation 6.5.0 build- 118166<br />
<img src="http://img198.imageshack.us/img198/7773/image001nxo.jpg" border="0" alt="" /></p>
<p>Các bạn có thể download phiên bản mới nhất ở đây <a href="http://www.vmware.com/download/ws/" target="_blank">http://www.vmware.com/download/ws/</a></p>
<p>Khi cài đặt, các thông số để mặc định.<br />
<img src="http://img25.imageshack.us/img25/6347/image002whu.jpg" border="0" alt="" /></p>
<p><img src="http://img76.imageshack.us/img76/39/image003geb.jpg" border="0" alt="" /></p>
<p>Khi cấu hình xong hệ thống yêu cầu Restart lại máy.<br />
<img src="http://img34.imageshack.us/img34/42/image004ijb.jpg" border="0" alt="" /></p>
<p>Sau khi cài đặt xong các bạn vào phần Network Connection kiểm tra có thêm 2 card mạng<br />
<img src="http://img36.imageshack.us/img36/2195/image005h.jpg" border="0" alt="" /></p>
<p>Card mạng Local Area Connection 2 và Local Area Connection 2 được nối vào VMnet1 và VMnet8 là 2 trong 10 Switch ảo mà VMWare hỗ trợ cho người sử dụng. Các Switch ảo này được dùng để kết nối máy thật với máy ảo, máy ảo với máy ảo nhằm tạo ra các hệ thống mạng riêng biệt. Để quản lý các thông số cấu hình, ta vào menu Edit&gt; Virtual Network Editor&gt;<br />
<img src="http://img192.imageshack.us/img192/8858/image007x.jpg" border="0" alt="" /></p>
<p>Tab Host Virtual Adapters<br />
<img src="http://img195.imageshack.us/img195/1668/image008n.jpg" border="0" alt="" /></p>
<p>Hay tab Host Virtual Network Mapping.<br />
<img src="http://img198.imageshack.us/img198/2079/image009lvp.jpg" border="0" alt="" /></p>
<p>Trong số 10 Switch ảo thì VMnet0, VMnet1, VMnet8 được kết nối với một vài thiết bị đặc biệt khác như Virtual Bridge, Virtual NAT Device, DHCP Server. Đặc điểm của các VMnet đó như sau:</p>
<p><strong>1. VMnet0</strong><br />
<img src="http://img198.imageshack.us/img198/3007/image010e.png" border="0" alt="" /></p>
<p>Trong VMWare, VMnet0 chính là một Switch ào được kết nối với Bridge ảo để liên lạc ra ngoài mạng thật. Các máy tính ảo nếu được cấu hình card mạng kết nối vào VMnet0 thì có thể tham gia vào việc liên lạc, chia sẻ dữ liệu với các máy thật ở mạng ngoài. Nếu mạng ngoài chúng ta có kết nối với Router ADSL, có cấu hình cấp IP động trên Router thì các máy ảo sẽ nhận được IP từ Router như một máy tính thật. Mặc định, VMWare sẽ tự chọn card mạng trên máy thật mà có kết nối ra mạng ngoài. Tuy nhiên nếu máy các bạn có nhiều hơn 2 card mạng thật thì chúng ta có thể lựa chọn lại cho phù hợp bằng cách điều chỉnh trong Virtual Network Editor &gt; tab Host Virtual Network Mapping&gt; VMnet0.</p>
<p><strong>2. VMnet1 (Host Only Mode) </strong><br />
<img src="http://img76.imageshack.us/img76/3997/image012.png" border="0" alt="" /></p>
<p>VMnet1 cũng là 1 Switch ảo nhưng chỉ sử dụng để kết nối máy thật và máy ảo trong máy tính cài VMWare. Bản thân VMnet1 được kết nối với Virtual DHCP Server để cấp IP động cho những máy kế nối vào nó. Giá trị mặc định của DHCP Scope là 192.168.18.0/24. Khi cài đặt xong VMWare thì card mạng Local Area Connection 2 do chương trình tạo ra được kết nối vào VMnet1 và có địa chỉ IP tĩnh cùng NetID với Scope. Các bạn có thể chỉnh sửa thông số Scope, gỡ bỏ DHCP Server được kết nối vào VMnet hiện tại hay thêm DHCP Server vào VMnet khác bằng cách vào Virtual Network Editor&gt; tab DHCP<br />
<img src="http://img36.imageshack.us/img36/4574/image014ojg.jpg" border="0" alt="" /></p>
<p>Trong tab này chúng ta có thể thêm DHCP kết nối vào VMnet khác (Add…), gỡ bỏ DHCP hiện có (Remove) hay dừng (Stop), khởi động (Start) dịch vụ DHCP. Chỉnh sửa thông số DHCP thông qua tab Properties<br />
<img src="http://img26.imageshack.us/img26/7459/image015bdo.jpg" border="0" alt="" /></p>
<p><strong>3. VMnet8 (NAT)</strong><br />
<img src="http://img192.imageshack.us/img192/6504/image016.png" border="0" alt="" /></p>
<p>VMnet 8 là Switch ảo vừa được nối vào Virtual DHCP lại được nối vào Virtual NAT Device giúp cho các máy ảo trong VMWare có thể liên lạc với mạng ngoài nhưng theo giao thức NAT. NAT Device ở đây là 1 thiết bị được quản lý bởi VMWare, có địa chỉ IP riêng chứ không phải giống như ta cấu hình ICS trên Windows là dùng máy thật như 1 NAT Device rồi NAT từ Virtual NIC (Adapter VMnet8) qua Physical NIC. Để quản lý DHCP Server thì bạn vào những phần tôi đã nêu ở trên VMnet1. Còn muốn quản lý cấu hình NAT Device thì bạn vào Virtual Network Editor&gt; tab NAT. Nhưng các bạn lưu ý, chúng ta chỉ được hỗ trợ 1 NAT Device nên chỉ có 1 VMnet được kết nối ra ngoài theo cơ chế NAT.<br />
<img src="http://img198.imageshack.us/img198/2342/image018.png" border="0" alt="" /></p>
<p><strong>4. VM Team và LAN Segment</strong></p>
<p>Ngoài cách cấu hình các máy ảo nối với nhau thông qua các VMnet, chúng ta còn có thể tạo ra các VM Team để nhóm các máy ảo lại với nhau rồi tạo ra LAN Segment để kết nối chúng lại. Với cách này ta không cần phải cho máy ảo nối vào các VMnet. Để tạo VM Team chúng ta vào Tab HOME&gt; New Team<br />
<img src="http://img259.imageshack.us/img259/9311/image020.png" border="0" alt="" /></p>
<p><img src="http://img93.imageshack.us/img93/5431/image022.png" border="0" alt="" /></p>
<p>Đặt tên và nơi lưu để quản lý<br />
<img src="http://img37.imageshack.us/img37/9715/image024.png" border="0" alt="" /></p>
<p>Đưa máy ảo vào. Ở bước này chúng ta có thể tạo máy ảo mới luôn hoặc là nhóm những máy ảo đã tạo sẵn.<br />
<img src="http://img195.imageshack.us/img195/1975/image026.png" border="0" alt="" /></p>
<p><img src="http://img198.imageshack.us/img198/9721/image028.png" border="0" alt="" /></p>
<p>Gắn các LAN Segment vào trong Team<br />
<img src="http://img259.imageshack.us/img259/9995/image030.png" border="0" alt="" /></p>
<p>Cấu hình kế nối với các LAN Segment<br />
<img src="http://img205.imageshack.us/img205/4269/image032.png" border="0" alt="" /></p>
<p>Để quản lý các VM Tam chúng ta vào menu Team&gt; Settings<br />
<img src="http://img13.imageshack.us/img13/6610/image034.png" border="0" alt="" /></p>
<p><img src="http://img197.imageshack.us/img197/4650/image036.png" border="0" alt="" /></p>
<p><strong><span style="color:red;"><span style="font-size:large;">C. Tạo máy ảo và cấu hình chức năng với VMWare Workstation</span></span></strong></p>
<p><strong>1. Tạo máy ảo với VMWare</strong></p>
<p>Trang HOME&gt; New Virtual Machine<br />
<img src="http://img200.imageshack.us/img200/5272/image038.png" border="0" alt="" /></p>
<p>Nếu các hệ điều hành bạn dự định cài đặt lên máy ảo thuộc loại phổ dụng thì bạn sẽ được VMWare hỗ trợ cài đặt không giám sát bằng cách chọn Typical rồi điền các thông số vào cần thiết vào trước<br />
<img src="http://img193.imageshack.us/img193/5189/image040.png" border="0" alt="" /></p>
<p>Số CD-Key<br />
<img src="http://img198.imageshack.us/img198/3446/image042.png" border="0" alt="" /></p>
<p>Nơi lưu<br />
<img src="http://img195.imageshack.us/img195/3197/image044.png" border="0" alt="" /></p>
<p>Định dung lượng ổ đĩa. Nếu dung lượng đĩa hơn 2 GB, bạn có thể lựa chọn lưu thành 1 file để dễ quản lý hay đưa ra thành cái file 2 GB để dễ di chuyển.<br />
<img src="http://img198.imageshack.us/img198/4791/image046.png" border="0" alt="" /></p>
<p><img src="http://img192.imageshack.us/img192/7391/image048.png" border="0" alt="" /></p>
<p>Trong trường hợp hệ điều hành các bạn sắp cài đặt không thuộc loại phổ dụng thì bạn nên chọn phần Custom rồi thiết lập các thông số cấu hình phù hợp cho máy ảo.</p>
<p>Với máy ảo VMWare, các phím thao tác được quy định như máy thật. Ví dụ muốn hiện của sổ Logon trong Windows Server 2003 trong máy ảo cũng nhấn Ctrl + Alt+ Del như máy thật chứ không bị hạn chế như Virtual PC là nhấn Alt phải + Del.</p>
<p><strong>2. Quản lý cấu hình phần cứng</strong></p>
<p>Để máy ảo hoạt động tối ưu hơn, sau khi cài đặt hệ điều hành cho máy ảo xong các bạn nên cài vào máy ảo bộ công cụ VMWare Tool. Khởi động máy ảo lên, chọn vào menu VM&gt; Install VMWare Tool<br />
<img src="http://img197.imageshack.us/img197/5354/image050.png" border="0" alt="" /></p>
<p>Cài đặt xong góc dưới bên tay phải của màn hình máy ảo sẽ có biểu tượng VMWare Tool. Các phần cứng đã được tối ưu dựa theo cấu hình phần cứng máy thật (card mạng tốc độ lên 1.0 Gbps)<br />
<img src="http://img195.imageshack.us/img195/8425/image052.png" border="0" alt="" /></p>
<p>Ngoài ra trong máy ảo, các bạn có thể chỉnh sửa cấu hình phần cứng như thêm card mạng, ổ đĩa cứng v.. v.. Nhưng trong quá trình chỉnh sửa bạn phải tắt máy ảo, trong cửa sổ máy ảo bạn chọn Edit Virtual machine settings<br />
<img src="http://img199.imageshack.us/img199/9154/image054.png" border="0" alt="" /></p>
<p>Giả sử trong trường hợp này ta muốn thêm card mạng. Trong cửa sổ Virtual Machine Settings&gt; tab Hardware&gt; Add<br />
<img src="http://img200.imageshack.us/img200/3940/image056.png" border="0" alt="" /></p>
<p>Chọn Network Adapter<br />
<img src="http://img198.imageshack.us/img198/7655/image058.png" border="0" alt="" /></p>
<p>Chọn VMnet muốn kết nối và Finish<br />
<img src="http://img198.imageshack.us/img198/9878/image060.png" border="0" alt="" /></p>
<p><strong>3. Nhân bản máy ảo</strong></p>
<p>Khi các bạn cần nhiều máy ảo để xây dựng mô hình mạng máy tính, mà các máy ảo cấu hình và hệ điều hành tương tự nhau thì các bạn có thể lựa chọn nhân bản máy ảo bạn đã cấu hình và cài đặt phần mềm đầy đủ thay vì phải cài đặt lại 1 máy ảo mới hoàn toàn. Để thực hiện các bạn chọn vào máy ảo đã có, click phải chọn Clone.<br />
<img src="http://img198.imageshack.us/img198/2397/image062.png" border="0" alt="" /></p>
<p>Các bạn chọn nơi đặt bản sao lưu. Máy nhân bản hoạt động như máy gốc, nhưng các bạn nhớ phải đổi computername và IP để tránh xung đột với máy gốc. trong trường hợp muốn join cả 2 máy vào domain thì các bạn phải đổi SID của máy nhân bản cho khác máy gốc.</p>
<p><strong>4. Tạo điểm phục hồi (Snapshoot)</strong></p>
<p>Trên máy thật, mỗi khi cần lưu lại thông số cấu hình hiện tại, ta phải sử dụng các chương trình sao lưu và phục hồi. Nhưng trong máy ảo ta có chức năng Snapshoot để tạo ra các điểm giúp phục hồi về trạng thái định trước mà không cần bất kì chương trình sao lưu nào.</p>
<p>Để tạo bản Snapshoot, trong cửa sổ máy ảo ta click phải, chọn Take Snapshoot.<br />
<img src="http://img198.imageshack.us/img198/5330/image064.png" border="0" alt="" /></p>
<p>Các bản Snap Shoot được quản lý trong Snapshoot Manager<br />
<img src="http://img197.imageshack.us/img197/7496/image066.png" border="0" alt="" /></p>
<p>Khi bạn muốn quay lại trạng thài cấu hình máy trước đó, chọn bản Snapshoot rồi click vào nút Go To.</p>
<p><strong><span style="color:red;"><span style="font-size:large;">D. Xây dựng mô hình mạng máy tính dựa trên các VMnet</span></span></strong></p>
<p>Chúng ta dùng chương trình máy ảo để dựng mô hình mạng sau:<br />
<img src="http://img199.imageshack.us/img199/5821/image068.png" border="0" alt="" /></p>
<p>Chúng ta sẽ cấu hình 2 máy giữa làm Router cho Client có thể truy cập tới Domain Controller. Chúng ta phải cấu hình cho máy làm Router có 2 card mạng, thiết lập địa chỉ cho các máy như sau:</p>
<p>Máy Client<br />
<img src="http://img195.imageshack.us/img195/6361/image070.png" border="0" alt="" /></p>
<p>Máy Router1<br />
<img src="http://img197.imageshack.us/img197/6818/image072.png" border="0" alt="" /></p>
<p>Máy Router2<br />
<img src="http://img261.imageshack.us/img261/7562/image074.png" border="0" alt="" /></p>
<p>Máy Domain Controller<br />
<img src="http://img25.imageshack.us/img25/4246/image076.png" border="0" alt="" /></p>
<p>Stop và disable dịch vụ Firewall trên các máy.</p>
<p>Triển khai Routing and Remote Access trên máy Router1: Start&gt; Programs&gt; Administrative Tools&gt; Routing and Remote Access&gt; Confirure and Enable Routing and Remote Access<br />
<img src="http://img190.imageshack.us/img190/1040/image078.png" border="0" alt="" /></p>
<p>Cửa sổ Welcome&gt; Next tới<br />
<img src="http://img177.imageshack.us/img177/2451/image080.png" border="0" alt="" /></p>
<p>Chọn Custom Configuration&gt; Next<br />
<img src="http://img23.imageshack.us/img23/9999/image082.png" border="0" alt="" /></p>
<p>Chọn LAN Routing<br />
<img src="http://img23.imageshack.us/img23/7966/image084.png" border="0" alt="" /></p>
<p>Và Next tới khi Finish. Dịch vụ Routing and Remote sẽ yêu cầu chúng ta Restart và sẽ bắt đầu hoạt động. Sau khi Enable xong chúng ta tạo Route định tuyến qua mạng bên kia. Chọn vào Static Route, click phải chọn New Static Route<br />
<img src="http://img36.imageshack.us/img36/9936/image086.png" border="0" alt="" /></p>
<p>Cấu hình các thông số như sau:<br />
<img src="http://img193.imageshack.us/img193/2720/image088.png" border="0" alt="" /></p>
<p>Tương tự chúng ta Enable Routing and Remote Access cho Router2. Sau đó cấu hình thông số Route trả về như sau:<br />
<img src="http://img197.imageshack.us/img197/1691/image090.png" border="0" alt="" /></p>
<p>Kiểm tra trên Client: Ping Domain Controller, dùng lệnh Nslookup phân giải DNS trên Domain Controller<br />
<img src="http://img193.imageshack.us/img193/9319/image092.png" border="0" alt="" /></p>
<p>Tương tự các bạn có thể triển khai nhiều mô hình khác với chương trình máy ảo VMWare</p>
<br />Posted in Network  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kokono0204.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kokono0204.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kokono0204.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kokono0204.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kokono0204.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kokono0204.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kokono0204.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kokono0204.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kokono0204.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kokono0204.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kokono0204.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kokono0204.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kokono0204.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kokono0204.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kokono0204.wordpress.com&amp;blog=9672481&amp;post=35&amp;subd=kokono0204&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kokono0204.wordpress.com/2009/10/16/h%c6%b0%e1%bb%9bng-d%e1%ba%abn-s%e1%bb%ad-d%e1%bb%a5ng-vmware-xay-d%e1%bb%b1ng-mo-hinh-m%e1%ba%a1ng/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a729376cf7edff690e81b8e26fcbbf2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kokono0204</media:title>
		</media:content>

		<media:content url="http://img198.imageshack.us/img198/7773/image001nxo.jpg" medium="image" />

		<media:content url="http://img25.imageshack.us/img25/6347/image002whu.jpg" medium="image" />

		<media:content url="http://img76.imageshack.us/img76/39/image003geb.jpg" medium="image" />

		<media:content url="http://img34.imageshack.us/img34/42/image004ijb.jpg" medium="image" />

		<media:content url="http://img36.imageshack.us/img36/2195/image005h.jpg" medium="image" />

		<media:content url="http://img192.imageshack.us/img192/8858/image007x.jpg" medium="image" />

		<media:content url="http://img195.imageshack.us/img195/1668/image008n.jpg" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/2079/image009lvp.jpg" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/3007/image010e.png" medium="image" />

		<media:content url="http://img76.imageshack.us/img76/3997/image012.png" medium="image" />

		<media:content url="http://img36.imageshack.us/img36/4574/image014ojg.jpg" medium="image" />

		<media:content url="http://img26.imageshack.us/img26/7459/image015bdo.jpg" medium="image" />

		<media:content url="http://img192.imageshack.us/img192/6504/image016.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/2342/image018.png" medium="image" />

		<media:content url="http://img259.imageshack.us/img259/9311/image020.png" medium="image" />

		<media:content url="http://img93.imageshack.us/img93/5431/image022.png" medium="image" />

		<media:content url="http://img37.imageshack.us/img37/9715/image024.png" medium="image" />

		<media:content url="http://img195.imageshack.us/img195/1975/image026.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/9721/image028.png" medium="image" />

		<media:content url="http://img259.imageshack.us/img259/9995/image030.png" medium="image" />

		<media:content url="http://img205.imageshack.us/img205/4269/image032.png" medium="image" />

		<media:content url="http://img13.imageshack.us/img13/6610/image034.png" medium="image" />

		<media:content url="http://img197.imageshack.us/img197/4650/image036.png" medium="image" />

		<media:content url="http://img200.imageshack.us/img200/5272/image038.png" medium="image" />

		<media:content url="http://img193.imageshack.us/img193/5189/image040.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/3446/image042.png" medium="image" />

		<media:content url="http://img195.imageshack.us/img195/3197/image044.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/4791/image046.png" medium="image" />

		<media:content url="http://img192.imageshack.us/img192/7391/image048.png" medium="image" />

		<media:content url="http://img197.imageshack.us/img197/5354/image050.png" medium="image" />

		<media:content url="http://img195.imageshack.us/img195/8425/image052.png" medium="image" />

		<media:content url="http://img199.imageshack.us/img199/9154/image054.png" medium="image" />

		<media:content url="http://img200.imageshack.us/img200/3940/image056.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/7655/image058.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/9878/image060.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/2397/image062.png" medium="image" />

		<media:content url="http://img198.imageshack.us/img198/5330/image064.png" medium="image" />

		<media:content url="http://img197.imageshack.us/img197/7496/image066.png" medium="image" />

		<media:content url="http://img199.imageshack.us/img199/5821/image068.png" medium="image" />

		<media:content url="http://img195.imageshack.us/img195/6361/image070.png" medium="image" />

		<media:content url="http://img197.imageshack.us/img197/6818/image072.png" medium="image" />

		<media:content url="http://img261.imageshack.us/img261/7562/image074.png" medium="image" />

		<media:content url="http://img25.imageshack.us/img25/4246/image076.png" medium="image" />

		<media:content url="http://img190.imageshack.us/img190/1040/image078.png" medium="image" />

		<media:content url="http://img177.imageshack.us/img177/2451/image080.png" medium="image" />

		<media:content url="http://img23.imageshack.us/img23/9999/image082.png" medium="image" />

		<media:content url="http://img23.imageshack.us/img23/7966/image084.png" medium="image" />

		<media:content url="http://img36.imageshack.us/img36/9936/image086.png" medium="image" />

		<media:content url="http://img193.imageshack.us/img193/2720/image088.png" medium="image" />

		<media:content url="http://img197.imageshack.us/img197/1691/image090.png" medium="image" />

		<media:content url="http://img193.imageshack.us/img193/9319/image092.png" medium="image" />
	</item>
	</channel>
</rss>
