顯示具有 SSH 標籤的文章。 顯示所有文章
顯示具有 SSH 標籤的文章。 顯示所有文章

2008年12月29日 星期一

set ssh connect without password prompt



in mirror computer ( 192.168.1.139)
create ssh key & place it to server


cd /home/atmail/.ssh/
su atmail
ssh-keygen -f atmailrsync
scp atmailrsync.pub 192.168.1.7:/home/atmail/.ssh/authorized_keys


connect the server with the ssh key

su atmail
ssh -i /home/atmail/.ssh/atmailrsync atmail@192.168.1.7

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

vi /root/.ssh/known_hosts
and del all data inside

2008年11月13日 星期四

redirect from 摩托學園討論區
Rsync over SSH with No Password (Crontabs)

這裡小弟將簡單紀錄,最基礎的異地同步備份方法:

—如何使用 rsync + ssh + crontab 製作 mirror Files。

因為示範的關係,所以採用一般 User 來進行,
實際狀況下,如果採用 root 進行 ssh 遠端同步時,

必須修改:/etc/ssh/sshd_config。

並設定:
PermitRootLogin no
PermitRootLogin forced-commands-only

另外也必須注意,備份檔案的相關權限問題。



########################################
rsync + ssh 基本同步化
########################################

$ sudo apt-get install rsync ssh

$ man rsync
-a, --archive (archive mode)
-v, --verbose (increase verbosity)
-z, --compress (compress file data)
-e, --rsh=COMMAND (specify rsh replacement)
--rsync-path=PATH (specify path to rsync on the remote machine)

透過 ssh 將 yenjinc.info 的 ~/backup,備份至本地端 /backup。

$ rsync -avz -e ssh yenjinc@yenjinc.info:~/backup/ /backup/
yenjinc@yenjinc.info's password: ←(輸入密碼)

如果需要 mirror 遠端與本地端的資料,請加上 --delete 選項。

$ rsync -avz -e ssh --delete yenjinc@yenjinc.info:~/backup/ /backup/
yenjinc@yenjinc.info's password: ←(輸入密碼)

以上僅適合一般同步作業,因為 ssh 必須提示輸入密碼才能進行同步化,
如果放在 Crontab 的話,會因為沒有辦法 key in password 而整個 hang 在那。



########################################
產生 ssh 公開金鑰與私密金鑰
########################################

rsync 會利用 ssh 登入到遠端主機,需要帳號與密碼。
若要在 crontab 中不需要輸入密碼就登入,必須製作 public / private keys。

$ ssh-keygen -t dsa -b 1024 -f yenjinc.info-key ←(file name 可自訂)
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase): ←(不需輸入,直接按 Enter)
Enter same passphrase again: ←(不需輸入,直接按 Enter)
Your identification has been saved in yenjinc.info-key.
Your public key has been saved in yenjinc.info-key.pub.
The key fingerprint is:
41:29:60:49:40:c3:a0:8f:2f:74:4e:40:64:a5:42:db Denny@PowerBook-G4.local
(公開金鑰與私密金鑰製作完成)

這時候目錄下會產生兩個檔案:
yenjinc.info-key
yenjinc.info-key.pub ←(必須放置到遠端的主機裡)

必須將 yenjinc.info-key.pub 放到遠端 ~/.ssh/authorized_keys 裡。

$ scp yenjinc.info-key.pub yenjinc@yenjinc.info:~/.ssh/
yenjinc@yenjinc.info's password: ←(輸入密碼)
yenjinc.info-key.pub 100% 614 0.6KB/s 00:00

登入到遠端的主機,把 pub key 加入 authorized_keys 中。

$ ssh yenjinc@yenjinc.info
yenjinc@yenjinc.info's password: ←(輸入密碼)

yenjinc@~$ cd .ssh/
yenjinc@~/.ssh$ cat yenjinc.info-key.pub >> authorized_keys
yenjinc@~/.ssh$ chmod 600 authorized_keys

完成之後,離開遠端主機,並且測試一下,是否已經可以不需密碼即 ssh login。

$ ssh -i yenjinc.info-key yenjinc@yenjinc.info ←(使用 -i 指定剛才的私密金鑰)
yenjinc@~$ ←(順利完成!不需密碼即可登入。)



########################################
rsync + ssh + No Password 基本同步化。
########################################

接著下來,即可利用 Rsync 透過 SSH 來同步本地與遠端的資料,而不需輸入密碼。
不用密碼的好處是,可以把 rsync 同步備份指令,寫在 Crontab 下自動執行。

透過 ssh 將 yenjinc.info 的 ~/backup,備份至本地端 /backup。

$ rsync -avz -e "ssh -i yenjinc.info-key" yenjinc@yenjinc.info:~/backup/ /backup/
←(順利完成!不需密碼,即可將遠端資料備份過來。)

如果需要 mirror 遠端與本地端的資料,請加上 --delete 選項。

$ rsync -avz -e "ssh -i yenjinc.info-key" --delete yenjinc@yenjinc.info:~/backup/ /backup/
←(順利完成!不需密碼,即可將將遠端的資料製作一份 mirror 到本地端。)



########################################
rsync + ssh + No Password + Crontab 基本同步化。
########################################

基本上,如果上面那一個步驟已經順利成功的話,在加上 Crontab 就不會出錯了。
只要將剛才那串指令,加到 crontab 指令中就行啦!

以下的 Script 是在我的 OSX + Fink 的環境,若是 Linux 則需要另做修改。

#!/bin/bash
RSYNC=/sw/bin/rsync
SSH=/usr/bin/ssh
KEY=/Users/Denny/yenjinc.info-key
USER=yenjinc
HOST=yenjinc.info
REMOTE_DIR=/home/yenjinc/backup/
LOCAL_DIR=/Users/Denny/backup/

# rsync+ssh+crontab command
$RSYNC -avz -e "$SSH -i $KEY" --delete $USER@$HOST:$REMOTE_DIR $LOCAL_DIR

將以上 script 內容,儲存為 /Users/Denny/crontab-file。
完成後,即可設定 Crontab。

$ crontab -e
*/2 * * * * /Users/Denny/crontab-file

大功告成,每兩分鍾就執行異地同步備份一次。



########################################
後記 (備註):
########################################

以上所有內容,僅是基本的 rsync+ssh+crontab 示範與練習。
如果是 Server 的環境,詳細的內容就更為繁複啦!

若想延伸閱讀,可參考以下。

—製作不用密碼可立即登入的 ssh 用戶

—以 rsync 進行同步鏡相備份

—使用 rsync (全自動網路備份法)

—[分享] rsync遠端備份

—Using Rsync and SSH Keys, Validating, and Automation

在 Google 敲入:rsync ssh crontab 等相關 keyword 可以找到更多哦!

2008年11月12日 星期三

ssh with password

There are two ways to do what you want. One involves a stored password, and one does not. Both are non-interactive, meaning that they can work when you're not there to enter a password.

First Method
The way that does not require a password. You can use public/private key authentication instead of passwords with SSH. I'm going to assume that you're using OpenSSH, which comes with practically every Linux distribution that there is.
  1. Configure your SSH server to accept private key logins. In /etc/ssh/sshd_config make sure that there's a line that says PubkeyAuthentication yes (and that there is no # infront of it). If you change this file, you need to restart the sshd service. If you're not sure, stop and ask somebody here before you break it.
  2. On your local machine (not the server), create yourself a pair of keys with ssh-keygen -t rsa (you can use other options than rsa, but I'm keeping it simple). Do not specify a password. Save the keys in the locations prompted.
  3. Open the contents of the id_rsa.pub file that you just created (it's one very long line of text), and copy the contents into the end of the file $HOME/.ssh/authorized_keys on the server machine. Create the file if it doesn't exist.
Now you should be able to ssh user@remote.host without a password. If that works, you can use it in scripts, etc. Because you have a private key with no password to protect it, it's important that you make sure that nobody gets their grubby hands on your id_rsa file. They can have the id_rsa.pub file (it's public, you see) but the other one's your precious.

If you think somebody has a copy of your id_rsa file, you can delete the line that you added to authorized_keys on the server, to disable that key.

FURTHER READING (Daniel Robbins at ibm.com)

Second method
If you thought that was complicated, you wait till you've tried to get this one working. The basic idea is to use expect, which is an administration automation tool, to type your password in to ssh when prompted. It might not always work, and when it doesn't, it's hard to figure out why not. I recommend the first method.

Anyway, here's a command that you can poke at until it does what you want:
Code:
expect -c 'spawn ssh user@remote.host ; expect assword ; send "passphrase\n" ; interact'
Expect might not be installed on your system. That's the first hurdle, although most distributions have it easily available. You need to modify user@remote.host to your remote username and hostname. You need ot make sure that ssh prompts for a password using the letters "assword"; if not, that needs changing to something that does appear. You need to change "passphrase" to whatever the password is.

Problem here is doing the scripting. You can either have expect type in further commands, or you can list them as a parameter to ssh in that spawn command (just before the semicolon ; ). It might never work properly for you; again, I recommend the first method.

SSH login without password

Your aim

You want to use Linux and OpenSSH to automize your tasks. Therefore you need an automatic login from host A / user b to Host B / user b. You don't want to enter any passwords, because you want to call ssh from a within a shell script.

How to do it

First log in on A as user a and generate a pair of authentication keys. Do not enter a passphrase:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A

Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh
b@B's password:

Finally append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password:

From now on you can log into B as b from A as a without password:

a@A:~> ssh b@B hostname
B

A note from one of our readers: Depending on your version of SSH you might also have to do the following changes:

  • Put the public key in .ssh/authorized_keys2
  • Change the permissions of .ssh to 700
  • Change the permissions of .ssh/authorized_keys2 to 640