MTU and fragmentation
6-byte MAC addresses and “//www.iana.org/assignments/ethernet-numbers for the first 3 bytes.
4-byte quad-dot, IP addrs, virtual Loopback addr,Ports, /etc/services, sockets, Privileged and Well-known ports
Address types –
Unicast – addrs that refer to a single host [NIC actually]
Multicast – addrs that identify a group of hosts [224 to 239] Good for videoconferencing
Broadcast – addrs that include all the hosts on the local network
Routing – Routing Tables, ICMP redirects
Static routing
/etc/sysconfig/static-routes : eth0 net 192.168.0.20 netmask 255.255.255.0 gw 192.168.0.10
netstat -r, netstat –rn
The destination is a NW address
The gateway must be a host address of a m/c which specifies that pkts must be sent to it to reach the destination address.
Gateway is the default route
Configuring static routes
route add –net 192.168.0.10 netmask 255.255.255.0 gw 192.168.0.10 eth1
route add default gw 192.168.0.10 eth0
/etc/sysconfig/network : GATEWAY=x.x.x.x and the default route
Local DNS and name resolution : and /etc/host.conf, /etc/nsswitch.conf
Dynamic routing – routed, gated
ICMP redirects
ARP – The Addr resolution protocol
mii-tool – Configuring autonegotiation and other media-specific options
Network Interfaces Config tools :
netconfig
netconf
netcfg
redhat-config-network
neat
neat-tui
neat-control
redhat-config-network-cmd
redhat-config-network-druid
Rsync [Remote Sync] là một công cụ dùng để sao chép và đồng bộ file/thư mục được dùng rất phổ biến. Với sự trợ giúp của rsync, bạn có thể đồng bộ dữ liệu trên local hoặc giữa các server với nhau một cách dễ dàng.
Nội dung bài viết
I. Tính năng nổi bật của Rsync
II. Cài đặt Rsync
III. Sử dụng Rsync
1. Copy file và thư mục trên local
2. Copy file và thư mục giữa các server
3. Rsync qua SSH
4. Hiển thị tiến trình trong khi transfer dữ liệu với rsync
5. Sử dụng tùy chọn –include và –exclude
6. Sử dụng tùy chọn –delete
7. Giới hạn dung lượng tối đa của file được đồng bộ
8. Tự động xóa dữ liệu nguồn sau khi đồng bộ thành công
9. Chạy thử nghiệm Rsync
10. Giới hạn bandwidth
IV. Tổng kết
I. Tính năng nổi bật của Rsync
- Rsync hỗ trợ copy giữ nguyên thông số của files/folder như Symbolic links, Permissions, TimeStamp, Owner và Group.
- Rsync nhanh hơn scp vì Rsync sử dụng giao thức remote-update, chỉ transfer những dữ liệu thay đổi mà thôi.
- Rsync tiết kiệm băng thông do sử dụng phương pháp nén và giải nén khi transfer.
- Rsync không yêu cầu quyền super-user.
II. Cài đặt Rsync
Rsync được cài đặt dễ dàng với một dòng lệnh:
– Trên Red Hat/CentOS
yum install rsync
– Trên Debian/Ubuntu
apt-get install rsysnc
III. Sử dụng Rsync
Câu lệnh căn bản của rsync:
rsync options source destination
Trong đó:
- Source: dữ liệu nguồn
- Destination: dữ liệu đích
- Options: một số tùy chọn thêm
Các tham số cần biết khi dùng Rsync
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/
4: hiển thị trạng thái kết quả
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/
5: copy dữ liệu recursively, nhưng không đảm bảo thông số của file và thư mục
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/
6: cho phép copy dữ liệu recursively, đồng thời giữ nguyên được tất cả các thông số của thư mục và file
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/
7: nén dữ liệu khi transfer, tiết kiệm băng thông tuy nhiên tốn thêm một chút thời gian
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/
8: human-readable, output kết quả dễ đọc
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/
9: xóa dữ liệu ở destination nếu source không tồn tại dữ liệu đó.
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/
0: loại trừ ra những dữ liệu không muốn truyền đi, nếu bạn cần loại ra nhiều file hoặc folder ở nhiều đường dẫn khác nhau thì mỗi cái bạn phải thêm
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/
0tương ứng.
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
Rsync không tự động chạy nên thường được dùng kết hợp với crontab. Tiếp theo mình sẽ giới thiệu một số ví dụ thường dùng với Rsync và kết thúc bài là script mình dùng để tự động backup toàn bộ VPS hàng ngày.
Khi lần đầu chạy rsync, toàn bộ dữ liệu nguồn sẽ được copy đến server đích, từ lần chạy sau trở đi chỉ những dữ liệu chưa được copy mới được transfer – đây là quá trình đồng bộ dữ liệu. Do đó, bạn có thể hiểu rsync thực hiện việc copy hoặc đồng bộ đều đúng. Trong bài viết mình sẽ sử dụng duy nhất khái niệm Copy cho ngắn gọn nhé.
1. Copy file và thư mục trên local
Copy file trên local
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Ví dụ trên copy file backup.tar sang thư mục /tmp/backups/ trên cùng một máy. Như bạn thấy thư mục đích chưa có nên rsync tự động tạo trước khi copy.
Copy thư mục trên local
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
Câu lệnh trên copy toàn bộ file từ thư mục
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/2 đến thư mục
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/3 trên cùng một máy.
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
2. Copy file và thư mục giữa các server
Copy thư mục từ Local lên Remote Server
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
Lệnh trên copy thư mục
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/4 từ Local lên Remote Server có IP
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/5, lưu ở thư mục
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/6
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
Copy thư mục từ Remote Server về Local
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
Lệnh trên sẽ copy dữ liệu ở thư mục
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/7 trên Remote Server
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/8về máy Local lưu ở thư mục
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/9
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
3. Rsync qua SSH
Với Rsync, bạn có thể transfer qua giao thức SSH, qua đó dữ liệu được bảo mật an toàn hơn.
Copy file từ Remote Server về Local Server qua SSH
Để xác định giao thức sẽ sử dụng với rsync, bạn cần thêm tùy chọn
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms0 cùng với tên giao thức, ở đây là
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms1.
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
Lệnh trên copy file
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms2 trên Remote Server
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
[root@hocvps]# rsync -avz rpmpkgs/ root@192.168.0.101:/home/8 về thư mục
root@192.168.0.101's password:
sending incremental file list
./
httpd-2.2.3-82.el5.centos.i386.rpm
mod_ssl-2.2.3-82.el5.centos.i386.rpm
nagios-3.5.0.tar.gz
nagios-plugins-1.4.16.tar.gz
sent 4993369 bytes received 91 bytes 399476.80 bytes/sec
total size is 4991313 speedup is 1.00
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms4 trên máy Local.
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
Copy file từ Local lên Remote Server qua SSH
[root@hocvps]# rsync -avzhe ssh backup.tar root@192.168.0.100:/backups/
root@192.168.0.100's password:
sending incremental file list
backup.tar
sent 14.71M bytes received 31 bytes 1.28M bytes/sec
total size is 16.18M speedup is 1.10
4. Hiển thị tiến trình trong khi transfer dữ liệu với rsync
Để hiển thị tiến độ transfer dữ liệu, bạn có thể sử dụng tùy chọn
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms5
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
[root@hocvps]# rsync -avzhe ssh --progress /home/rpmpkgs root@192.168.0.100:/root/rpmpkgs
root@192.168.0.100's password:
sending incremental file list
created directory /root/rpmpkgs
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
1.02M 100% 2.72MB/s 0:00:00 [xfer#1, to-check=3/5]
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
99.04K 100% 241.19kB/s 0:00:00 [xfer#2, to-check=2/5]
rpmpkgs/nagios-3.5.0.tar.gz
1.79M 100% 1.56MB/s 0:00:01 [xfer#3, to-check=1/5]
rpmpkgs/nagios-plugins-1.4.16.tar.gz
2.09M 100% 1.47MB/s 0:00:01 [xfer#4, to-check=0/5]
sent 4.99M bytes received 92 bytes 475.56K bytes/sec
total size is 4.99M speedup is 1.00
5. Sử dụng tùy chọn –include và –exclude
Hai tùy chọn này cho phép chúng ta thêm hoặc bớt file hoặc thư mục trong quá trình đồng bộ dữ liệu.
apt-get install rsysnc0
Ở ví dụ trên, Rsync include toàn bộ những file hoặc thư mục có tên bắt đầu bởi ký tự ‘R’ và exclude toàn bộ những file hoặc thư mục còn lại.
6. Sử dụng tùy chọn –delete
Nếu muốn xóa một file hoặc thư mục không có ở thư mục nguồn, mà lại xuất hiện ở thư mục đích trong quá trình transfer, bạn hãy sử dụng tùy chọn
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/9.
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
apt-get install rsysnc1
Server đích đã có file
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms7, trong quá trình đồng bộ với option
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/9, file sẽ bị xóa.
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
7. Giới hạn dung lượng tối đa của file được đồng bộ
Để giới hạn những file lớn được đồng bộ, bạn có thể sử dụng option
[root@hocvps]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms9
root@192.168.0.100's password:
receiving incremental file list
created directory /tmp/myrpms
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 91 bytes received 4.99M bytes 322.16K bytes/sec
total size is 4.99M speedup is 1.00
apt-get install rsysnc2
8. Tự động xóa dữ liệu nguồn sau khi đồng bộ thành công
Để rsync tự động xóa dữ liệu sau khi đồng bộ lên server đích thành công, bạn có thể sử dụng lựa chọn
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/0
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
apt-get install rsysnc3
9. Chạy thử nghiệm Rsync
Nếu bạn không chắc câu lệnh có thực hiện chính xác những gì mong muốn hay không, hãy thêm tùy chọn
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/1.
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
Rsync lúc này sẽ không thay đổi gì dữ liệu cả mà chỉ show output mà thôi. Nếu mọi thứ hoạt động ổn, hãy bỏ tùy chọn
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/1 ra khỏi câu lệnh.
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
apt-get install rsysnc4
10. Giới hạn bandwidth
apt-get install rsysnc5
IV. Tổng kết
Ứng dụng của Rsync có rất nhiều, bạn có thể đồng bộ hóa file giữa các thư mục, giữa các server qua đó backup server sang một server khác hoặc synchronize real time. Tùy nhu cầu mà bạn hãy ứng dụng Rsync cho hiệu quả.
Chúc bạn thành công.
nguồn : //hocvps.com/rsync/
Những biểu hiện thường gặp server hosing đang chứa mã độc là thực hiện gửi spam mail, brute force ssh, DOS… tới server khác. Thông thường bạn sẽ nhận được cảnh báo này từ nhà cung câp dịch vụ server, vps… Sau đây là một số thủ thuật giúp bạn nhanh chóng phát hiện tài khoản nào đang bị hacker kiểm soát.
1 Kiểm tra bằng lệnh top
Trên server hosting sẽ có nhiều tài khoản chạy đồng thời. Tài khoản bị tấn công thường xuất hiện liên tục ở những hàng đầu khi gõ lệnh top. Ở cột command sẽ xuất hiện những lệnh thường gặp như perl, python, host, mail…
Hình bên trên cho biết tài khoản noithat đang chạy các tiến trình perl
2 Kiêm tra file log apache
Các tài khoản hosting chữa mã độc thường được hacker điều khiển bằng cách gửi POST request tới website.
Một tài khoản đang bị tấn công thông qua đường dẫn //domain.com/wp-content/themes/saladmag/css/general19.php
3 Tìm kiếm các file nghi vấn khác trên tài khoản user.
Tìm kiếm các file mới được up lên.
# cd /home/noithat/public_html
# find . -name “*.php” -mtime -14
Tìm kiếm các script có khả năng chữa mã độc.
# find . -type f -name ‘*.php’ | xargs egrep -i “[mail|fsockopen|pfsockopen|stream_socket_client|exec|system|passthru|base64_decode] *\[”
4 Tìm kiếm mã độc thông qua những email đang gửi spam
Các tài khoản bị tấn công đa phần sẽ thực hiện gửi spam mail. Do đó việc kiểm tra script đang gửi spam mail sẽ tìm ra được vị trí mã nguồn bị tấn công.
nguồn: //hotrovps.info/linux/tim-kiem-ma-doc-tren-server-hosting-linux.html
Today’s Blog is all about networking stuffs and I will be covering following points:
- Know your Network Controller Device with lspci
- Display all interfaces which are currently available, even if down with ifconfig
- Enable the service networking
- Enable Apache server
- Then Enable Network Manager with network-manager
- Then Enable your WiFi with nmcli nm
AND REMEMBER ALL THESE THINGS WILL BE DONE WITH OUR TERMINAL THIS IS WHY LINUX IS SO COOL…
Then we are all set to run the internet.And now we are some basic troubleshooter about networking, though networking is a huge this would be the first step. Not actually first first step in networking is always theory The Basics of Networking, but we are not dealing with it today. So lets begin
1. In order to know the hardware devices assembled in your laptop open your TERMINALCTRL+ALT+T and then type lspci .
This displays all the hardware devices like PCI BRIDGE, SATA controller, SMBus, USB Controller, Audio Device assembled in your laptop / computer.
My Network controller is:
02:00.0 Network controller: Broadcom Corporation BCM4313 802.11bgn Wireless Network Adapter [rev 01]
2. So now we need to know the interfaces that are available in our UBUNTU. To see this typeifconfig in the terminal and you will see the output like this :
3. To access the internet first your hardware device should be activated, In order to do so type
sudo service networking and press tab twice which displays arguments like:
force-reload reload restart start stop
Perform any you need.
4. To enable apache server in terminal type:
sudo service apache2 start
5. Now enable the network manager. In order to do so in terminal type:
sudo service network-manager start
6. Now First view the available connecting points Wireless point or Ethernet. To view the available wifi points in terminal type
sudo nmcli con list
This displays the available devices With its UID and SSID.
Now to enable the wifi in terminal type:
Sudo nmcli con up SSID or the UID
You can manipulate these basic networking commands and triubleshoot on your own.
source: //www.techjhola.com/2015/05/troubleshoot-basic-networking-wifi.html
Khái niệm facility level và secrity level:
Facility level: đại diện cho đối tượng tạo ra thông báo [kernel, process].
acility NumberKeywordFacility Description0kemkernel messages1useruser-level messages2mailmail system3daemonsystem daemons4authsecurity/authorization messages5syslogmessages generated internally by syslog6lprline printer subsystem7newsnetwork news subsystem8uucpUUCP subsystem9–clock daemon10authprivsecurity/authorization messages11ftpFTP daemon12–network news subsystem13–log audit14–log alert15cronclock daemon16local0local use 0 [local0]17local1local use 1 [local1]18local2local use 2 [local2]19local3local use 3 [local3]20local4local use 4 [local4]21local5local use 5 [local5]22local6local use 6 [local6]23local7local use 7 [local7]
Việc sử dụng facility cho phép chúng ta kiểm soát log được dễ dàng hơn, dựa vào nguồn gốc của nó.
Severity levels: xác định một ngưỡng mà messages sẽ được logged
CodeSeverityKeywordDescription0Emergencyemerg [panic]System is unstable1AlertalertAction must be taken immediately2CriticalcritCritical conditions3Errorerr [error]Error conditions4Warningwarning [warn]Warning conditions5NoticenoticeNormal but significant condition6InformationalinfoInformational messages7DebugdebugDebug-level messages
Ngoài ra còn một mức đặc biệt được gọi là none, mức này sẽ disable facility đi cùng . Severity levels định nghĩa một số lượng các bản ghi chi tiết trong log file. Dấu sao[*] có thể được sử dụng để miêu tả cho tất cả cácfacilities level hoặc tất cả Severity levels.
Cài đặt syslog-ng:
Yêu cầu:
· Hệ điều hành CentOS 6.x trở lên
· Kết nối internet
Cài đặt EPEL repo:
Code:
apt-get install rsysnc6
kiểm tra lại EPEL repo đã cài đặt thành công hay không
Code:
apt-get install rsysnc7
cài đặt syslog-ng
Code:
apt-get install rsysnc8
Nếu hệ thống yêu cầu import GPG key, gõ y để đồng ý
Code:
apt-get install rsysnc9
Hệ thống đã cài đặt thành công syslog-ng. Nhưng chưa thể chạy được vì mặc định hệ thống đang dùng rsylog.
Cấu hình hệ thống dùng syslog-ng:
tắt rsyslog
Code:
rsync options source destination0
Kiểm tra lại kết quả
Code:
rsync options source destination1
bật syslog-ng
Code:
rsync options source destination2
kiểm tra lại kết quả
Code:
rsync options source destination3
Stop Rsyslog
Code:
rsync options source destination4
Start Syslog-ng
Code:
rsync options source destination5
Cấu hình syslog-ng nhận log từ các thiết bị trong mạng:
Thêm dòng cấu hình sau vào phần cuối của file cấu hình syslog-ng /etc/syslog-ng/syslog-ng.conf:
Code:
rsync options source destination6
với cấu hình trên hệ thống sẽ nhận log từ tất cả các thiết bị trong mạng với địa chỉ ip nguồn là bất kỳ, trên port 514, giao thức tcp và upd. Và sẽ đặt file log của thiết bị trong thư mục /var/log/syslog. Tên của file log sẽ được đặt dựa vào tên của máy chủ và thời gian hiện hành của hệ thống.
khởi động lại dịch vụ syslog-ng để thực hiện các thay đổi.
Code:
rsync options source destination7
Trong bài viết sau sẽ hướng dẫn các bạn cấu hình forward logs từ Windows & Linux đến syslog-ng server.
Trên hệ điều hành Linux
Sử dụng rsyslog.
Mở file rsyslog.conf và thêm dòng cấu hình sau vào cuối file cấu hình:
Code:
rsync options source destination8
Khởi động lại dịch vụ để các thay đổi được thực hiện
Code:
rsync options source destination9
Sử dụng syslog-ng.
Mở file cấu hình syslog-ng.conf, xóa toàn bộ nội dung bên trong file này và thêm vào phần cấu hình bên dưới.
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/0
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Lưu ý: Trong file cấu hình trên server sẽ gởi log đến remote server bằng giao thức tcp. Nếu muốn dùng giao thức udp thay cho giao thức tcp chúng ta cần sửa phần cấu hình destination syslog-ng_server thành
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/1
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Khởi động lại dịch vụ để các thay đổi được thực hiện
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/2
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Kiểm tra ghi log
Chạy lệnh bên dưới để kiểm tra việc ghi log đến server syslog-ng trung tâm.
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/3
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Trên server log trung tâm chạy lênh sau để kiểm tra quá trình ghi log có thành công hay không. Nếu không thì kiểm tra lại kết nối mạng và cấu hình tường lửa.
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/4
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Trên hệ điều hành windows.
Download ứng dụng Evtsys theo link sau.
//shink.in/7yRdX
Sau khi download file, chúng ta tiến hành giải nén.
Mở command line và di chuyển vào thư mục file được giải nén, chạy lệnh sau.
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/5
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Tham khảo thêm phần cấu hình Syslog-ng theo link sau: //svuit.com/showthread.php?718-…3%AAn-CentOS-6
Cấu hình xoay vòng log file
Phần lớn các bản phân phối sẽ cài đặt một cấu hình syslog mặc định cho hệ thống, bao gồm logging to messages và các log file khác trong thư mục /var/log. Để ngăn cản nhưng file này ngày càng lớn trở nên cồng kềnh và có thể làm đầy ổ cứng gây ảnh hưởng đến các dịch vụ khác đang chạy. một hệ thống xoay vòng log [log rotation] nên được cài đặt. Hệ thống cron đưa ra các lệnh để thiết lập những log files mới, những log file cũ được đổi tên bằng cách thay một con số ở hậu tố. ví dụ khi có sự xoay vòng log xảy ra file /var/log/messages sẽ được chuyển thành /var/log/messages.1 và hệ thống sẽ tạo mới một file /var/log/mesages. Sự xoay vòng này có thể được cấu hình cho một số lượng lớn các file. Tùy theo cấu hình mà các file này sẽ được lưu lại bao nhiêu bản cũ nhất trên server trước khi bị xóa đi.
Tiện ích thi hành rotation là logrotate. Lệnh này được cấu hình sử dụng cho một hoặc nhiều files – được xác định bởi các tham số đi cùng. File cấu hình mặc định là /etc/logrotate.conf
File cấu hình logrotate sẽ tương tự như bên dưới
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/6
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Có hai kiểu xoay vòng log thường được sử dụng:
- Xoay vòng log theo dung lượng file
- Xoay vòng log theo thời gian
Mỗi kiểu đều có ưu nhược điểm riêng. Tùy thuộc vào đặc điểm của từng hệ thống mà ta chọn kiểu xoay vòng log thích hợp
Để cấu hình xoay vòng log cho các dịch vụ ta sẽ đặt các file cấu hình xoay vòng log trong thư mục /etc/logrotate.d
Ví dụ: Cấu hình xoay vòng log cho log của web server apache ta làm như sau.
Tạo file cấu hình cấu hình xoay vòng log
Vim /etc/logrotate.d/httpd
Thêm phần cấu hình bên dưới vào file cấu hình.
Code:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/7
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Size 70M: hệ thống sẽ xoay vòng log khi dung lượng file log lớn hơn 70M. Tại đây chúng ta cũng có thể cấu hình xoay vòng log dựa trên thời gian bằng cách dùng các chỉ thị sau [daily, weekly, monthly]. Để xoay vòng log theo ngày, tuần và tháng.
Compress: nén các file log cũ theo chuẩn gzip. Mục đích giảm không gian đĩa cứng
Dateext: ngày sẽ được thêm vào trong tên file mới.
Rotate 7: hệ thống chỉ lưu giữ 7 file log trên server. Các file cũ hơn sẽ được xóa bỏ khỏi hệ thống
Misingok: hệ thống sẽ không tạo ra lỗi. Nếu tập tin không được tìm thấy.
Sharedscripts: chạy các script bất kỳ trước [prerotate] hoặc sau khi [postrotate] xoay vòng cho mỗi file log.
Tham khảo thêm tại: //linuxcommand.org/man_pages/logrotate8.html
Mình thi thoảng phải quản lý linux server, mà trong đó có một số thao tác quản lý quan trong như quản lý đĩa cứng, quản lý mạng, quản lý đường truyền mà nếu phải tự tìm thông tin mà không có tool sẽ rất bất tiện. Ở dưới đây mình sẽ giới thiệu một sô tool quản lý mà cá nhân mình thấy “siêu tiện” với những bạn nào hay phải mày mò trên server linux:
iotop : quản lý disk IO
Cài đặt:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/8
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Sau khi cài đặt xong thì bạn chỉ cần khởi động bằng lệnh
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/3 sẽ thấy một màn hình như sau:
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
Từ màn hình trên thì chúng ta có thể thấy câu lệnh
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/4 đang “ngốn” disk IO [read] với tốc độ đọc 9.4M 1s, và từ đó đưa ra được chính sách thích hợp
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
iftop: quản lý đường truyền:
Cài đặt:
[root@hocvps]# rsync -zvh backup.tar /tmp/backups/9
created directory /tmp/backups
backup.tar
sent 14.71M bytes received 31 bytes 3.27M bytes/sec
total size is 16.18M speedup is 1.10
Cũng như trên bạn chỉ cần khởi động bằng :
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/5, sẽ cho chúng ta màn hình như dưới đây:
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
Bạn có thể thấy rất trực quan kết nối nào, port nào đang ngốn bao nhiêu đường truyền của bạn.
Bạn có thể dùng option
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/6 để chỉ định ethernet device:
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/0
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
htop: quản lý tổng quan
Chắc tool này đã quá quen thuộc với mọi người rồi nên mình không cần phải nói thêm nhiều. Mình hay sử dụng htop để xem các thông số chính như load average và số lượng core được sử dụng.
Cách cài đặt và khởi động cũng tương tự như trên, bạn có thể sử dụng yum:
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/1
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
ohmu : tool để visualize dung lượng đĩa cứng
Đây là một tiện ích rất cool để bạn xem được phân phối đĩa cứng của bạn, xem phần mềm nào đang “ngốn” dung lượng nhất.
Bạn cài đặt thông qua python:
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/2
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
Sau khi cài đặt xong chỉ cần khởi động
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/7. Kết quả là một màn hình rất cool và dễ hiểu về phân phối dung lượng đía cứng:
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
Hy vọng bài viết sẽ có ích cho các bạn
nguon: //kipalog.com/
Thông thường Varnish sử dụng chính để làm cache front end, tuy nhiên bạn hoàn toàn có thể cấu hình sử dụng để chống DDoS.
Hướng dẫn này viết từ trường hợp thực tế, mình đã chống DDoS thành công cho một người bạn. Nếu bạn cũng đang đau đầu về việc bị DDoS, hãy tham khảo bài viết này biết đâu có thể áp dụng được.
Ngay khi kết nối SSH, mình kiểm tra file access.log thì thấy ngay đúng là server đang bị DDoS.
Một loạt request có cách thức giống nhau, cùng một user-agent, IP khác nhau. Tính sơ sơ cũng phải đến cả trăm request mỗi giây, hỏi sao server không bị overload.
Nếu sử dụng CSF để block từng IP thì không ổn, vì quá nhiều IP. May thay các request này đều có một điểm chung là user-agent trong header giống nhau, như vậy có thể block lại được toàn bộ những request này dễ dàng.
Do CSF không hỗ trợ block theo header, nên mình phải sử dụng Varnish để filter riêng những request này ra và trả về Error 403. Với một đoạn code ngắn bên dưới để trong block
[root@hocvps]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/8, mình đã khống chế hoàn toàn được vụ DDoS.
root@192.168.0.100's password:
receiving incremental file list
install.log
sent 30 bytes received 8.12K bytes 1.48K bytes/sec
total size is 30.74K speedup is 3.77
[root@hocvps]# rsync -avzh /root/rpmpkgs /tmp/backups/3
sending incremental file list
rpmpkgs/
rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm
rpmpkgs/nagios-3.5.0.tar.gz
rpmpkgs/nagios-plugins-1.4.16.tar.gz
sent 4.99M bytes received 92 bytes 3.33M bytes/sec
total size is 4.99M speedup is 1.00
Ý nghĩa của đoạn code trên là filter những request có user-agent chứa Hotbar, nếu có thì trả lại lỗi 403, ngược lại thì pass cho Nginx webserver xử lý.