Nibelungen Code

Tag Archives: SSH

lsyncd+sshでのミラーサーバー構築

このエントリをはてなブックマークに追加このエントリをGoogle bookmarkに追加このエントリをdel.icio.usに追加このエントリをfacebookに追加

以前、lsyncd+rsyncdでのミラーサーバー構築にて
lsyncd+rsyncdでのミラーサーバー構築を書いたけど、やっとSSHを使用したやりかたを確認したので以下に。

秘密鍵と公開鍵を作成

ミラー元で

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hogehoge/.ssh/id_rsa):
Created directory '/home/hogehoge/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hogehoge/.ssh/id_rsa.
Your public key has been saved in /home/hogehoge/.ssh/id_rsa.pub.
The key fingerprint is:
75:28:f4:77:3f:68:93:d2:c5:7d:2a:5c:96:c9:2f:17 hogehoge@TestServer

できた公開鍵をミラー先へ

scp .ssh/id_rsa.pub hogehoge@ミラー先:/home/hogehoge/

ミラー先で

mkdir .ssh
cat id_rsa.pub >> .ssh/authorized_keys
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

ミラー元からパスワードなしでログインできることを確認

ssh hogehoge@ミラー先

ミラー元、ミラー先のid_rsa.pubを削除する

rm id_rsa.pub

ミラー元設定

lsyncdをインストールする。
Ubuntu10.10(maverick)以降でないとUbuntu公式リポジトリにないので注意。

sudo aptitude install lsyncd

設定ファイルのサンプルをコピーして編集する。
source pathにはミラーしたいパス、target pathにはミラー先のユーザ名@IP:ミラー先パスを記述しておく。(scpでファイル送るときの宛先指定と同じですね)
パスの最後に/がついていればその配下、ついていなければディレクトリそのものになります。

sudo cp -p /usr/share/doc/lsyncd/examples/lsyncd.conf.xml /etc/
sudo nano /etc/lsyncd.conf.xml
<lsyncd version="1">

<!--
This is a default config file template for lsyncd.
Actually all settings are optional, if you delete them
lsyncd will use the option value (which is likely identical
to the values specified as defaults in this template)

                                  - Happy Syncing! -

-->

<settings>
        <!--uncomment to log all debug messages.-->
        <!--debug/-->

        <!--uncomment to log only errors.-->
        <!--scarce/-->

        <!--uncomment to not detach, log to stdout/stderr.-->
        <!--no-daemon/-->

        <!--uncomment to not call any actions, run dry only.-->
        <!--dryrun/-->

        <!--uncomment to exclude file handled to rsync.-->
        <!--exclude-from filename="/tmp/exclude"/-->

        <!-- the file to log messages -->
        <logfile      filename="/var/log/lsyncd/lsyncd.log"/>

        <!--Specify the rsync (or other) binary to call-->
        <binary       filename="/usr/bin/rsync"/>

        <!--uncomment to create a file containing pid of the daemon-->
        <pidfile      filename="/var/run/lsyncd.pid"/>

        <!--this specifies the arguments handled to the rsync (or other)
                binary.

                option is the default literal.
                only '%r' will be replaced with r when recursive
                operation is wanted, d when not.

                exclude-file will be replaced with -exclude-from FILE

                source will be the source path to sync from

                destination will be the destination path to sync to -->
        <callopts>
                <option text="-lt%r"/>
                <option text="-p"/>
                <option text="-avz"/>
                <option text="--delete"/>
                <option text="--rsh=ssh"/>
                <exclude-file/>
                <source/>
                <destination/>
        </callopts>
</settings>


<directory>
        <source path="/var/www/"/>
        <target path="hogehoge@ミラー先IP:/var/www/"/>
        <!--
        or it can also be an absolute path for localhost
        <target path="/absolute/path/to/target">
        -->
</directory>

<!--
     You can specify even more than one source/destination pair,
     all will be synced.

         Please take care you do not make any with other source/target pairs.
-->
<!--directory>
        <source path="/home/axel/lsyncd-svn/3"/>
        <target path="/home/axel/lsyncd-svn/4"/>
</directory-->

</lsyncd>

lsyncdの自動起動スクリプトを作成する。

cd /etc/init.d/
sudo nano lsyncd
#!/bin/sh
### BEGIN INIT INFO
# Provides:          lsyncd
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Should-Start:      $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: fast remote file copy program daemon
# Description:       start and stop lsyncd
### END INIT INFO

# Source function library.
. /lib/lsb/init-functions

PATH=$PATH:/usr/bin

# Source networking configuration.
[ -r /etc/lsyncd ] && . /etc/lsyncd

option="$SHORT_LOG $IGNORE_START_ERRORS $DEBUG"

RETVAL=0

prog="lsyncd"

start(){
        echo -n $"Starting $prog: "
        $prog $option
        RETVAL=$?
        echo
        touch /var/lock/lsyncd
        return $RETVAL
}

stop(){
        echo -n $"Stopping $prog: "
        killproc $prog
        RETVAL=$?
        echo
        rm -f /var/lock/lsyncd
        return $RETVAL
}

reload(){
        echo -n $"Reloading configuration: "
        killproc $prog -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

condrestart(){
        [ -e /var/lock/lsyncd ] && restart
        return 0
}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        status)
                status $prog
                ;;
        restart)
                restart
                ;;
        reload)
                reload
                ;;
        condrestart)
                condrestart
                ;;
        *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        RETVAL=1
esac

exit $RETVAL
sudo chmod +x lsyncd

lsyncdの起動オプションファイルを作成

sudo nano /etc/lsyncd
IGNORE_START_ERRORS="--stubborn"
#SHORT_LOG="--scarce"
#DEBUG="--debug"

lsyncdの自動起動の設定

sudo update-rc.d lsyncd defaults 56 50

たぶん以下でも可だと思う。

sudo update-rc.d lsyncd start 56 2 3 4 5 . stop 50 0 1 6  .

lsyncdのログローテート設定

sudo nano /etc/logrotate.d/lsyncd
/var/log/lsyncd/lsyncd.log {
        daily
        missingok
        rotate 30
        compress
        delaycompress
        dateext
        notifempty
}

lsyncdのログの保存先のディレクトリを作成してローテートのテスト。

sudo mkdir -p /var/log/lsyncd
sudo logrotate -d /etc/logrotate.conf

lsyncdをスタートさせる。

sudo /etc/init.d/lsyncd start

これでミラーされるか試してみる。

sudo touch /var/www/test

ミラー先にtestができてれば成功。
できていなければログを確認して解決してください。
ちなみにポートは22なのでiptables等で制限してる場合は許可するようにしてください。

 | 
このエントリーをはてなブックマークに追加

秘密鍵・公開鍵を使用したSSH接続

このエントリをはてなブックマークに追加このエントリをGoogle bookmarkに追加このエントリをdel.icio.usに追加このエントリをfacebookに追加

参考URL:http://www.server-memo.net/server-setting/ssh/ssh-key.html
詳しくは参考URLを参照してもらうとして、以下メモ。

秘密鍵と公開鍵を作成

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hogehoge/.ssh/id_rsa):
Created directory '/home/hogehoge/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hogehoge/.ssh/id_rsa.
Your public key has been saved in /home/hogehoge/.ssh/id_rsa.pub.
The key fingerprint is:
75:28:f4:77:3f:68:93:d2:c5:7d:2a:5c:96:c9:2f:17 hogehoge@TestServer

authorized_keysが無い場合

cd ~/.ssh
mv id_rsa.pub authorized_keys
chmod 600 authorized_keys

既にauthorized_keysがある場合

cat id_dsa.pub >> authorized_keys

WinSCP等でid_rsaをローカルに持ってきてPuTTYgenに喰わせる。

(略)

sshdの設定変更

sudo cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config_yyymmdd
sudo nano /etc/ssh/sshd_config
--- sshd_config_20110218        2011-02-17 23:07:29.804689290 +0900
+++ sshd_config 2011-02-18 00:34:08.837514406 +0900
@@ -23,12 +23,12 @@

 # Authentication:
 LoginGraceTime 120
-PermitRootLogin yes
+PermitRootLogin no
 StrictModes yes

 RSAAuthentication yes
 PubkeyAuthentication yes
-#AuthorizedKeysFile    %h/.ssh/authorized_keys
+AuthorizedKeysFile     %h/.ssh/authorized_keys

 # Don't read the user's ~/.rhosts and ~/.shosts files
 IgnoreRhosts yes
@@ -47,7 +47,7 @@
 ChallengeResponseAuthentication no

 # Change to no to disable tunnelled clear text passwords
-#PasswordAuthentication yes
+PasswordAuthentication no

 # Kerberos options
 #KerberosAuthentication no
sudo /etc/init.d/sshd restart
 | 
このエントリーをはてなブックマークに追加

iphone6ケース ルイヴィトン iphone6ケース シャネル 手帳型 シャネル iphone6ケース タバコ ブランドスマホケース