30 thg 5, 2013

Thêm ổ cứng với linux

* Cơ bản
- Tháo lắp phần cứng
- Phân vùng (Primary Partition, Extended Partition, Logical Partition)
- Terminal
- Vim editor
* Sau khi lắp thêm ổ cứng
- SCSI hard drive thì Conventional Naming là sd* còn IDE drives là hd*, ở đây sdb là drives mới được thêm vào, do đó nếu chạy command: $ df -h sẽ không thấy phân vùng nào của sdb, công việc tiếp theo chúng ta cần làm là format đối với ổ cứng mới, hoặc mount đối với ổ cứng đã có dữ liệu, ở đây ổ cứng là mới hoàn toàn do đó chúng ta cần set Partition cho nó.
* Phân vùng ổ cứng 
- command: $ sudo fdisk /dev/sdb gõ m để thấy help rõ ràng nhất
- n để tạo mới Partition, có 2 option e để tạo Extended Partition và p Primary Partition, bắt buộc phải có Primary Partition trước khi phân vùng Extended Partition
- Partition number (1-4): để hiểu hơn về Partition (link), chọn 1
- Start cylinder: Mặc định là 1, chọn 1
- End  cylinder : chọn giá trị cuối (ở đây tôi chia làm 1 Primary Partition)
- Đợi xong tác vụ (ít thời gian) => xong việc phân vùng
- Sau khi hoàn thành nhơ chọn w để write lại Partition cho ổ cứng, hoặc có thể check lại command: $ ls /dev/sd* nếu thấy sd1 là ok
* Format ổ cứng
- Có thể format hầu hêt các định dạng của file system, Linux phổ biến nhất với các định dạng ext2,ext3,ext4,(mình chọn ext4)
$ sudo mkfs -t ext4 /dev/sdb1
Nếu thây wrtite inode done là ok rồi 
* Mount Partition sdb1 để sử dụng 
- Command : $ sudo vim /etc/fstab   chọn insert mode thêm /dev/sdb1       /media/data     ext4    defaults        0       2
- Command : $ cd /media
- Command : $ sudo mkdir data
- Command : $ sudo mount -a
- Command : $ sudo df -h
- Nếu thấy /dev/sdb1  dung lượng bị giảm là do mất 5% cho user root :)
Restart để sử dụng 

29 thg 5, 2013

Cấu Trúc Phân Vùng và Thư Mục Trong Linux


Khi cài hoặc sử dụng Linux đa phần mọi người mới lần đầu đến với linux đều phải đối diện với câu hỏi "Kích thước phân vùng đĩa của tôi cần như thế nào ?". Bạn cũng có thể cung cấp 1 phân vùng duy nhất để cài đặt tất cả lên, hoặc bạn cần tính toán để tạo từng phân vùng phù hợp với từng đối tượng chương trình trong hệ thống linux phù hợp với yêu cầu sử dụng của bạn, điều này là rất cần thiết để hoạt động của linux là tối ưu. Để làm được điều đó trước tiên bạn cần phải hiểu rõ hơn về các thư mục trong hệ thống linux.

1. Thư mục /
Là thư mục Root Directory - thường được gọi là thư mục gốc , Đây là nơi sẽ chứ mọi thư mục con khác. Đây là thư mục bắt buộc phải có cho bất cứ một hệ thống linux nào.
Nếu bạn không quan tâm đến việc tối ưu phân vùng, chúng ta chỉ cần tạo một phân vùng khoảng > 5Gb và mount [ext3] vào / là hoàn toàn có thể cài đặt và sử dụng linux một cách hiệu quả. Tuy nhiên tùy thuộc vào yêu cầu hệ thống của bạn cũng có thể chỉ cần 1gb cho linux là đã hoạt động tốt rồi.

2. Thư mục /Boot
Thư mục /boot thường được dùng để chứa kernel, và các cấu hình để boot hệ thống.
Không nhất thiết phải tạo riêng một phân vùng đĩa cho /Boot nếu bạn chỉ sử dụng duy nhất 1 distro cho hệ thống của bạn. Nếu bạn có ý định sử dụng nhiều linux distro khác nhau trên cùng một hệ thống, Khi đó bạn có thể đặt tất cả các kernel, các options và các cấu hình vào một phân vùng, và mount vào /boot cho mỗi distro.

3. Thư mục /home
Thư mục /home là nơi chứa tất cả các dữ liệu cá nhân cho từng user trên hệ thống (ngoại trừ user root). Rất khó để biết được chính xác bạn cần bao nhiêu dung lượng cho /Home, trừ khi bạn tiến hành giới hạn tài nguyên cho từng account trên hệ thống và bạn biết rõ sẽ có bao nhiêu account. Theo kinh nghiệm thông thường bạn sẽ chia các phân vùng khác trước, phần không gian đĩa còn lại được dành cho /home

Hướng dẫn cài đặt và cấu hình DNS Server trên LINUX với BIND

Cài đặt và cấu hình DNS
cơ bản về DNS trên Linux cũng tương tự như trên Windows.


Chúng ta sẽ cài đặt và config các phần như sau

• Cài đặt cơ bản
• Cấu hình Master DNS
• Các lệnh kiểm tra DNS
• Cấu hình Slave DNS
• Bảo mật thông tin transfer

1. Cài đặt DNS Server Linux với BIND

- Cách 1 Cài bằng YUM
Trích dẫn:
#yum -y install bind*


hoặc
Trích dẫn:
#yum install bind bind-utils bind-chroot
 Hoặc
#yum install –excule=bind97* bind*

- Cách 2 cài bằng file .tar.gz
Download Bind.version.tar.gz về máy và cài:
#cd /tmp
#wget ftp://ftp.isc.org/isc/bind9/9.7.0/bind-9.7.0.tar.gz
#tar -xvzf bind-9.7.0.tar.gz
#cd bind-9.7.0
#./configure
#make
#make test
#make install

2. Cấu Hình Master DNS

Domain name : dnstest.com
IP: 192.168.100.1
Cài gói bind-chroot, thư mục chứa file cấu hình /var/named/chroot/..

2.1 - cấu hình file named.conf như sau :
Trích dẫn:
[root@dnstest etc]# vi /var/named/chroot/etc/named.conf
acl mysubnet { <-- tạo ACL - access control list cái này chắc anh em học cisco đều hiểu
192.168.100.0/24;
127.0.0.1;
};

options {
directory "/var/named";
notify no;
auth-nxdomain yes;
listen-on { <-- chỉ cho phép lắng nghe trên mysubnet được bạn định nghĩa ở trên
mysubnet;
};
allow-query { mysubnet; };
allow-notify {mysubnet;};
version ""; <-- Không hiển thị thông tin version của bind
};

controls {
inet 127.0.0.1 allow { localhost; } keys { rndckey; };
};

zone "localdomain" IN {
type master;
file "localdomain.zone";
allow-update { none; };
};

zone "localhost" IN {
type master;
file "localhost.zone";
allow-update { none; };
};

zone "0.0.127.in-addr.arpa" IN {
type master;
file "0.0.127.in-addr.arpa.zone";
allow-update { none; };
};

include "/etc/rndc.key";

zone "dnstest.com" {
type master; <-- kiểu dns là master
file "dnstest.com.zone"; <-- file zone thuận
allow-query{mysubnet;};
allow-update{mysubnet;};
};

zone "100.168.192.in-addr.arpa" {
type master;
file "100.168.192.in-addr.arpa.zone"; <-- file zone nghịch
allow-query{mysubnet;};
allow-update{mysubnet;};
};


2.2 Cấu hình file ZONE
- Các định nghĩa thông tin trong file cấu hình zone

Name: tên gốc của zone. Ký tự @ tham chiếu đến zone gốc hiện tại trong /etc/named.conf

Class: IN hoặc Internet - định nghĩa địa chỉ IP khi map thông tin trong BIND. Các classes khác dành cho các giao thức non-internel và hiếm khi được sử dụng

Type: loại bản ghi DNS

Name-server: tên đầy đủ của name server chính của bạn.

Email-address: Địa chỉ email của người quản trị name server.

Serial-no: một số serial cho cấu hình hiện tại. bạn có thể dùng định dạng ngày tháng YYYYMMDD, theo sau là 1 chữ số. <-- bạn cần chú ý sau, nếu có 1 thay đổi muốn update ngay bạn cần thay đổi giá trị này.

Refresh: Khoảng thời gian mà slave DNS server sẽ check master DNS Server.

Retry: Khoảng thời gian Slave sẽ thử kết nối lại đến master sau khi bị lỗi

Expiry: Tổng khoảng thời gian slave nên thử lại để contact đến master trước khi data bị expired.

Minimum-TTL: Khi client tạo một query khôgn tồn tại trong sub domain, DNS server của bạn sẽ response là NXDOMAIN. Giá trị này định nghĩa thời lượng caching DNS của bạn bao gồm reponse đó.

Các bản ghi này có định dạng tương tự như SOA

NS: địa chỉ IP hoặc CNAME của name server

MX: tên mail server DNS

A: IP address của server

CNAME: tên bí danh của server

PTR: tên server đầy đủ khi phân giải ngược từ 1 địa chỉ IP

2.3 - Cấu hình file zone thuận dnstest.com.zone :

[root@dnstest etc]# vi /var/named/chroot/var/named/dnstest.com.zone
Trích dẫn:
$TTL 86400
@ IN SOA root.dnstest.com. (
  20100410 ; Serial
  28800 ; Refresh
  7200 ; Retry
  604800 ; Expire
  86400 ; TTL
);

@ IN NS dns.dnstest.com.
@ IN MX 10 mail.dnstest.com.

$ORIGIN dnstest.com.

@ IN A 192.168.100.1
dns IN A 192.168.100.1
mail IN A 192.168.100.1
www IN A 192.168.100.1


2.4 -Cấu hình file zone nghịch 100.168.192.in-addr.arpa.zone như sau:
Trích dẫn:
$TTL 86400
@ IN SOA root.dnstest.com. (
 20100410 ; Serial
 28800 ; Refresh
 7200 ; Retry
 604800 ; Expire
 86400 ; TTL
);

@ IN NS dns.dnstest.com.

1 IN PTR dnstest.com.
1 IN PTR dns.dnstest.com.
1 IN PTR mail.dnstest.com.
1 IN PTR www.dnstest.com.


2.5 - Khai báo Prefer DNS server
#vi /etc/resolv.conf

nameserver 192.168.100.1 <-- thêm vào nội dung

2.6 - Khởi động named
# /etc/init.d/named restart

2.7 - để named start mỗi khi khởi động
# chkconfig named on

3. Cấu hình Slave DNS

Trên Master DNS của bạn thêm option allow-transfer, để cho phép Slave dns được update.
Trích dẫn:
zone "dnstest.com" {
type master;
file "dnstest.com.zone";
allow-query{mysubnet;};
allow-update{mysubnet;};
allow-transfer {192.168.100.2;}; <-- chỉ ra IP của Slave DNS
};

zone "100.168.192.in-addr.arpa" {
type master;
file "100.168.192.in-addr.arpa.zone";
allow-query{mysubnet;};
allow-update{mysubnet;};
allow-transfer {192.168.100.2;}; <-- chỉ ra IP của Slave DNS
};

Trong file zone thuận chỉ ra thêm 1 nameserver
@ IN NS dns1.dnstest.com. <-- Master
@ IN NS dns2.dnstest.com. <-- Slave
dns1 IN A 192.168.100.1
dns2 IN A 192.168.100.2

Trên máy Slave DNS cài đặt tương tự và cấu hình chỉnh 2 option vào zone.
Trích dẫn:
zone "dnstest.com" {
type slave; <-- kiểu dns là slave
file "dnstest.com.zone"; <-- file zone thuận
masters {192.168.100.1;}; <-- chỉ IP Master dns
allow-query {mysubnet;};
allow-update {mysubnet;};
};

zone "100.168.192.in-addr.arpa" {
type slave;
file "100.168.192.in-addr.arpa.zone"; <-- file zone nghịch
masters {192.168.100.1;}; <-- chỉ IP Master dns
allow-query {mysubnet;};
allow-update {mysubnet;};
};

Chú ý: 2 file zone sẽ được tự động phát sinh để lưu thông tin của Master DNS. Và được lưu lại thư mục /var/named/chroot/var/named/slaves/..

Khai báo Prefer DNS Server


# vi /etc/resolv.conf
nameserver 192.168.100.2 <-- Slave DNS
nameserver 192.168.100.1 <-- Master DNS


24 thg 5, 2013

How to change a file's owner and group in Linux


chown - change the owner of a file >

You can change the owner and group of a file or a directory with the chown command. Please, keep in mind you can do this only if you are the root user or the owner of the file.
Set the file's owner:
chown username somefile
After giving this command, the new owner of a file called somefile will be the user username. The file's group owner will not change. Instead of a user name, you can also give the user's numeric ID here if you want.
You can also set the file's group at the same time. If the user name is followed by a colon and a group name, the file's group will be changed as well.
chown username:usergroup somefile
After giving this command, somefile's new owner would be user username and the group usergroup.
You can set the owner of a directory exactly the same way you set the owner of a file:
chown username somedir
Note that after giving this command, only the owner of the directory will change. The owner of the files inside of the directory won't change.
In order to set the ownership of a directory and all the files in that directory, you'll need the -R option:
chown -R username somedir
Here, R stands for recursive because this command will recursively change the ownership of directories and their contents. After issuing this example command, the user username will be the owner of the directory somedir, as well as every file in that directory.
Tell what happens:
chown -v username somefile
changed ownership of 'somefile' to username
Here, v stands for verbose. If you use the -v option, chown will list what it did (or didn't do) to the file.
The verbose mode is especially useful if you change the ownership of several files at once. For example, this could happen when you do it recursively:
chown -Rv username somedir
changed ownership of 'somedir/' to username
changed ownership of 'somedir/boringfile' to username
changed ownership of 'somedir/somefile' to username
As you can see, chown nicely reports to you what it did to each file.

chgrp - change the group ownership of a file >

In addition to chown, you can also use the chgrp command to change the group of a file or a directory. You must, again, be either the root user or the owner of the file in order to change the group ownership.
chgrp works pretty much the same way as chown does, except it changes the file's user group instead of the owner, of course.
chgrp usergroup somefile
After issuing this command, the file somefile will be owned by a user group usergroup. Although the file's group has changed to usergroup, the file's owner will still be the same.
The options of using chgrp are the same as using chown. So, for example, the -R and -v options will work with it just like they worked with chown:
chgrp -Rv usergroup somedir
changed group of 'somedir/' to usergroup
changed group of 'somedir/boringfile' to usergroup
changed group of 'somedir/somefile' to usergroup
chown nicely reports to you what it did to each file.


Linux file permissions


 How to view file permissions 

You can view the access permissions of a file by doing the long directory listing with the ls -l command. This is what a long directory listing might look like:
me@puter: /home/writers$ ls -l
total 17
drwxr-xr-x 3 nana writers 80 2005-09-20 21:37 dir
-rw-r----- 1 nana writers 8187 2005-09-19 13:35 file
-rwxr-xr-x 1 nana writers 10348 2005-07-17 20:31 otherfile
What does the output of ls -l mean? The very first column, the one that looks like a bunch of mumbo jumbo, shows the file type and permissions. The second column shows the number of links (directory entries that refer to the file), the third one shows the owner of the file, and the fourth one shows the group the file belongs to. The other columns show the file's size in bytes, date and time of last modification, and the filename.
The first column, the one that shows the file's permissions and looks like mumbo jumbo, is organized into four separate groups, although it certainly doesn't look very organized.
The first group consists of only one character, and it shows the file's type. For example, d means a directory and - means a normal file, so if you take a look at our example output, you'll notice dir is a directory, while file and otherfile are regular files.
The first character can be any of these:
d = directory
- = regular file
l = symbolic link
s = Unix domain socket
p = named pipe
c = character device file
b = block device file
The next nine characters show the file's permissions, divided into three groups, each consisting of three characters. The first group of three characters shows the read, write, and execute permissions for user, the owner of the file. The next group shows the read, write, and execute permissions for the group of the file. Similarly, the last group of three characters shows the permissions for other, everyone else. In each group, the first character means the read permission, the second one write permission, and the third one execute permission.
The characters are pretty easy to remember.
r = read permission
w = write permission
x = execute permission
- = no permission
What does this mean in practice? Let's have an example. Remember the imaginary directory listing we did at the beginning? The output looked like this:
drwxr-xr-x 3 nana writers 80 2005-09-20 21:37 dir
-rw-r----- 1 nana writers 8187 2005-09-19 13:35 file
-rwxr-xr-x 1 nana writers 10348 2005-07-17 20:31 otherfile
As we already noticed, dir is a directory, because the first column begins with a d. The owner of this directory is user nana and the group owner is writers. The first three characters,rwx, indicate the directory's owner, nana in this case, has full access to the directory. The user nana is able to access, view, and modify the files in that directory. The next three characters,r-x, indicate that all users belonging to group writers have read and execute permissions to the directory. They can change into the directory, execute files, and view its contents. However, because they don't have write permissions, they can't make any changes to the directory content. Finally, the last three characters, r-x, indicate that all the users who are notnana or don't belong into group writers, have read and execute permissions in the directory.
How about file? Because the first column begins with a -, the file is a regular file, owned by user nana and group writers, just like the directory in our example. The first three characters, rw-, indicate the owner has read and write access to the file. According to the next three characters, r--, the users belonging to group writers can view the file but not modify or execute it. The final three characters, ---, indicate no one else has any access to the file.
Similarly, you can see otherfile is a regular file and its owner has full access to it, while everyone else can read and execute the file but not modify it.


How to set file permissions - symbolic mode >

You can set file permissions with the chmod command. Both the root user and the file's owner can set file permissions. chmod has two modes, symbolic and numeric.
The symbolic mode is pretty easy to remember. First, you decide if you set permissions for the user (u), the group (g), others (o), or all of the three (a). Then, you either add a permission (+), remove it (-), or wipe out the previous permissions and add a new one (=). Next, you decide if you set the read permission (r), write permission (w), or execute permission (x). Last, you'll tell chmod which file's permissions you want to change.
Let's have a couple of examples. Suppose we have a regular file called testfile, and the file has full access permissions for all the groups (long directory listing would show -rwxrwxrwx as the file's permissions).
Wipe out all the permissions but add read permission for everybody:
chmod a=r testfile
After the command, the file's permissions would be -r--r--r--
Add execute permissions for group:
chmod g+x testfile
Now, the file's permissions would be -r--r-xr--
Add both write and execute permissions for the file's owner. Note how you can set more than one permission at the same time:
chmod u+wx testfile
After this, the file permissions will be -rwxr-xr--
Remove the execute permission from both the file's owner and group. Note, again, how you can set them both at once:
chmod ug-x testfile
Now, the permissions are -rw-r--r--
As a summary, have a look at this quick reference for setting file permissions in symbolic mode:
Which user?
uuser/owner
ggroup
oother
aall
What to do?
+add this permission
-remove this permission
=set exactly this permission
Which permissions?
rread
wwrite
xexecute

How to set file permissions - numeric mode >

The other mode in which chmod can be used is the numeric mode. In the numeric mode, the file permissions aren't represented by characters. Instead, they are represented by a three-digit octal number.
= read (r)
2 = write (w)
1 = execute (x)
0 = no permission (-)
To get the permission bits you want, you add up the numbers accordingly. For example, the rwx permissions would be 4+2+1=7, rx would be 4+1=5, and rw would be 4+2=6. Because you set separate permissions for the owner, group, and others, you'll need a three-digit number representing the permissions of all these groups.
Let's have an example.
chmod 755 testfile
This would change the testfile's permissions to -rwxr-xr-x. The owner would have full read, write, and execute permissions (7=4+2+1), the group would have read and execute permissions (5=4+1), and the world would have the read and execute permissions as well.
Let's have another example:
chmod 640 testfile
In this case, testfile's permissions would be -rw-r-----. The owner would have read and write permissions (6=4+2), the group would have read permissions only (4), and the others wouldn't have any access permissions (0).
The numeric mode may not be as straightforward as the symbolic mode, but with the numeric mode, you can more quickly and efficiently set the file permissions. This quick reference for setting file permissions in numeric mode might help:
Which number?
0---
1--x
2-w-
3-wx
4r--
5r-x
6rw-
7rwx

Quản lý User, group và bảo mật trên linux


1. User

User là người có thể truy cập đến hệ thống.

User có username và password.

Có hai loại user: super user và regular user.

Mỗi user còn có một định danh riêng gọi là UID.

Định danh của người dùng bình thường sử dụng giá trị bắt đầu từ 500.

2. Group

Group là tập hợp nhiều user lại.

Mỗi user luôn là thành viên của một group.

Khi tạo một user thì mặc định một group được tạo ra.

Mỗi group còn có một định danh riêng gọi là GID.

Định danh của group thường sử dụng giá trị bắt đầu từ 500.

3. Tập lệnh quản lý User và Group

Tạo User:

Cú pháp:

#useradd [option] <username>
-c “Thông tin người dùng”
-d <Thư mục cá nhân>
-m : Tạo thư mục cá nhân nếu chưa tồn tại
-g <nhóm của người dùng>
Ví dụ: #useradd –c “Le Quoc Van – Server Admin” –g serveradmin quocvan

Thay đổi thông tin cá nhân:

Cú pháp:

#usermod [option] <username>
Những option tương tự Useradd

Ví dụ: #usermod –g kinhdoanh quocvan //chuyển quocvan từ nhóm server admin sang nhóm kinh doanh.

Xóa người dùng

Cúpháp :

#userdel [option] <username>
Vídụ : #userdel –r quocvan

Khóa/Mở khóa người dùng


passwd –l <username> / passwd –u <username>

usermod –L <username> /  usermod –U <username>
Trong /etc/shadow có thể khóa tài khoản bằng cách thay từ khóa x bằng từ khóa *.

Tạo nhóm:

Cú pháp:

#groupadd <groupname>
Ví dụ: #groupadd serveradmin

Xóa nhóm

Cú pháp:

#groupdel <groupname>
Ví dụ: #groupdel <serveradmin>

Xem thông tin về User và Group

Cú pháp:

#id <option> <username>
Ví dụ: #id -g quocvan //xem GroupID của user quocvan

Cú pháp:

 #groups <username>
Ví dụ: #groups quocvan //xem tên nhóm của user quocvan

4. Những file liên quan đến User và Group

#/etc/passwd
Mỗi dòng trong tập tin gồm có 7 trường, được phân cách bởi dấu hai chấm.

#/etc/group
Mỗi dòng trong tập tin gồm có 4 trường, được phân cách bởi dấu hai chấm.

#/etc/shadow
Lưu mật khẩu đã được mã hóa và chỉ có user root mới được quyền đọc.

5. Quyền hạn
Trong Linux có 3 dạng đối tượng :


·                 Owner (người sở hữu).
·                 Group owner (nhóm sở hữu).
·                 Other users (những người khác).

Các quyền hạn :


·                 Read – r – 4 : cho phép đọc nội dung.
·                 Write – w – 2 : dùng để tạo, thay đổi hay xóa.
·                 Execute – x – 1 : thực thi chương trình.

Vídụ : Với lệnh ls –l ta thấy :
[root@task ~]# ls -l
total 32
-rw-------. 1 root root  1416 Jan 10 14:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 15522 Jan 10 14:06 install.log
-rw-r--r--. 1 root root  5337 Jan 10 14:06 install.log.syslog
drwxr-xr-x  6 root root  4096 Feb  9 10:02 softs
Ngoài ra, chúng ta có thể dùng số.

Vídụ : quyền r, w, x : 4+2+1 = 7

Tổ hợp 3 quyền trên có giá trị từ 0 đến 7.
5. Các lệnh liên quan đến quyền hạn

Lệnh Chmod : dùng để cấp quyền hạn.

Cú pháp :
 

#chmod <specification> <file>
Ví dụ: #chmod 644 baitap.txt //cấp quyền cho owner có thể ghi các nhóm các chỉ có quyền đọc với file taptin.txt

Lệnh Chown : dùng thay đổi người sở hữu.

Cú pháp :
 

#chown <owner>  <filename>
Lệnh Chgrp : dùng thay đổi nhóm sở hữu.

Cú pháp :
 

#chgrp <group>  <filename>

"Set" hạn ngạch Disk Quota trên linux


Với một môi trường làm việc đa người dùng, việc giới hạn dung lượng đĩa sử dụng cho từng người dùng là cần thiết. Để thực hiện điều này, có thể có nhiều cách khác nhau, như là chia ổ đĩa cứng thành nhiều phân vùng nhỏ rồi gắn cho từng user. Cách này cũng tốt nhưng nó có nhiều nhược điểm là: thứ nhất khi các phân vùng gần đầy, việc ghi dữ liệu sẽ khó khăn; thứ hai là khi bạn cần thay đổi hạn ngạch thì sẽ khó khăn; thứ ba là đĩa cứng bị lãng phí; thứ tư là không hạn ngạch theo nhóm được. May mắn thay là bạn không cần làm như vậy, vì Linux cung cấp cho chúng ta một mô-đun là quota.
quota giúp bạn quản lý hạn ngạch đĩa cứng cho từng user cũng như group. Hạn ngạch cho group là tổng dung lượng đĩa cho các thành viên trong nhóm. Khi hạn ngạch này hết, thì cho dù user còn quota cũng không thể sử dụng.
Sử dụng quota thích hợp nhất có lẽ là cho thư mục /home. Giả sử bạn sử dụng /dev/sdb5 để gắn cho thư mục này.
1. Cấu hình để sử dụng quota
Cần cài đặt nếu máy chưa có:
Mã:
$ apt-get install quota
Để sử dụng quota cho /dev/sdb5 một cách tự động cho thư mục /home bạn cần khai báo thêm tuỳ chọn usrquota cho user và grpquota để áp dụng quota cho nhóm. Sửa file /etc/fstab từ:

Mã:
/dev/sdb5 /home ext4 defaults 1 2
thành:

Mã:
/dev/sdb5 /home ext4 defaults,usrquota,grpquota 1 2
Bạn cũng cần tạo file aquota.user, aquota.group lưu thông tin. Ở đây tôi tạo file aquota.user để đặt quota cho user làm ví dụ minh hoạ, với group bạn có thể làm tương tự.

Mã:
$ touch /home/aquota.user
Sau bước này bạn cần phải khởi động lại hệ điều hành. Có thể gắn lại sử dụng mount, nhưng không phải lúc nào cũng thành công, nhìn chung thì tốt nhất là nên khởi động lại.
2. Sử dụng quota
Khởi tạo bảng quota, bảng này chứa những thông tin file system đã được cấp phát cho từng user trong hệ thống. Bảng này sẽ tự động và trong suốt cập nhật thông tin mỗi khi user thao tác với file system. Hệ thống sẽ so sánh giá trị trong bảng này và giá trị quota mà bạn đã đặt cho các user trong quota file để xem xem user có thể sử dụng vượt quá không gian ổ cứng được phép hay không.
khởi tạo bảng quota bằng lệnh

Mã:
$ quotacheck -vagum
Để đặt giá trị quota cho user bạn dùng lệnh edquota . Ví dụ để đặt quota cho user nam bạn dùng lệnh

Mã:
$ edquota -u nam
thông tin hiện ra cho phép bạn xem xét và chỉnh sửa quota cho user tương ứng thông qua trình soạn thảo mạc định của hệ thống thường là vi. Để thay đổi sang nano bạn chạy lệnh export EDITOR=nano. Bạn cũng có thể thêm dòng này vào .bash_profile hay .bashrc.

Mã:
#Filesystem blocks soft hard inodes soft hard
/dev/sdb5 24 0 0 7 0 0
Blocks: thể hiện dung lượng ổ cứng mà user đang sử dụng, tính theo đơn vị block. Bạn không sửa trường dữ liệu này! Để biết kích thước của một block của phân vùng đĩa cứng, bạn sử dụng lệnh:

Mã:
$ dumpe2fs /dev/sdb5 | grep size
Inodes: thể hiện số lượng file tất nhiên là cả thư mục nữa của user đó. Bạn không sửa trường dữ liệu này!

Soft: thể hiện dung lượng ổ cứng lớn nhất mà user được phép dùng. Người dùng có thể sử dụng tạm thời vượt quá giá trị này nếu như Hard limit lớn hơn soft limit và chỉ trong thời gian được hạn định (grace periods tạm dịch là thời gian gia hạn). Khi đó nếu sử dụng quá dung lượng được phép trong giới hạn thời gian quy định bởi tham số “grace” user chỉ bị cảnh báo. Nếu vượt quá giá trị “grace” mà giá trị soft không được đặt lại, user sẽ không thể sử dụng thêm không gian ổ cứng.

Hard: thể hiện dung lượng ổ cứng lớn nhất mà user được phép dùng thông số này chỉ có ý nghĩa khi tham số “grade” được sử dụng. Nó thể hiện rằng user chỉ có thể vượt quá giá trị soft chứ không thể vượt quá giá trị hard (khi tham số grade được đặt).

Như bạn thấy có hai trường soft và hard. Cặp thứ nhất là dành cho block và cặp thứ hai dành cho node tức là dành cho số lượng file và thư mục. Nếu bạn là người mới bắt đầu thì chỉ nên sửa trường thứ ba và thứ tư mà thôi, và đặt chúng bằng nhau. Ví dụ bạn đặt thông tin quota cho user nam thành

Mã:
#Filesystem blocks soft hard inodes soft hard
/dev/sdb5 24 5000 5000 7 0 0
khi đó user nam không thể sử dụng dung lượng ổ cứng quá 5MB, trong trường hợp blocksize=1024.
Tham số “grace” thể hiện khoảng thời gian mà user có thể sử dụng dung lượng ổ cứng vượt quá giá trị Soft limit, mặc định nó thường là 7 ngày. Bạn có thể sử dụng các đơn vị thời gian như: seconds, minutes, hours, days, weeks, và months. Đặt giá trị cho tham số này bằng lệnh:
Mã:
$ edquota –tu # chỉnh cho user có thể bỏ -u
$ edquota –tg # chỉnh cho group

Filesystem Block grace period Inode grace period
/dev/sdb5 7days 7days
Bạn có thể ‘nhân bản’ quota, thật tiện lợi phải không nào. Ví dụ bạn có thể áp dụng quota của ‘nam’ cho ‘minh’:

Mã:
$ edquota -p nam -u minh
3. Xem thông tin về quota
Để xem thông tin quota bạn dùng lệnh quota. Ví dụ để xem thông tin quota của user nam bạn dùng lệnh

Mã:
$ quota -u nam # user nam
$ quota -g staff # nhóm staff Để thống kê thông tin quota về các nhóm và user bạn dùng

$ repquota -au # theo người dùng
$ repquota -ag # theo nhóm
$ repquota -agu # tất cả