<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jojo's blog--快樂憂傷都與你同在
    為夢想而來,為自由而生。 性情若水,風起水興,風息水止,故時而激蕩,時又清平……
    posts - 11,  comments - 30,  trackbacks - 0

    How to Automate Secure File Synchronization using SSH and rsync

    Tom Hilinski
    Natural Resource Ecology Laboratory,
    Colorado State University
    Last updated: Dec 2008

    Introduction

    In order to automate file transfers between computers without a password, a private-public key identification key simplifies the process. This is useful, for instance, when using rsync to synchronize files in local and remote directories. For instance, after editing files on your local Linux desktop or Microsoft Windows laptop, you want to automatically update the files on the office computer with your modified files. In this case, a utility such as rsync can be used to do the update without prompting you for a password on the office computer.

    The process of creating the key is described here in the context of using rsync on a local Windows computer with Cygwin installed. Use the Cygwin setup program to install SSH, rsync, and Bash. Here, I assume that the remote computer, say, your office server, is running Linux, and you have an account on it with the user name yourUserName.

    In the examples below, command lines begin with a $ character, comment lines begin with a # character, while text beginning without either is written to the console display. Example text that is italicized means you substitute your own information there; for example, yourUserName is replace by your actual user name.

    Create a Key

    On your local Windows computer, open a Bash shell console window. If you don't have a directory named .ssh create one by using SSH to connect to your office computer for the first time. You will be prompted to accept the key.

    $ ls -d .ssh
    # if the directory does not exist, run ssh
    $ ssh yourUserName@calypso.nrel.colostate.edu

    Go into the .ssh directory and create a key. This key will have two files, a private file and a public file. When prompted, do not enter a password or passphrase.

    $ ssh-keygen -t dsa -b 1024 -f yourUserName-rsync-key
    Generating public/private dsa key pair.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in yourUserName-rsync-key.
    Your public key has been saved in yourUserName-rsync-key.pub.
    The key fingerprint is:
    (a long string of hexadecimal digits)

    Check the permissions on your key files (e.g., ls -l). The permissions should be 600 (or rw-----).

    On your office computer, make sure you have in your home directory, a subdirectory named .ssh (note the leading dot).

    $ ssh yourUserName@calypso.nrel.colostate.edu
    $ ls -d .ssh
    # If this directory doesn't exist, create it:
    $ mkdir .ssh
    # Make sure the permissions are secure:
    $ chmod 700 .ssh

    Now, log off your office computer.

    Next, copy the public key file to your office computer, log onto that computer, then append the key file to the SSH file containing keys it knows about.

    # Copy the public key to your office computer:
    $ scp yourUserName-rsync-key.pub yourUserName@calypso.nrel.colostate.edu:/home/nrel/yourUserName/.ssh/
    # Log on to the remote computer:
    $ ssh yourUserName@calypso.nrel.colostate.edu
    # If your are not in a bash shell, then start one:
    $ bash
    # If the key file does not exist, create it:
    $ if [ ! -f authorized_keys ]; then touch authorized_keys; chmod 600 authorized_keys; fi
    # Append your new public key to the key file:
    $ cat yourUserName-rsync-key.pub >> authorized_keys
    $ rm yourUserName-rsync-key.pub

    Your key is now ready to use with rsync. Optionally, you can restrict the use of the key to an IP address and a particular process (e.g., rsync). To restrict the key to rsync, create the file listed in Appendix A in your ~/.ssh directory on your office computer. You can use a text editor to paste that text in. Then set the permissions so no one else can read it. For example:

    # Use vi to create the file; paste in the script from Appendix A.
    $ vi restrict-to-rsync
    $ chmod 700 restrict-to-rsync

    Next, edit the file authorized_keys so that the line with your key (the last line, since the key was just appended to the file) begins with a command to run that script. The command points to the full path of the script file. The line originally began with:

    ssh-dss AAAAB3...

    After inserting the script command, the line starts with:

    command="/home/nrel/yourUserName/.ssh/restrict-to-rsync" ssh-dss AAAAB3...

    Using rsync With SSH and Your Key

    Test the use of your new key by copying a junk file from your local computer to your office computer. Here, the local file is junk.txt and the remote directory in your office computer is tmp, and the direction of transfer is local-to-remote. Give SSH the name of your private key file on your local computer, including its path, using the following form:

    rsync -auvz -e "ssh -i private-key-file" source destination

    Here, source is a file or a directory, and destination has the form yourUserName@remote-computer:/remote-path

    A real example, using the file names from the previous examples, is:

    rsync -auvz -e "ssh -i /home/yourUserName/.ssh/yourUserName-rsync-key" junk.txt yourUserName@calypso.nrel.colostate.edu

    The rsync flags -auvz specify "archive", "update", "verbose messages", and "compress files for transfer", respectively. "Update" means that files on the destination that are newer than your local files are not overwritten. The "-e" flag tells rsync the SSH command.

    If you want details on what SSH is doing, add "-v" to the ssh options. To run rsync quietly, remove the "-v" option from both rsync and SSH option list.

    To reverse the synchronization so the remote file is updated on your local computer, reverse the source and destinations.

    You can store your rsync commands that you use all the time in a script file. Keep this script with your project files or in a script directory that is specified in your PATH environment variable.

    Additional Information

    rsync document: http://rsync.samba.org/ftp/rsync/rsync.html

    rsync web site: http://rsync.samba.org/

    Acknowledgements

    Many online sources provided the information I used to create this process. A particularly succinct source was http://troy.jdmz.net/rsync/index.html provided the basis of the script in Appendix A. Thanks to all.

    Appendix A: File restrict-to-rsync

    The following shell script checks that rsync is the process attempting to connect. If it is not, the script fails, and SSH also fails. A log file named validate-rsync.log is created or appended to with each connection.

    #!/bin/sh
    logfile=/home/nrel/yourUserName/.ssh/restrict-to-rsync.log
    case "$SSH_ORIGINAL_COMMAND" in
    *\&*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\(*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\{*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\;*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\<*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\`*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    *\|*)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    rsync\ --server*)
    {
    echo `date` "- SSH connection accepted" >> $logfile
    $SSH_ORIGINAL_COMMAND
    }
    ;;
    *)
    echo `date` "- SSH connection rejected" >> $logfile
    ;;
    esac

    posted on 2009-01-20 17:18 Blog of JoJo 閱讀(266) 評論(0)  編輯  收藏 所屬分類: Linux 技術相關

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(6)

    隨筆檔案

    文章分類

    文章檔案

    新聞分類

    新聞檔案

    相冊

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国精产品一区一区三区免费视频| 亚洲福利电影在线观看| 亚洲毛片不卡av在线播放一区 | 亚洲国产精品日韩av不卡在线 | 亚洲成人在线电影| 亚洲一区二区三区日本久久九 | 在线观看91精品国产不卡免费| 日韩成人免费在线| 免费人成网站7777视频| 亚洲日韩国产一区二区三区| 在线精品亚洲一区二区小说| 亚洲国产精品一区二区成人片国内 | 高清永久免费观看| a毛片在线免费观看| 日本免费大黄在线观看| 四虎在线最新永久免费| 成年女人毛片免费播放视频m| 日本媚薬痉挛在线观看免费| 免费大黄网站在线观看| 久久久久亚洲精品无码网址| 亚洲国产精品久久久久婷婷软件 | 久久精品国产亚洲AV网站| 亚洲网站在线观看| 天堂亚洲国产中文在线| 美女黄频a美女大全免费皮| 激情吃奶吻胸免费视频xxxx| 中文在线观看永久免费| 久久久久久国产精品免费无码| 日韩版码免费福利视频| 暖暖免费高清日本一区二区三区| 亚洲精品无码你懂的网站| 亚洲国产另类久久久精品黑人| 亚洲欧洲高清有无| 亚洲AV永久无码天堂影院| 亚洲第一视频在线观看免费| 无码国产精品一区二区免费式芒果| 免费观看黄网站在线播放| 亚洲国产成人久久综合一区77| 亚洲日韩区在线电影| 亚洲日韩一区精品射精| 在线观看免费黄色网址|