Friday, March 14, 2008

Các lệnh Shell cơ bản trong Linux

Khi mở một shell, bạn cần đến tài khoản kích hoạt vào thư mục chủ (thông thường nằm trong /home/tên_người_dùng).

Chú ý rằng mỗi lệnh đều có nhiều tùy chọn riêng của mình. Để xem các tùy chọn cho một lệnh cụ thể, đơn giản bạn chỉ cần gõ "man " (trong đó: man là từ khóa; command là tên lệnh). Một điểm quan trọng cần chú ý là trong các hệ điều hành Linux, câu lệnh có phân biệt chữ hoa, chữ thường. “A” sẽ được hệ điều hành hiểu là khác so với “a”.

Để vào hệ thống file, dùng:

- pwd: đưa ra ngoài màn hình thư mục đang hoạt động (ví dụ: /etc/ssh).

- cd: thay đổi thư mục (ví dụ: cd .. – ra một cấp thư mục hiện tại; cd vidu – vào thư mục /vidu).

- ls: đưa ra danh sách nội dung thư mục.


- mkdir: tạo thư mục mới (mkdir tên_thumuc).

- touch: tạo file mới (touch ten_file).

- rmdir: bỏ một thư mục (rmdir ten_thumuc).

- cp: copy file hoặc thư mục (cp file_nguồn file_đích).

- mv: di chuyển file hoặc thư mục; cũng được dùng để đặt lại tên file hoặc thư mục (mv vị_trí_cũ vị_trí_mới hoặc mv tên_cũ tên_mới).

- rm: loại bỏ file (rm tên_file).

Để tìm kiếm file, bạn có thể dùng:

- find : dùng cho các tên file.

- grep <>: để tìm nội dung trong file.

Để xem một file, bạn có thể dùng:

- more : hiển thị file theo từng trang.

- cat <>: hiển thị tất cả file.

- head <>: hiển thị các dòng đầu tiên.

- tail <>: hiển thị các dòng cuối cùng (có thể hữu ích trong những trường hợp như khi bạn muốn xem thông tin cuối cùng của một file hệ thống).

Để chính sửa file, bạn phải sử dụng trình soạn thảo tích hợp sẵn trên dòng lệnh. Thông thường, đây là vi và được dùng với cú pháp: vi .

Để giải nén một lưu trữ (thông thường có đuôi tar.gz), bạn phải dùng lệnh tar với cú pháp tar -xvf .

Để in một file, dùng lệnh lpr . Chú ý là bạn phải có một số daemon hoạt động để quản lý máy in. Thông thường đây là các cup (chủ yếu là UNIX Printing System) có thể sử dụng cho tất cả các phân phối chính.

Để loại bỏ file khỏi hàng đợi ở máy in (bạn có thể lên danh sách hàng đợi bằng lệnh lpq), sử dụng câu lệnh lprm .

Để lắp hoặc gỡ bỏ thiết bị (thêm vào hệ thống file với vai trò như một phương tiện được phép sử dụng), dùng:

- mount /mnt/floppy: lắp thêm ổ mềm.

- umount /mnt/floppy: gỡ bỏ ổ mềm.

- mount /mnt/cdrom: lắp ổ CD-ROM.

- mount /mnt/cdrom: gỡ ổ DC-ROM.

Các thiết bị này thường được cài và cho phép sử dụng một cách tự động. Nhưng có thể một ngày đẹp trời nào đó bạn lại phải tự mình thực hiện công việc này khi có lỗi xảy ra. Đừng lo lắng!

Để tạo một phân vùng

Đầu tiên, tạo một thư mục trong /mnt (mkdir /mnt/ổ_đĩa_mới). Sau đó sử dụng lệnh mount (mount /dev/source /mnt/ ổ_đĩa_mới), trong đó /dev/source là thiết bị (tức phân vùng) bạn muốn lắp thêm vào hệ thống file.

Nếu muốn kết nối tới một host từ xa, sử dụng lệnh ssh. Cú pháp là ssh .

Quản lý hệ thống:

- ps: hiển thị các chương trình hiện thời đang chạy (rất hữu ích: ps là cái nhìn toàn bộ về tất cả các chương trình).

Trong danh sách đưa ra khi thực hiện lệnh ps, bạn sẽ thấy có số PID (Process identification - nhân dạng tiến trình).

Con số này sẽ được hỏi đến khi muốn ngừng một dịch vụ hay ứng dụng, dùng lệnh kill .

- top: hoạt động khá giống như Task Manager trong Windows. Nó đưa ra thông tin về tất cả tài nguyên hệ thống, các tiến trình đang chạy, tốc độ load trung bình… Lệnh top -d thiết lập khoảng thời gian làm tươi lại hệ thống. Bạn có thể đặt bất kỳ giá trị nào, từ .1 (tức 10 mili giây) tới 100 (tức 100 giây) hoặc thậm chí lớn hơn.

- uptime: thể hiện thời gian của hệ thống và tốc độ load trung bình trong khoảng thời gian đó, trước đây là 5 phút và 15 phút.

Thông thường tốc độ load trung bình được tính toán theo phần trăm tài nguyên hệ thống (vi xử lý, RAM, ổ cứng vào/ra, tốc độ load mạng) được dùng tại một thời điểm. Nếu tốc độ được tính toán là 0.37, tức có 37% tài nguyên được sử dụng. Giá trị lớn hơn như 2.35 nghĩa là hệ thống phải đợi một số dữ liệu, khi đó nó sẽ tính toán nhanh hơn 235% mà không gặp phải vấn đề gì. Nhưng giữa các phân phối có thể khác nhau một chút.

- free: hiển thị thông tin trên bộ nhớ hệ thống.

- ifconfig : để xem thông tin chi tiết về các giao diện mạng; thông thường giao diện mạng ethernet có tên là eth(). Bạn có thể cài đặt các thiết lập mạng như địa chỉ IP hoặc bằng cách dùng lệnh này (xem man ifconfig). Nếu có điều gì đó chưa chính xác, bạn có thể stop hoặc start (tức ngừng hoặc khởi_động) giao diện bằng cách dùng lệnh ifconfig up/down.

- passwd: cho phép bạn thay đổi mật khẩu (passwd người_dùng_sở_hữu_mật_khẩu hoặc tên người dùng khác nếu bạn đăng nhập hệ thống với vai trò root).

- useradd: cho phép bạn thêm người dùng mới (xem man useradd).

Dù ở phân phối nào, bạn cũng có thể dùng phím TAB để tự động hoàn chỉnh một lệnh hoặc tên file. Điều này rất hữu ích khi bạn quen với các lệnh. Bạn cũng có thể sử dụng các phím lên, xuống để cuộn xem các lệnh đã nhập. Bạn có thể dùng lệnh đa dòng trên một dòng. Ví dụ như, nếu muốn tạo ba thư mục chỉ trên một dòng, cú pháp có thể là: mkdir thư_mục_1 ; mkdir thư_mục_2 ; mkdir thư_mục_3.

Một điều thú vị khác nữa là các lệnh dạng pipe. Bạn có thể xuất một lệnh thông qua lệnh khác. Ví dụ: man mkdir | tail sẽ đưa ra thông tin các dòng cuối cùng trong trang xem "thủ công" của lệnh mkdir.

Nếu lúc nào đó được yêu cầu phải đăng nhập với tài khoản gốc (tức "siêu" admin của hệ thống), bạn có thể đăng nhập tạm thời bằng cách dùng lệnh su. Tham số -1 (su-1) dùng để thay đổi thư mục chủ và cho các lệnh đã hoặc đang dùng. Chú ý là bạn cũng sẽ được nhắc một mật khẩu.

Để thoát hay đóng shell, gõ exit hoặc logout.

T.Thu

Để website luôn online với cluster Apache High Availability Linux

Cluster sửa chữa lỗi (failover cluster) được dùng để đảm bảo tính sẵn sàng cho các dịch vụ và ứng dụng hệ thống khi bị tấn công, xử lý các lỗi phần cứng và rủi ro do môi trường. Trong bài này chúng tôi sẽ hướng dẫn các bạn cách thức thực hiện một cluster Apache hai nút, chắc chắn tin cậy và hiệu quả cao với ứng dụng thú vị của dự án The High-Availability Linux. Cluster này đã được kiểm tra trên các phân phối Fedora Core 5, CentOS 4.3, và Ubuntu 6.06.1 LTS server.

Trong môi trường cluster, hệ thống 'có tính sẵn sàng cao' (high ability - HA) chịu trách nhiệm bắt đầu và kết thúc các dịch vụ, cài đặt và gỡ bỏ tài nguyên, giám sát khả năng sẵn sàng của hệ thống trong môi trường cluster và điều khiển quyền sở hữu địa chỉ IP ảo chia sẻ giữa các nút cluster. Dịch vụ heartbeat (trung tâm) cung cấp các tính năng cơ sở cần thiết cho hệ thống HA.

Cấu hình cluster phổ biến nhất là standby, sẽ được mô tả dưới đây. Trong cấu hình cluster này, một nút thực hiện tất cả các việc, còn các nút khác ở trạng thái nghỉ ngơi. Heartbeat giám sát "sức khoẻ" của từng dịch vụ cụ thể, thông thường qua một giao diện Ethernet phân tách vốn chỉ dùng cho hệ thống HA sử dụng câu lệnh đặc biệt ping. Nếu vì một lý do nào đó, nút đang thực hiện bị hỏng, heartbeat sẽ chuyển tất cả thành phần HA sang nút khoẻ mạnh khác. Khi nút cũ phục hồi, nó có thể khôi phục lại tình trạng cũ trước đó của mình.

Cài đặt và cấu hình

Để kiểm tra hệ thống High Availability Linux, bạn cần một bộ điều hợp Ethernet thứ hai trên từng nút dành cho heartbeat. Cài đặt Web server Apache và chương trình heartbeat phải trên cả hai nút. Nếu gói heartbeat không nằm trong bất kỳ phần lưu trữ của các phân phối đã có, bạn có thể download tại đây. Trên server CentOS, tôi dùng yum để cài đặt phần mềm cần thiết:

yum install -y httpd heartbeat


Các file cấu hình cho hearbeat không nằm ở nơi phần mềm được cài đặt. Bạn cần copy chúng từ thư mục documentation tới thư mục /etc/ha.d/:

cp /usr/share/doc/heartbeat*/ha.cf /etc/ha.d/

cp /usr/share/doc/heartbeat*/haresources /etc/ha.d/

cp /usr/share/doc/heartbeat*/authkeys /etc/ha.d/

Trong file /etc/hosts bạn phải bổ sung thêm tên hostname và địa chỉ IP để hai nút có thể giao tiếp được với nhau. Trong trường hợp của tôi sẽ như thế này:


192.168.1.1 node1.example.com node1

192.168.1.2 node2.example.com node2


Phải đảm bảo file /etc/hosts là giống nhau ở cả hai nút. Sau đó ping chúng, copy file từ nút này tới nút khác, sử dụng câu lệnh copy an toàn:


scp /etc/hosts root@node2:/etc/

Tiếp theo, chỉnh sửa file cấu hình /etc/ha.d/ha.cf theo các điểm vào để heartbeat có thể hoạt động:

logfile /var/log/ha-log # chỗ để log mọi thứ từ heartbeat

logfacility local0 # tiện ích sử dụng cho syslog hoặc logger

keepalive 2 # thời gian giữa các heartbeat

deadtime 30 # thời gian đến khi host được đưa ra 'chết'

warntime 10 # thời gian trước khi cung cấp cảnh báo chậm trễ "late heartbeat".

initdead 120 # thời gian chết đầu tiên (initdead)

udpport 694 # cổng udp cho truyền thông bcast hoặc ucast

bcast eth1 # giao diện quảng bá (broadcast)

ucast eth1 10.0.0.1 # cluster 2 nút, vì thế không cần dùng dạng đa quảng bá (multicast)

auto_failback on # tự động gửi tài nguyên sai trở lại nút chính

node node1.example.com # tên của nút đầu tiên

node node2.example.com # tên của nút thứ hai

Đây là các tuỳ chọn cơ sở cần thiết để heartbeat hoạt động. File này phải được cấu hình giống hệt nhau trên cả hai nút, ngoại trừ phần "ucast" (nơi để địa chỉ IP của hàng để gửi các gói tới).

File tiếp theo là /etc/ha.d/haresources. Trong file này bạn cần định nghĩa tên nút chính, địa chỉ IP ảo (cluster IP) và tài nguyên dùng để bắt đầu. Ở trường hợp của chúng ta thì đó là Web server Apache.

Chỉ cần một dòng dữ liệu ở đây:

node1.example.com 192.168.1.5 httpd


Hãy chắc chắn rằng file này giống nhau hoàn toàn trên cả hai nút. Chú ý tên tài nguyên là tên script khởi tạo đặt trong thư mục /etc/init.d. Nếu tên tài nguyên trong /etc/init.d không hoàn toàn giống nhau, heartbeat sẽ không thể tìm thấy khi cố gắng đọc nó và cả Apache lẫn hearbeat đều không thể khởi động được.

File liên quan đến heartbeat cuối cùng là /etc/ha.d/authkeys. File này cũng phải hoàn toàn giống nhau trên cả hai nút và chỉ được đọc hoặc ghi bởi người dùng root. Nếu quyền hạn bị thiết lập khác đi, heartbeat sẽ từ chối khởi động. Bạn cần phải cấu hình file như thế này:

auth 1

1 crc

và giới hạn quyền đọc hoặc ghi chỉ dành cho người dùng root:

chmod 600 /etc/ha.d/authkeys

Bây giờ là cấu hình Apache service. Chúng ta muốn Apache nghe địa chỉ IP ảo 192.168.1.5 và cần trỏ thư mục Apache gốc tới điểm cài đặt dữ liệu /data, nơi các file Web được lưu lại. Chú ý là nơi lưu trữ dành cho Apache có thể là một nơi cụ thể từ thư mục file hệ thống cục bộ tới mạng khu vực lưu trữ. Tất nhiên nếu dữ liệu trong cả hai nút không giống nhau thì sẽ chẳng có điểm nào trong cluster failover cả. Nếu bạn không có thiết bị lưu trữ mạng mở rộng (như Fibre Channel chẳng hạn), bạn có thể cài đặt bất kỳ file hệ thống nào như SMB, NFS, iSCSI, hoặc SAN là thư mục địa phương để dữ liệu có thể được truy cập trên từng nút khi các nút hoạt động. Điều này được thực hiện bằng cách chỉnh sửa các thông số đầu vào trong file

/etc/httpd/conf/httpd.conf như sau (ít nhất là trên phân phối CentOS):

Listen 192.168.1.5:80

DocumentRoot "/data"

Bỏ chức năng bắt đầu tự động khởi động trong thời gian boot cho Apache service là khá quan trọng. Khi đó heartbeat sẽ bắt đầu và kết thúc dịch vụ khi cần thiết. Vô hiệu hoá chức năng bắt đầu với câu lệnh sau (trên hệ thống Red Hat):

chkconfig httpd remove

Phải đảm bảo cấu hình Apache giống nhau trên cả hai nút.


Kiểm tra

Bây giờ chúng ta sẽ kiểm tra thử chương trình làm việc với cấu hình vừa thiết lập, bắt đầu tạo cluster mới, khởi động dịch vụ hearbeat trên cả hai nút:

/etc/init.d/heartbeat start

Xem thư mục /var/log/ha-log trên cả hai nút. Nếu tất cả được cấu hình chính xác, bạn sẽ thấy thông tin trong các file log như sau:


Configuration validated. Starting heartbeat 1.2.3.cvs.20050927

heartbeat: version 1.2.3.cvs.20050927

Link node1.example.com:eth1 up.

Link node2.example.com:eth1 up.

Status update for node node2.example.com: status active

Local status now set to: 'active'

remote resource transition completed.

Local Resource acquisition completed. (none)

node2.example.com wants to go standby [foreign]

acquire local HA resources (standby).

local HA resource acquisition completed (standby).

Standby resource acquisition done [foreign].

Initial resource acquisition complete (auto_failback)

remote resource transition completed.

Tiếp theo là kiểm tra failover, khởi động lại server master (server chính). Server slave (server phụ) là dịch vụ Apache. Nếu mọi thứ hoạt động tốt bạn sẽ thấy như sau:


Received shutdown notice from 'node1.example.com'.

Resources being acquired from node1.example.com.

acquire local HA resources (standby).

local HA resource acquisition completed (standby).

Standby resource acquisition done [foreign].

Running /etc/ha.d/rc.d/status status

Taking over resource group 192.168.1.5

Acquiring resource group: node1.example.com 192.168.1.5 httpd

mach_down takeover complete for node node1.example.com.

node node1.example.com: is dead

Dead node node1.example.com gave up resources.

Link node1.example.com:eth1 dead.

Khi master online trở lại, thông số trên Apache service như sau:


Heartbeat restart on node node1.example.comheartbeat

Link node1.example.com:eth1 up.

node2.example.com wants to go standby [foreign]

standby: node1.example.com can take our foreign resources

give up foreign HA resources (standby).

Releasing resource group: node1.example.com 192.168.1.5 httpd

Local standby process completed [foreign].

remote resource transition completed.

Other node completed standby takeover of foreign resources.


Kết luận


Đó là tất cả các bước để xây dựng một Web server cluster mang tính sẵn sàng cao với chi phí thấp. Tất nhiên có nhiều sản phẩm thương mại khác cũng được cung cấp với cùng mục đích này. Nhưng với các doanh nghiệp nhỏ hay các tổ chức tương tự thì High Availability Linux và heartbeat là một lựa chọn sáng suốt.

Wednesday, February 13, 2008

Config vsftp in linux

Contents

[hide]

ponsors

Introduction

The File Transfer Protocol (FTP) is used as one of the most common means of copying files between servers over the Internet. Most web based download sites use the built in FTP capabilities of web browsers and therefore most server oriented operating systems usually include an FTP server application as part of the software suite. Linux is no exception.

This chapter will show you how to convert your Linux box into an FTP server using the default Very Secure FTP Daemon (VSFTPD) package included in Fedora.

FTP Overview

FTP relies on a pair of TCP ports to get the job done. It operates in two connection channels as I'll explain:

FTP Control Channel, TCP Port 21: All commands you send and the ftp server's responses to those commands will go over the control connection, but any data sent back (such as "ls" directory lists or actual file data in either direction) will go over the data connection.

FTP Data Channel, TCP Port 20: This port is used for all subsequent data transfers between the client and server.

In addition to these channels, there are several varieties of FTP.

Types of FTP

From a networking perspective, the two main types of FTP are active and passive. In active FTP, the FTP server initiates a data transfer connection back to the client. For passive FTP, the connection is initiated from the FTP client. These are illustrated in Figure 15-1.

Figure 15-1 Active And Passive FTP Illustrated

Image:Ftp.gif

From a user management perspective there are also two types of FTP: regular FTP in which files are transferred using the username and password of a regular user FTP server, and anonymous FTP in which general access is provided to the FTP server using a well known universal login method.

Take a closer look at each type.

Active FTP

The sequence of events for active FTP is:

  1. Your client connects to the FTP server by establishing an FTP control connection to port 21 of the server. Your commands such as 'ls' and 'get' are sent over this connection.
  2. Whenever the client requests data over the control connection, the server initiates data transfer connections back to the client. The source port of these data transfer connections is always port 20 on the server, and the destination port is a high port (greater than 1024) on the client.
  3. Thus the ls listing that you asked for comes back over the port 20 to high port connection, not the port 21 control connection.

FTP active mode therefore transfers data in a counter intuitive way to the TCP standard, as it selects port 20 as it's source port (not a random high port that's greater than 1024) and connects back to the client on a random high port that has been pre-negotiated on the port 21 control connection.

Active FTP may fail in cases where the client is protected from the Internet via many to one NAT (masquerading). This is because the firewall will not know which of the many servers behind it should receive the return connection.

Passive FTP

Passive FTP works differently:

  1. Your client connects to the FTP server by establishing an FTP control connection to port 21 of the server. Your commands such as ls and get are sent over that connection.
  2. Whenever the client requests data over the control connection, the client initiates the data transfer connections to the server. The source port of these data transfer connections is always a high port on the client with a destination port of a high port on the server.

Passive FTP should be viewed as the server never making an active attempt to connect to the client for FTP data transfers. Because client always initiates the required connections, passive FTP works better for clients protected by a firewall.

As Windows defaults to active FTP, and Linux defaults to passive, you'll probably have to accommodate both forms when deciding upon a security policy for your FTP server.

Regular FTP

By default, the VSFTPD package allows regular Linux users to copy files to and from their home directories with an FTP client using their Linux usernames and passwords as their login credentials.

VSFTPD also has the option of allowing this type of access to only a group of Linux users, enabling you to restrict the addition of new files to your system to authorized personnel.

The disadvantage of regular FTP is that it isn't suitable for general download distribution of software as everyone either has to get a unique Linux user account or has to use a shared username and password. Anonymous FTP allows you to avoid this difficulty.

Anonymous FTP

Anonymous FTP is the choice of Web sites that need to exchange files with numerous unknown remote users. Common uses include downloading software updates and MP3s and uploading diagnostic information for a technical support engineers' attention. Unlike regular FTP where you login with a preconfigured Linux username and password, anonymous FTP requires only a username of anonymous and your email address for the password. Once logged in to a VSFTPD server, you automatically have access to only the default anonymous FTP directory (/var/ftp in the case of VSFTPD) and all its subdirectories.

As seen in Chapter 6, "Installing Linux Software", using anonymous FTP as a remote user is fairly straight forward. VSFTPD can be configured to support user-based and or anonymous FTP in its configuration file which you'll see later.

Problems With FTP And Firewalls

FTP frequently fails when the data has to pass through a firewall, because firewalls are designed to limit data flows to predictable TCP ports and FTP uses a wide range of unpredictable TCP ports. You have a choice of methods to overcome this.

Note: The Appendix II, "Codes, Scripts, and Configurations", contains examples of how to configure the VSFTPD Linux firewall to function with both active and passive FTP.

Client Protected By A Firewall Problem

Typically firewalls don't allow any incoming connections at all, which frequently blocks active FTP from functioning. With this type of FTP failure, the active FTP connection appears to work when the client initiates an outbound connection to the server on port 21. The connection then appears to hang, however, as soon as you use the ls, dir, or get commands. The reason is that the firewall is blocking the return connection from the server to the client (from port 20 on the server to a high port on the client). If a firewall allows all outbound connections to the Internet, then passive FTP clients behind a firewall will usually work correctly as the clients initiate all the FTP connections.

  • Solution

Table 15-1 shows the general rules you'll need to allow FTP clients through a firewall:

Table 15-1 Client Protected by Firewall - Required Rules for FTP

Method Source Address Source Port Destination Address Destination Port Connection Type
Allow outgoing control connections to server
Control Channel FTP client / network High1 FTP server2 21 New
FTP server2 21 FTP client/ network High Established3
Allow the client to establish data channels to remote server
Active FTP FTP server 2 20 FTP client / network High New
FTP client / network High FTP server 2 20 Established3
Passive FTP FTP client / network High FTP server 2 High New
FTP server 2 High FTP client / network High Established 3
1 Greater than 1024.
2 In some cases, you may want to allow all Internet users to have access, not just a specific client server or network.
3 Many home-based firewall/routers automatically allow traffic for already established connections. This rule may not be necessary in all cases.

Server Protected By A Firewall Problem

Typically firewalls don't let any connections come in at all. When a an incorrectly configured firewall protects an FTP server, the FTP connection from the client doesn't appear to work at all for both active and passive FTP.

  • Solution

Table 15-2 outlines the general rules needed to allow FTP servers through a firewall.

Method Source Address Source Port Destination Address Destination Port Connection Type
Allow incoming control connections to server
Control Channel FTP client / network 2 High1 FTP server 21 New
FTP server 21 FTP client / network 2 High Established3
Allow server to establish data channel to remote client
Active FTP FTP server 20 FTP client / network 2 High New
FTP client / network 2 High FTP server 20 Established3
Passive FTP FTP client / network 2 High FTP server High New
FTP server High FTP client / network 2 High Established 3
1 Greater than 1024.
2 In some cases, you may want to allow all Internet users to have access, not just a specific client server or network.
3Many home-based firewall/routers automatically allow traffic for already established connections. This rule may not be necessary in all cases.

How To Download And Install VSFTPD

Most Linux software products are available in a precompiled package format. Downloading and installing packages isn't hard. If you need a refresher, Chapter 6, "Installing Linux Software", covers how to do this in detail. It is best to use the latest version of VSFTPD.

When searching for the file, remember that the VSFTPD packages' filename usually starts with the word vsftpd followed by a version number, as in vsftpd-1.2.1-5.i386.rpm for Redhat/Fedora or vsftpd_2.0.4-0ubuntu4_i386.deb for Ubuntu.

How To Get VSFTPD Started

With Fedora, Redhat, Ubunbtu and Debian You can start, stop, or restart VSFTPD after booting by using these commands:

[root@bigboy tmp]# /etc/init.d/vsftpd start
[root@bigboy tmp]# /etc/init.d/vsftpd stop
[root@bigboy tmp]# /etc/init.d/vsftpd restart

With Redhat / Fedora you can configure VSFTPD to start at boot you can use the chkconfig command.

[root@bigboy tmp]# chkconfig vsftpd on

With Ubuntu / Debian the sysv-rc-conf command can be used like this:

root@u-bigboy:/tmp# sysv-rc-conf on

Note: In RedHat Linux version 8.0 and earlier, VSFTPD operation is controlled by the xinetd process, which is covered in Chapter 16, "Telnet, TFTP, and xinetd". You can find a full description of how to configure these versions of Linux for VSFTPD in Appendix III, "Fedora Version Differences."


Testing the Status of VSFTPD

You can always test whether the VSFTPD process is running by using the netstat -a command which lists all the TCP and UDP ports on which the server is listening for traffic. This example shows the expected output.

[root@bigboy root]# netstat -a | grep ftp
tcp 0 0 *:ftp *:* LISTEN
[root@bigboy root]#

If VSFTPD wasn't running, there would be no output at all.

The vsftpd.conf File

VSFTPD only reads the contents of its vsftpd.conf configuration file only when it starts, so you'll have to restart VSFTPD each time you edit the file in order for the changes to take effect. The file may be located in either the /etc or the /etc/vsftpd directories depending on your Linux distribution.

This file uses a number of default settings you need to know about.

  • VSFTPD runs as an anonymous FTP server. Unless you want any remote user to log into to your default FTP directory using a username of anonymous and a password that's the same as their email address, I would suggest turning this off. The configuration file's anonymous_enable directive can be set to no to disable this feature. You'll also need to simultaneously enable local users to be able to log in by removing the comment symbol (#) before the local_enable instruction.
  • If you enable anonymous FTP with VSFTPD, remember to define the root directory that visitors will visit. This is done with the anon_root directive.
anon_root=/data/directory
  • VSFTPD allows only anonymous FTP downloads to remote users, not uploads from them. This can be changed by modifying the anon_upload_enable directive shown later.
  • VSFTPD doesn't allow anonymous users to create directories on your FTP server. You can change this by modifying the anon_mkdir_write_enable directive.
  • VSFTPD logs FTP access to the /var/log/vsftpd.log log file. You can change this by modifying the xferlog_file directive.
  • By default VSFTPD expects files for anonymous FTP to be placed in the /var/ftp directory. You can change this by modifying the anon_root directive. There is always the risk with anonymous FTP that users will discover a way to write files to your anonymous FTP directory. You run the risk of filling up your /var partition if you use the default setting. It is best to make the anonymous FTP directory reside in its own dedicated partition.

The configuration file is fairly straight forward as you can see in the snippet below where we enable anonymous FTP and individual accounts simultaneously.

# Allow anonymous FTP?
anonymous_enable=YES
...
# The directory which vsftpd will try to change
# into after an anonymous login. (Default = /var/ftp)
anon_root=/data/directory
...
# Uncomment this to allow local users to log in.
local_enable=YES
...
# Uncomment this to enable any form of FTP write command.
# (Needed even if you want local users to be able to upload files)
write_enable=YES
...
# Uncomment to allow the anonymous FTP user to upload files. This only
# has an effect if global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
#anon_upload_enable=YES
...
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
...
# Activate logging of uploads/downloads.
xferlog_enable=YES
...
# You may override where the log file goes if you like.
# The default is shown below.
xferlog_file=/var/log/vsftpd.log
...

To activate or deactivate a feature, remove or add the # at the beginning of the appropriate line.

Other vsftpd.conf Options

There are many other options you can add to this file:

  • Limiting the maximum number of client connections (max_clients)
  • Limiting the number of connections by source IP address (max_per_ip)
  • The maximum rate of data transfer per anonymous login. (anon_max_rate)
  • The maximum rate of data transfer per non-anonymous login. (local_max_rate)

Descriptions on this and more can be found in the vsftpd.conf man pages.

FTP Security Issues

FTP has a number of security drawbacks, but you can overcome them in some cases. You can restrict an individual Linux user's access to non-anonymous FTP, and you can change the configuration to not display the FTP server's software version information, but unfortunately, though very convenient, FTP logins and data transfers are not encrypted.

The /etc/vsftpd.ftpusers File

For added security, you may restrict FTP access to certain users by adding them to the list of users in the /etc/vsftpd.ftpusers file. The VSFTPD package creates this file with a number of entries for privileged users that normally shouldn't have FTP access. As FTP doesn't encrypt passwords, thereby increasing the risk of data or passwords being compromised, it is a good idea to let these entries remain and add new entries for additional security.

Anonymous Upload

If you want remote users to write data to your FTP server, then you should create a write-only directory within /var/ftp/pub. This will allow your users to upload but not access other files uploaded by other users. The commands you need are:

[root@bigboy tmp]# mkdir /var/ftp/pub/upload
[root@bigboy tmp]# chmod 722 /var/ftp/pub/upload

FTP Greeting Banner

Change the default greeting banner in the vsftpd.conf file to make it harder for malicious users to determine the type of system you have. The directive in this file is.

ftpd_banner= New Banner Here

Using SCP As Secure Alternative To FTP

One of the disadvantages of FTP is that it does not encrypt your username and password. This could make your user account vulnerable to an unauthorized attack from a person eavesdropping on the network connection. Secure Copy (SCP) and Secure FTP (SFTP) provide encryption and could be considered as an alternative to FTP for trusted users. SCP does not support anonymous services, however, a feature that FTP does support.

Troubleshooting FTP

You should always test your FTP installation by attempting to use an FTP client to log in to your FTP server to transfer sample files.

The most common sources of day-to-day failures are incorrect usernames and passwords.

Initial setup failures could be caused by firewalls along the path between the client and server blocking some or all types of FTP traffic. Typical symptoms of this are either connection timeouts or the ability to use the ls command to view the contents of a directory without the ability to either upload or download files. Follow the firewall rule guidelines to help overcome this problem. Connection problems could also be the result of typical network issues outlined in Chapter 4, "Simple Network Troubleshooting".

Tutorial

FTP has many uses, one of which is allowing numerous unknown users to download files. You have to be careful, because you run the risk of accidentally allowing unknown persons to upload files to your server. This sort of unintended activity can quickly fill up your hard drive with illegal software, images, and music for the world to download, which in turn can clog your server's Internet access and drive up your bandwidth charges.

FTP Users with Only Read Access to a Shared Directory

In this example, anonymous FTP is not desired, but a group of trusted users need to have read only access to a directory for downloading files. Here are the steps:

1) Disable anonymous FTP. Comment out the anonymous_enable line in the vsftpd.conf file like this:

# Allow anonymous FTP?
anonymous_enable=NO

2) Enable individual logins by making sure you have the local_enable line uncommented in the vsftpd.conf file like this:

# Uncomment this to allow local users to log in.
local_enable=YES

3) Start VSFTP.

[root@bigboy tmp]# service vsftpd start

4) Create a user group and shared directory. In this case, use /home/ftp-users and a user group name of ftp-users for the remote users

[root@bigboy tmp]# groupadd ftp-users
[root@bigboy tmp]# mkdir /home/ftp-docs

5) Make the directory accessible to the ftp-users group.

[root@bigboy tmp]# chmod 750 /home/ftp-docs
[root@bigboy tmp]# chown root:ftp-users /home/ftp-docs

6) Add users, and make their default directory /home/ftp-docs

[root@bigboy tmp]# useradd -g ftp-users -d /home/ftp-docs user1
[root@bigboy tmp]# useradd -g ftp-users -d /home/ftp-docs user2
[root@bigboy tmp]# useradd -g ftp-users -d /home/ftp-docs user3
[root@bigboy tmp]# useradd -g ftp-users -d /home/ftp-docs user4
[root@bigboy tmp]# passwd user1
[root@bigboy tmp]# passwd user2
[root@bigboy tmp]# passwd user3
[root@bigboy tmp]# passwd user4

7) Copy files to be downloaded by your users into the /home/ftp-docs directory

8) Change the permissions of the files in the /home/ftp-docs directory for read only access by the group

[root@bigboy tmp]# chown root:ftp-users /home/ftp-docs/*
[root@bigboy tmp]# chmod 740 /home/ftp-docs/*
Users should now be able to log in via FTP to the server using their new usernames and passwords. If you absolutely don't want any FTP users to be able to write to any directory, then you should set the write_enable line in your vsftpd.conf file to no:
write_enable = NO

Remember, you must restart VSFTPD for the configuration file changes to take effect.

Sample Login Session To Test Functionality

Here is a simple test procedure you can use to make sure everything is working correctly:

1) Check for the presence of a test file on the ftp client server.

[root@smallfry tmp]# ll
total 1
-rw-r--r-- 1 root root 0 Jan 4 09:08 testfile
[root@smallfry tmp]#

2) Connect to bigboy via FTP

[root@smallfry tmp]# ftp 192.168.1.100
Connected to 192.168.1.100 (192.168.1.100)
220 ready, dude (vsFTPd 1.1.0: beat me, break me)
Name (192.168.1.100:root): user1
331 Please specify the password.
Password:
230 Login successful. Have fun.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
As expected, we can't do an upload transfer of testfile to bigboy.
ftp> put testfile
local: testfile remote: testfile
227 Entering Passive Mode (192,168,1,100,181,210)
553 Could not create file.
ftp>
But we can view and download a copy of the VSFTPD RPM located on the FTP server bigboy.
ftp> ls
227 Entering Passive Mode (192,168,1,100,35,173)
150 Here comes the directory listing.
-rwxr----- 1 0 502 76288 Jan 04 17:06 vsftpd-1.1.0-1.i386.rpm
226 Directory send OK.
ftp> get vsftpd-1.1.0-1.i386.rpm vsftpd-1.1.0-1.i386.rpm.tmp
local: vsftpd-1.1.0-1.i386.rpm.tmp remote: vsftpd-1.1.0-1.i386.rpm
227 Entering Passive Mode (192,168,1,100,44,156)
150 Opening BINARY mode data connection for vsftpd-1.1.0-1.i386.rpm (76288 bytes).
226 File send OK.
76288 bytes received in 0.499 secs (1.5e+02 Kbytes/sec)
ftp> exit
221 Goodbye.
[root@smallfry tmp]#
As expected, anonymous FTP fails.
[root@smallfry tmp]# ftp 192.168.1.100
Connected to 192.168.1.100 (192.168.1.100)
220 ready, dude (vsFTPd 1.1.0: beat me, break me)
Name (192.168.1.100:root): anonymous
331 Please specify the password.
Password:
530 Login incorrect.
Login failed.
ftp> quit
221 Goodbye.
[root@smallfry tmp]#

Now that testing is complete, you can make this a regular part of your FTP server's operation.

Conclusion

FTP is a very useful software application that can have enormous benefit to a Web site or to collaborative computing in which files need to be shared between business partners. Although insecure, it is universally accessible, because FTP clients are a part of all operating systems and Web browsers. If data encryption security is of great importance to you, then you should probably consider SCP as a possible alternative

Note: Enable FTP allow users to change their default home diretory to login succesful (in SELinux Administration).

Cài đặt unrar trên linux

Cài đặt unrar thông qua dòng lệnh

Trên Debian, bạn dùng apt-get để cài đặt unrar:
# apt-get install unrar

Nếu là Fedora core thì sử dụng yum:
# yum install unrar

Nếu bạn dùng FreeBSD, thì:
# pkg_add -v -r unrar

Nếu một trong những cách trên không được thì có thể download unrar tại rarlab site:
$ cd /tmp
$ wget http://www.rarlab.com/rar/rarlinux-3.6.0.tar.gz

Giải nép
$ tar -zxvf rarlinux-3.6.0.tar.gz

Cả 2 trình unrar và rar đều nằm trong thư mục có tên là rar.
Nhảy vào trong thư mục rar:
$ cd rar
$ ./unrar

Bây giờ chép cả rar và unrar vào trong thư mực /bin :
# cp rar unrar /bin

Sử dụng unrar


Task: Mở file rar trong thư mục hiện tại:

$ unrar e file.rar

Task: Liệt kê các file trong gói rar:

$ unrar l file.rar

Task: Giải nén các tập tin trong rar:

$ unrar x file.rar

Kiểm tra gói rar có bị hư hỏng hay không:

$ unrar t file.rar

cài đặt NTFS trên linux

Để cài đặt NTFS cần qua 5 bước:
(1) Cài đặt NTFS support,
(2) Kiểm tra bạn có bao nhiêu partition,
(3) Tạo các mount point,
(4) mount partitions
(5) update fstab để mount cho lần sau khi boot.

Sau khi cài đặt xong các bước trên bạn có thể lấy dữ liệu, ghi dữ liệu vào partition thuộc kiểu NTFS

1. Cài đặt NTFS support

Chức năng NTFS support có thể có sẵn trong đĩa DVD cài đặt. Nếu không, có thể cài đặt bằng yum nếu như là Fedora Core:
[mirandam@charon ~]$ sudo yum install fuse fuse-libs ntfs-3g

Trên Debian hay Ubuntu, bạn dùng apt-get:
[mirandam@charon ~]$ sudo apt-get install fuse fuse-libs ntfs-3g

Nếu bạn dùng FreeBSD, thì:
[mirandam@charon ~]$ sudo pkg_add install fuse fuse-libs ntfs-3g

2. Kiểm tra bạn có bao nhiêu partition

Sử dụng fdisk để liệt kê các partition.
[mirandam@charon ~]$ sudo /sbin/fdisk -lu /dev/sda | grep NTFS
/dev/sda1 * 63 33559784 16779861 7 HPFS/NTFS
/dev/sda2 33559785 67119569 16779892+ 7 HPFS/NTFS
/dev/sda3 67119570 100679354 16779892+ 7 HPFS/NTFS

3. Tạo các mount point

Với mỗi partition ở bước số 2, bạn sẽ cần tạo một "mount point". Một mount point chỉ đơn giản là một thư mục. Các thư mục thường được sử dụng là: /media/ và /mnt/. Nhưng bạn có thể sử dụng một thư mục bất kì.
[mirandam@charon ~]$ cd /media/
[mirandam@charon media]$ sudo mkdir c_drive d_drive e_drive

4. Mount Partitions

Sử dụng NTFS-3G có thể sử dụng NTFS partitions dưới 2 chế độ :read-only và read-write. Đối với những người mới sử dụng nên sử dụng chế độ read-only.
[mirandam@charon ~]$ sudo mount /dev/sda1 /media/c_drive -t ntfs-3g -r -o umask=0222
[mirandam@charon ~]$ sudo mount /dev/sda2 /media/d_drive -t ntfs-3g -r -o umask=0222
[mirandam@charon ~]$ sudo mount /dev/sda3 /media/e_drive -t ntfs-3g -r -o umask=0222

Chế độ Read/Write: ví dụ ở trên là chế độ read-only . Để dùng chế độ read/write, bạn phải dùng tham số -rw -o umask=0000. Ví dụ:
[mirandam@charon ~]$ sudo mount /dev/sda1 /media/c_drive -t ntfs-3g -rw -o umask=0000

Khuyến cáo: Sử dụng lệnh man mount để hiểu rõ về tham số umask= có tác dụng gì.

5. Update /etc/fstab

Nếu bạn muốn mỗi lần khởi động các partition được mount tự động bạn cần thay đổi trong tập tin /etc/fstab.

Mở/etc/fstab bằng một trình soạn thảo: (sử dụng nano thay cho gedit nếu như bạn không có GUI)
[mirandam@charon ~]$ sudo gedit /etc/fstab

Thêm vào cuối tập tin những thông tin sau:
/dev/sda1 /media/c_drive ntfs-3g ro,defaults,umask=0222 0 0
/dev/sda2 /media/d_drive ntfs-3g ro,defaults,umask=0222 0 0
/dev/sda3 /media/e_drive ntfs-3g ro,defaults,umask=0222 0 0

Chế độ Read/Write: ví dụ ở trên là chế độ read-only . Để dùng chế độ read/write, bạn phải dùng tham số -rw -o umask=0000. Ví dụ:
/dev/sda1 /media/c_drive ntfs-3g rw,defaults,umask=0000 0 0