huong dan cau hinh clustering

22
Kết nối từ xa với Linux sử dụng ssh. Posted by hmha on Tháng Chín 13, 2008 SSH-Secure Shell: là một protocol để kết nối an toàn với một máy tính khác qua mạng. Vâng, quan trọng là an toàn. Cài đặt OpenSSH trên debian: # apt-get install ssh Sau khi cài đặt thành công bạn có thể thực hiện kết nối từ *BSD, Linux… và Windows vào server Linux. Với client là Windows tôi dùng putty . Nếu bạn không dùng Debian, hãy nói với tôi, tôi sẽ chỉ cho bạn cách cài OpenSSH trên hệ thống của bạn. Tập tin cấu hình ssh server trên debian là: /etc/ssh/sshd_config. Để kết nối tới server chúng ta sử dụng lệnh: $ssh -p 8888 [email protected] hãy nhập mật khẩu của người dùng debian và bạn đã trên máy 192.168.1.1. Trong câu lệnh trên -p 8888 là cổng để kết nối, tham số này được xác định trong tập tin cấu hình ssh server: /etc/ssh/sshd_config. Tuy nhiên, bạn là một người ưu thích X, bạn muốn chạy các ứng dụng như pidgin, gaim … thì phải làm sao? Đơn giản, trước hết hãy bảo đảm trong tập tin cấu hình của server có chứa đoạn sau: X11Forwarding yes X11DisplayOffset 10 sau đó cũng sử dụng câu lệnh trên chỉ thêm một tham số là -X: $ssh -p 8888 -X [email protected] nhập mật khẩu vào và làm những gì bạn muốn: $pidgin & $gaim & ... hãy nhớ rằng bạn đang làm việc với pidgin, gaim … trên máy ở xa đấy nhé. Thiết lập danh sách người dùng, host được phép truy cập từ xa: Ví dụ sau cho phép người dùng debian và cấm người dùng naibed. AllowUsers debian

Upload: linhmta

Post on 24-Oct-2014

198 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: Huong Dan Cau Hinh Clustering

Kết nối từ xa với Linux sử dụng ssh.

Posted by hmha on Tháng Chín 13, 2008

SSH-Secure Shell: là một protocol để kết nối an toàn với một máy tính khác qua mạng. Vâng, quan trọng là an toàn.

Cài đặt OpenSSH trên debian:

# apt-get install ssh

Sau khi cài đặt thành công bạn có thể thực hiện kết nối từ *BSD, Linux… và Windows vào server Linux. Với client là Windows tôi dùng putty.Nếu bạn không dùng Debian, hãy nói với tôi, tôi sẽ chỉ cho bạn cách cài OpenSSH trên hệ thống của bạn.Tập tin cấu hình ssh server trên debian là: /etc/ssh/sshd_config.Để kết nối tới server chúng ta sử dụng lệnh:$ssh -p 8888 [email protected]

hãy nhập mật khẩu của người dùng debian và bạn đã trên máy 192.168.1.1. Trong câu lệnh trên -p 8888 là cổng để kết nối, tham số này được xác định trong tập tin cấu hình ssh server: /etc/ssh/sshd_config.Tuy nhiên, bạn là một người ưu thích X, bạn muốn chạy các ứng dụng như pidgin, gaim … thì phải làm sao? Đơn giản, trước hết hãy bảo đảm trong tập tin cấu hình của server có chứa đoạn sau:X11Forwarding yesX11DisplayOffset 10

sau đó cũng sử dụng câu lệnh trên chỉ thêm một tham số là -X:$ssh -p 8888 -X [email protected]

nhập mật khẩu vào và làm những gì bạn muốn:$pidgin &$gaim &...

hãy nhớ rằng bạn đang làm việc với pidgin, gaim … trên máy ở xa đấy nhé.

Thiết lập danh sách người dùng, host được phép truy cập từ xa:

Ví dụ sau cho phép người dùng debian và cấm người dùng naibed.AllowUsers debianDenyUsers naibed

(thêm vào tập tin cấu hình của server)

Sao chép dữ liệu an toàn qua ssh:

$scp file.txt [email protected]:~/

Với câu lệnh trên tập tin file.txt trên client sẽ được copy đến thư mục cá nhân của người dùng debian trên server ở xa.$scp [email protected]:~/file.txt .

Còn trong trường hợp này tập tin file.txt của người dùng debian trên server ở xa sẽ được copy về thư mục hiện tại cuả client.

Page 2: Huong Dan Cau Hinh Clustering

Kết nối không mật khẩu:

ssh cung cấp cho chúng ta một phương pháp kết nối khác đó là không cần mật khẩu mà sử dụng một cặp khoá công khai và riêng tư. Trước hết hãy tạo cặp khoá đó trên máy client. Sử dụng thuật toán dsa.

$ ssh-keygen -t dsaGenerating public/private dsa key pair.Enter file in which to save the key (/home/user/.ssh/id_dsa):Created directory '/home/user/.ssh'.Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /home/user/.ssh/id_dsa.Your public key has been saved in /home/user/.ssh/id_dsa.pub.The key fingerprint is:93:b6:94:f0:45:34:4d:2b:12:d7:df:17:51:22:50:cf [email protected] key's randomart image is:+--[ DSA 1024]----+|        ..**+ ..+||         + .o= o ||      . . o ..E..||       o = .  . o||        S       .||       o o       ||        .        ||                 ||                 |+-----------------+

Sau khi quá trình kết thúc 2 tập tin được tạo ra trong ~/.ssh là:-rw------- 1 user client 672 Сен 14 13:20 id_dsa-rw-r--r-- 1 user client 603 Сен 14 13:20 id_dsa.pub

Tiếp theo hãy copy khoá công khai của bạn lên server: id_dsa.pub.$ scp -P 8888 ~/.ssh/id_dsa.pub [email protected]:.ssh/authorized_keys

Tập tin authorized_keys dùng để kiểm tra khi ta muốn kết nối. Bây giờ để kết nối đến server ta chỉ cần thực hiện lệnh:$ ssh -p 8888 [email protected]

Linux and Unix rsync command

Quick links

About rsyncSyntaxExamples

Page 3: Huong Dan Cau Hinh Clustering

Related commandsLinux and Unix main page

About rsync

Faster, flexible replacement for rcp.

Syntax

rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST

rsync [OPTION]... [USER@]HOST:SRC DEST

rsync [OPTION]... SRC [SRC]... DEST

rsync [OPTION]... [USER@]HOST::SRC [DEST]

rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST

rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

-v, --verbose increase verbosity

-q, --quiet suppress non-error messages

-c, --checksum skip based on checksum, not mod-time & size

-a, --archive archive mode; same as -rlptgoD (no -H)

-r, --recursive recurse into directories

-R, --relative use relative path names

--no-relative turn off --relative

--no-implied-dirs don't send implied dirs with -R

-b, --backup make backups (see --suffix & --backup-dir)

--backup-dir=DIR make backups into hierarchy based in DIR

Page 4: Huong Dan Cau Hinh Clustering

--suffix=SUFFIX backup suffix (default ~ w/o --backup-dir)

-u, --update skip files that are newer on the receiver

--inplace update destination files in-place

-d, --dirs transfer directories without recursing

-l, --links copy symlinks as symlinks

-L, --copy-links transform symlink into referent file/dir

--copy-unsafe-links only "unsafe" symlinks are transformed

--safe-links ignore symlinks that point outside the tree

-H, --hard-links preserve hard links

-K, --keep-dirlinks

treat symlinked dir on receiver as dir

-p, --perms preserve permissions

-o, --owner preserve owner (root only)

-g, --group preserve group

-D, --devices preserve devices (root only)

-t, --times preserve times

-O, --omit-dir-times

omit directories when preserving times

-S, --sparse handle sparse files efficiently

-n, --dry-run show what would have been transferred

Page 5: Huong Dan Cau Hinh Clustering

-W, --whole-file copy files whole (without rsync algorithm)

--no-whole-file always use incremental rsync algorithm

-x, --one-file-system

don't cross filesystem boundaries

-B, --block-size=SIZE

force a fixed checksum block-size

-e, --rsh=COMMAND

specify the remote shell to use

--rsync-path=PROGRAM specify the rsync to run on remote machine

--existing only update files that already exist

--ignore-existing ignore files that already exist on receiver

--remove-sent-files sent files/symlinks are removed from sender

--del an alias for --delete-during

--delete delete files that don't exist on sender

--delete-before receiver deletes before transfer (default)

--delete-during receiver deletes during xfer, not before

--delete-after receiver deletes after transfer, not before

--delete-excluded also delete excluded files on receiver

--ignore-errors delete even if there are I/O errors

--force force deletion of dirs even if not empty

--max-delete=NUM don't delete more than NUM files

--min-size=SIZE don't transfer any file smaller than SIZE

--max-size=SIZE don't transfer any file larger than SIZE

--partial keep partially transferred files

--partial-dir=DIR put a partially transferred file into DIR

--delay-updates put all updated files into place at end

Page 6: Huong Dan Cau Hinh Clustering

--numeric-ids don't map uid/gid values by user/group name

--timeout=TIME set I/O timeout in seconds

-I, --ignore-times

don't skip files that match size and time

--size-only skip files that match in size

--modify-window=NUM compare mod-times with reduced accuracy

-T, --temp-dir=DIR

create temporary files in directory DIR

-y, --fuzzy find similar file for basis if no dest file

--compare-dest=DIR also compare received files relative to DIR

--copy-dest=DIR ... and include copies of unchanged files

--link-dest=DIR hardlink to files in DIR when unchanged

-z, --compress compress file data during the transfer

-C, --cvs-exclude

auto-ignore files in the same way CVS does

-f, --filter=RULE add a file-filtering RULE

-F same as --filter='dir-merge /.rsync-filter'repeated: --filter='- .rsync-filter'

--exclude=PATTERN exclude files matching PATTERN

--exclude-from=FILE read exclude patterns from FILE

--include=PATTERN don't exclude files matching PATTERN

--include-from=FILE read include patterns from FILE

--files-from=FILE read list of source-file names from FILE

-0, --from0 all *from file lists are delimited by nulls

--version print version number

Page 7: Huong Dan Cau Hinh Clustering

--port=PORT specify double-colon alternate port number

--blocking-io use blocking I/O for the remote shell

--no-blocking-io turn off blocking I/O when it is default

--stats give some file-transfer stats

--progress show progress during transfer

-P same as --partial --progress

-i, --itemize-changes

output a change-summary for all updates

--log-format=FORMAT log file-transfers using specified format

--password-file=FILE read password from FILE

--list-only list the files instead of copying them

--bwlimit=KBPS limit I/O bandwidth; KBytes per second

--write-batch=FILE write a batched update to FILE

--read-batch=FILE read a batched update from FILE

--protocol=NUM force an older protocol version to be used

--checksum-seed=NUM set block/file checksum seed (advanced)

-4, --ipv4 prefer IPv4

-6, --ipv6 prefer IPv6

-h, --help show this help screen

Rsync can also be run as a daemon, in which case the following options are accepted:

--daemon run as an rsync daemon

--address=ADDRESS

bind to the specified address

Page 8: Huong Dan Cau Hinh Clustering

--bwlimit=KBPS limit I/O bandwidth; KBytes per second

--config=FILE specify alternate rsyncd.conf file

--no-detach do not detach from the parent

--port=PORT listen on alternate port number

-v, --verbose increase verbosity

-4, --ipv4 prefer IPv4

-6, --ipv6 prefer IPv6

-h, --help show this help screen

Examples

rsync -t *.htm hope:public_html/

The above example would use rsync to transfer all .htm files to the public_html directory on the hope computer.

Related commands

rcp

-RSync là công cụ để sao chép dữ liệu giữa các máy tình (hay giữa các thư mục trên localhost). Nhưng không chỉ có vậy. Giả sử bạn có một đống tài liệu trên máy A, bạn muốn hàng ngày vào lúc nào đó sẽ sao chép chúng đến máy B để lưu trữ, tuy nhiên nếu sao chép theo kiểu thông thường như là sử dụng SSH thì tất cả các file sẽ được sao chép ghi đè lên (trong trường hợp file đã tồn tại) mặc dù file đó không có gì thay đổi và như vậy sẽ tốn thời gian hơn. Còn với RSync – chỉ sao chép những file đã có sự

Page 9: Huong Dan Cau Hinh Clustering

thay đổi, do đó quá trình diễn ra sẽ nhanh hơn. Ngoài ra với việc sử dụng RSync qua ssh tất cả những gì bạn sao chép sẽ được mã hoá, điều này có ích khi bạn sao chép qua internet.-Trước hết hãy cấu hình ssh đ ể s ử d ụ ng k ế t n ố i không m ậ t kh ẩ u .

-Đồng bộ dữ liệu:rsync -axvz --delete --rsh='ssh -p8888' /path/on/A user@remote:/path/trong ví dụ trên chúng ta dùng rsync qua ssh với cổng 8888tuỳ chọn -axvz nghĩa là sao hết mọi thứ (tất nhiên trong thư mục chỉ định) với chế độ nén và hiển thị lên terminal.–delete sẽ xoá những file trên remote host mà trên máy A không có.và như thường lệ hãy man rsync.-Để tự động hoá quá trình hãy sử dụng với cron. Nếu bạn chưa biết cách sử dụng cron có thể tìm hiểu qua ví dụ ở đây (cron+wget)

Synchronize files on two web servers with rsync

October 1, 2010

On Wbhost01

useradd -d /home/rsyncuser -m -s /bin/bash rsyncuserpasswd rsyncuser9&kupAsw

On webhost02rsync -avz -e ssh [email protected]:/var/www/html/ /var/www/html/

Create The Keys On webhost02mkdir /root/rsyncssh-keygen -t dsa -b 1024 -f /root/rsync/mirror-rsync-key

scp /root/rsync/mirror-rsync-key.pub rsyncuser@webhost01:/home/rsyncuser/

Configure webhost01Now log in through SSH on webhost01 as  rsyncuser(not root!)mkdir ~/.sshchmod 700 ~/.sshmv ~/mirror-rsync-key.pub ~/.ssh/cd ~/.sshtouch authorized_keyschmod 600 authorized_keyscat mirror-rsync-key.pub >> authorized_keys

right at the beginning of /home/rsyncuser/.ssh/authorized_keys: Add

vi /home/rsyncuser/.ssh/authorized_keys

Page 10: Huong Dan Cau Hinh Clustering

command=”/home/rsyncuser/rsync/checkrsync”,from=”webhost02″,no-port-forwarding,no-X11-forwarding,no-pty ssh-dss AAAAB3NzaC1kc3MAAA[...]lSUom root@webhost02

Now create the script /home/rsyncuser/rsync/checkrsync that rejects all commands except rsync.

mkdir ~/rsyncvi ~/rsync/checkrsync

case “$SSH_ORIGINAL_COMMAND” in*\&*)echo “Rejected”;;*\(*)echo “Rejected”;;*\{*)echo “Rejected”;;*\;*)echo “Rejected”;;*\<*)echo “Rejected”;;*\`*)echo “Rejected”;;rsync\ –server*)$SSH_ORIGINAL_COMMAND;;*)echo “Rejected”;;esac

======================================chmod 700 ~/rsync/checkrsync

Test rsync On webhost02

do this as root!rsync -avz –delete  -e “ssh -i /root/rsync/mirror-rsync-key” rsyncuser@webhost01:/usr/local/etc/ /usr/local/etc/

(The –delete option means that files that have been deleted on webhost01 should also be deleted on webhost02 The –exclude option means that these files/directories should not be mirrored; e.g.

Page 11: Huong Dan Cau Hinh Clustering

–exclude=**/error means “do not mirror /var/www/error”. You can use multiple –exclude options. I have listed these options as examples; you can adjust the command to your needs.

Create A Cron Job

*/10 * * * * /usr/bin/rsync -avz –delete  -e “ssh -i /root/rsync/mirror-rsync-key” rsyncuser@webhost01:/usr/local/etc/ /usr/local/etc/ > /var/tmp/rsyncuser.log 2>&1

Filed under:Tech Docs by admin

Comments are closed.

<< C ấ u hình Web Cluster | C ấ u hình DHCP trên Router Juniper >>

Đ ồ ng b ộ d ữ li ệ u gi ữ a các Node trong mô hình Web Cluster Đăng vào ngày 21 Tháng Ba ̉y 2011 lúc 22:14 bởi Duy Khánh Mục: IT, Network Solution, Unix/Linux || Tags: lighttpd, Linux, web design, replication Bình lu ậ n (1)

I – Giới thiệu:

Ở bài trước, mình có trình bày Cấu hình Web Cluster giúp tăng tính sẵn sàng cho hệ thống Web Server của chúng ta. Tuy nhiên, nếu một Node trong hệ thống Cluster “chết”, sau một thời gian sau Node đó “sống” trở lại thì dữ liệu trên Node không còn nhất quán với các Node còn lại nữa!. Trong bài này, mình sẽ tiếp tục trình bày về đồng bộ dữ liệu giữa các Node trong mô hình Web Cluster.

Page 12: Huong Dan Cau Hinh Clustering

Yêu cầu:

2 Server làm Web Server với dịch vụ httpd (Ở đây mình dùng CentOS 5.6) Đã thực hiện c ấ u hình Web Cluster .

Chú ý: Bài này mình trình bày về đồng bộ dữ liệu chứa trên các thư mục của Web Server (ví dụ: hình ảnh, tệp tin âm thanh,…). Để có được mô hình đẩy đủ hơn, chúng ta có thể tiến hành thiết lập đồng bộ Cơ Sở Dữ Liêu của các Web Server này, bạn có thể tham khảo bài viết Đồng bộ dữ liệu trên 2 Database Server sử dụng MySQL Server, sử dụng kĩ thuật MySQL Replication.

II – Cài đặt gói rsync và test đồng bộ:

Trên cả 2 Node, tiến hành cài đặt các gói rsync

#yum install rsync

Tạo một user dùng để 2 Node xác thực trong quá trình đăng nhập đồng bộ dữ liệu. Ở Node1 mình tạo một user với tên là backup

#root@node1# useradd backup #root@node1# passwd backup

Page 13: Huong Dan Cau Hinh Clustering

Trên Node2 mình sẽ thử đồng bộ bằng tay để xem các gói rsync đã cài đặt đúng hay chưa.

root@node2# rsync –avz –e ssh [email protected]:/var/www/html/ /var/www/html/

Trong đó:

backup là user đã tạo ở Node1. 172.16.1.1 là IP của Node1. /var/www/html/ là 2 thư mục cần đồng bộ.

Đồng bộ dữ liệu từ Node1 qua Node2 thành công!. Ta kiểm tra 2 thư mục /var/www/html/ ở Node1 và Node2 thì thấy chúng có nội dung giống nhau.

III – Tạo SSH-Key và lập lịch tự động đồng bộ:

Trên Node2, tiến hành tạo Key

root@node2# mkdir /root/rsync root@node2# ssh-keygen –t dsa –b 1024 –f /root/rsync/mirror-rsync-key

Ở đây bạn sẽ được yêu cầu nhập passphrase 2 lần. Hãy nhập passphrase trắng cả 2 lần!

Page 14: Huong Dan Cau Hinh Clustering

Sau khi tạo Key xong, tiến hành chuyển Key này tới cho Node1. Chúng ta sẽ đưa key vào mục home của user backup được tạo ở bước trên

root@node2# scp /root/rsync/mirror-rsync-key.pub [email protected]:/home/backup

Trở lại Node1, ta tiến hành chuyển qua thao tác trên user backup:

backup@node1# mkdir ~/.ssh backup@node1# chmod 700 ~/.ssh backup@node1# mv ~/mirror-rsync-key.pub ~/,ssh/ backup@node1# cd ~/.ssh backup@node1 .ssh# touch authorized_keys backup@node1 .ssh# chmod 600 authorized_keys backup@node1 .ssh# cat mirror-rsync-key.pub >> authorized_keys

Tiến hành mở file authorized_keys lên backup@node1 .ssh# vi authorized_keys

Thêm vào trước nội dung có sẵn của file authorized_keys nội dung sau:

Page 15: Huong Dan Cau Hinh Clustering

command="/home/backup/rsync/checkrsync",from="172.16.1.2",no-port-forwarding,no-X11-forwarding,no-pty ssh-dss

Chú ý: Có khoảng trắng giữa đoạn mới thêm với đoạn nội dung có sẵn của file authorized_keys

nhé .

Để đảm bảo hệ thống được an toàn khi thực các lệnh bởi user backup. Chúng ta nên cấm tất các các câu lệnh ngoại trừ rsync trên user này.

backup@node1# ~/rsync backup@node1# vi ~/rsync/checkrsync

Tạo thêm nội dung dưới đây cho file checkrsync

case "$SSH_ORIGINAL_COMMAND" in     *\&*)         echo "Rejected"         ;;     *\(*)         echo "Rejected"         ;;     *\{*)         echo "Rejected"         ;;     *\;*)         echo "Rejected"         ;;     *\<*)         echo "Rejected"         ;;     *\'*)         echo "Rejected"         ;;

Page 16: Huong Dan Cau Hinh Clustering

    rsync\ --server*)         $SSH_ORIGINAL_COMMAND         ;;     *)         echo "Rejected"         ;; esac    

s

Sau đó lưu file checkrsync lại và chmod nó ở 700

#chmod 700 ~/rsync/checkrsync

Bây giờ, trên Node2 ta sẽ kiểm tra lại sự đồng bộ dữ liệu đăng nhập bởi user backup có sử dụng SSH-Key

root@node2# rsync -avz --delete --exclude=**/stats --exclude=**/error -e "ssh -i /root/rsync/mirror-rsync-key" [email protected]:/var/www/html/ /var/www/html/

Trong đó:

Page 17: Huong Dan Cau Hinh Clustering

delete: Khi file được xóa trên Node1 thì nó cũng sẽ được xóa trên Node2 exclude: Loại ra các thư mục không muốn đồng bộ. Ví dụ ở trên mình loại ra các thư mục

như /stats và /error

Nếu có kết quả đồng bộ đại loại như hình trên thì bạn đã cấu hình thành công!. Bây giờ ta sẽ tiến hành lập lịch để Node2 tự động đồng bộ dữ liệu giống như Node1.

root@node2# crontab –e

Thêm vào nội dung như bên dưới rồi tiến hành lưu lại:

*/1 * * * * /usr/bin/rsync -azq --delete --exclude=**/stats --exclude=**/error -e "ssh -i /root/rsync/mirror-rsync-key" [email protected]:/var/www/html/ /var/www/html/

Ở đoạn lệnh trên mình đã lập lịch Node2 cứ một phút thì nó lại tiến hành đồng bộ dữ liệu với Node1. Chúng ta tiến hành kiểm tra đơn giản như sau.

Trên Node1, tạo một file kiemtra.txt: root@node1# touch /var/www/html/kiemtra.txt

Page 18: Huong Dan Cau Hinh Clustering

Trên Node2, ta tiến hành kiểm tra lại thư mục /var/www/html/ thì thấy file kiemtra.txt đã xuất

hiện

IV – Đồng bộ dữ liệu qua lại giữa 2 Node:

Ở các bước cấu hình trên, mình đã thiết lập được quá trình đồng bộ dữ liệu từ Node1 tới Node2. Kiểu thiết lập này nhằm mục đích backup lại dữ liệu ở Node chính. Tuy nhiên, nếu Node1 bị chết, Node2 thay thế để đáp ứng nhu cầu Web cho Client và khi Node1 được phục hồi thì dữ liệu của chúng ta sẽ không còn nhất quán nữa! (dữ liệu không thể đồng bộ từ Node2 tới Node1). Để khắc phục điều này, chúng ta sẽ tiến hành tương tự như trên, nhưng ngược lại đối với 2 Node! Node1 sẽ đồng bộ dữ liệu trên Node2!

Mô hình Backup: Node1 -----> Node2

Mô hình đồng bộ trên 2 Node: Node1 <---------> Node2

Chúc các bạn thành công!