OpenSSH
OpenSSH: Secure Remote Shell
- 具有加密功能,這是用來取代以前傳統明碼傳輸的遠端存取工具
- 允許認證、加密存取遠端系統
- ssh [user@]hostname
- ssh [user@]hostname command
- 要達成SSH的連線需要一個sshd server及一個ssh client
- ssh client
- 在windows底下有這幾套軟體
- putty原始的putty中文可以顯示,但在輸入有點小問題。
- piettyputty的中文修改版,友善多了。
- winscp支援ssh ftp的程式,可以結合putty的帳號密碼
- 以上是windows上的免費工具
[student@server1 ~]$ ssh --help
# 他的簡單參數說明
usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
[-D port] [-e escape_char] [-F configfile]
[-i identity_file] [-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[user@]hostname [command]
# 直接連線格式為:ssh 帳號@主機名稱
[student@server1 ~]$ ssh guest1@192.168.3.249
The authenticity of host '192.168.3.249 (192.168.3.249)' can't be established.
RSA key fingerprint is 90:8c:cd:57:e3:c8:27:50:92:39:e9:05:c8:3c:ae:84.
Are you sure you want to continue connecting (yes/no)? yes
# 第一次會因金鑰產生這問題,請回答yes加入此站台的key
Warning: Permanently added '192.168.3.249' (RSA) to the list of known hosts.
guest1@192.168.3.249's password: password(此範例密碼為password)
# 輸入你的密碼
[guest1@station249 ~]$
# 登入成功,你可以輸入指令了
-
[root@server1 ~]# rpm -qa | grep openssh
openssh-4.3p2-41.el5
openssh-clients-4.3p2-41.el5
openssh-server-4.3p2-41.el5
openssh-askpass-4.3p2-41.el5
[root@server1 ~]# /etc/init.d/sshd restart
# 重新啟動sshd
Shutting down SSH daemon done
Starting SSH daemon done
# 正常這樣的話就可已使用ssh client登入主機了
[root@server1 ~]# telnet 192.168.3.249 22
Trying 192.168.3.249...
Connected to 192.168.3.249 (192.168.3.249).
Escape character is '^]'.
SSH-2.0-OpenSSH_4.3
Connection closed by foreign host.
- 連線到本機使用者 mtchang 並執行 whoami 指令
[root@cccm ~]# ssh mtchang@localhost 'whoami'
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is 15:bd:3a:e8:23:ce:92:88:cc:2e:49:07:5a:e6:7c:99.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
mtchang@localhost's password:
mtchang
scp: 加密的檔案傳輸
- Secure replacement for rcp
- Layered on top of ssh
- scp source destination
- Remote files can be specified using:
- [user@]host:/path/to/file
- Use -r to enable recursion
- Use -p to preserve times and permissions
- Use -C to compress datastream
[root@server1 ~]# scp ks.cfg guest1@192.168.3.249:/home/guest1/ks.cfg
guest1@192.168.3.249's password:
ks.cfg 100% 1582 1.5KB/s 00:00
[root@server1 ~]# ssh guest1@192.168.3.249 ls -la
guest1@192.168.3.249's password:
總計 23
drwx------ 4 guest1 users 1024 6月 11 05:12 .
drwxr-xr-x 39 root root 1024 6月 11 04:49 ..
-rw------- 1 guest1 users 92 6月 11 05:08 .bash_history
-rw-r--r-- 1 guest1 users 33 6月 11 04:48 .bash_logout
-rw-r--r-- 1 guest1 users 176 6月 11 04:48 .bash_profile
-rw-r--r-- 1 guest1 users 124 6月 11 04:48 .bashrc
-rw-r--r-- 1 guest1 users 515 6月 11 04:48 .emacs
-rw-r--r-- 1 guest1 users 1582 6月 11 05:12 ks.cfg
drwxr-xr-x 4 guest1 users 1024 6月 11 04:48 .mozilla
drwx------ 2 guest1 users 1024 6月 11 05:01 .ssh
-rw-r--r-- 1 guest1 users 658 6月 11 04:48 .zshrc
rsync: Efficient File Sync
- 有效率的拷貝到遠端,目前大多數的mirror站台都是使用rsync來達到同步拷貝的功能
- 使用安全的 ssh 連線來作傳輸
- 速度比 scp 指令來的快,因為他只拷貝不同的檔案
- 使用說明
Usage: rsync [OPTION]... SRC [SRC]... DEST
or rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
or rsync [OPTION]... [USER@]HOST:SRC [DEST]
or rsync [OPTION]... [USER@]HOST::SRC [DEST]
or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
to an rsync daemon, and require SRC or DEST to start with a module name.
[root@localhost ~]# rsync /etc/sysconfig/ /root/backups/ -r
skipping non-regular file "selinux"
skipping non-regular file "network-scripts/ifdown"
skipping non-regular file "network-scripts/ifdown-isdn"
skipping non-regular file "network-scripts/ifup"
skipping non-regular file "network-scripts/ifup-isdn"
[student@server1 ~]$ rsync guest1@192.168.3.249:/home/guest1/ \
/home/student/guest1/ -r
guest1@192.168.3.249's password:
- 遠端目錄同步範例(不需要密碼,因為已經使用 key-base 認證)
[student@server1 ~]$ rsync guest1@192.168.3.249:/home/guest1/ \
/home/student/guest1/ -r
[student@server1 ~]$ ls /home/student/guest1/ -l
總計 8
-rw-r--r-- 1 student student 1582 6月 10 21:37 ks.cfg
OpenSSH 以加密金鑰為認證方式
- 不用密碼就可以認證登入ssh的主機
- Uses two keys generated by ssh-keygen:
- private key stays on your system
- Usually passphrase-protected (recommended)
- public key is copied to destination with ssh-copy-id
- ssh-copy-id [user@]host
- 再比較舊的版本上面因為沒有ssh-copy-id這支程式,需要手動把公開金鑰(public key)放到遠端主機的 ~/.ssh/authorized_keys 檔案裡面。
- 實作範例:
- 目的: 以CentOS server1主機上面的 student 帳號,登入到遠端主機 192.168.3.249 的 guestX(x是編號)
- 以下產生本地端的 student 使用者的 id_dsa 私人金鑰及 id_dsa.pub 公開金鑰,請確認其權限為 600 on your provate key 及 644 on your public key。
[[student@server1 ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/student/.ssh/id_dsa):
Created directory '/home/student/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/student/.ssh/id_dsa.
Your public key has been saved in /home/student/.ssh/id_dsa.pub.
The key fingerprint is:
84:24:30:70:da:43:d3:b7:a1:2c:db:5d:e0:c3:42:d0 student@server1
- 請連到遠端主機使用 ssh-keygen 產生遠端主機的私人金鑰及公開金鑰
- 從本地端的 student 帳號複製 ~/.ssh/id_dsa.pub(公開金鑰)到遠端主機 192.168.3.249:/home/guest1/.ssh/authorized_keys 內
[student@server1 ~]$ scp ~/.ssh/id_dsa.pub guest1@192.168.3.249:/home/guest1/.ssh/authorized_keys
guest1@192.168.3.249's password:
id_dsa.pub 100% 605 0.6KB/s 00:00
- 請注意,你的 /home/guest1/.ssh/ 目錄必須要存在才可以。
- 在遠端的 192.168.3.249 的 guest1 帳號,確認authorized_keys 檔案已經被建立。
[guest1@station249 ~]$ ls /home/guest1/.ssh/ -l
總計 7
-rw-r--r-- 1 guest1 users 605 6月 11 05:01 authorized_keys
-rw------- 1 guest1 users 1675 6月 11 05:01 id_rsa
-rw-r--r-- 1 guest1 users 411 6月 11 05:01 id_rsa.pub
- 可以觀察一下,這份公開金鑰的內容,如果有的二個使用者以上要加入,只需要把第二個使用的公開金鑰添加再後面即可。
[guest1@station249 ~]$ cat .ssh/authorized_keys
ssh-dss AAAAB3NzaC1kc3MAAACBAJR1QgoEiGIJ+i901snf3pH15hX5OI2XRrwm
lun6eB
Kz75TMedjaxoITokv+iyuNZ7DG4hGZW+uAwucdrE+dzPd/eJyFJCabhyhMPGdbUj
/d1ZWIPy523JnrMdk9wxya+PG36o4YbzXr/kZSfhRK5i0KSNaRfBoQNB/jYcAw2s
UPAAAAFQDO06XMbkZvYTO/yA+ZixSX0A2xVwAAAIEAkOkpYnx5UZVpPnXAtwETQd
wTM5oZNFOaMRpcFQqfzGS
n0XoN6LfSBEIdeE3CHxfUHDAsij8n2Bm/f8yvgyUwEQo+BPMI6IrKOXGEQaT08So
1zKrvpAJnupsbtb3z8lN+jcI7c96G52tBkVPSkhoJkb/NpIVqQYjfLFcJborvz08
AAACAGDpauJRDPntn0VVcmxpefB7/pR0TSPLikEoY7MHQzmWA6kJP0U+PAf6XehL
tPg6eWdiFhNVqwIgf6O5kdIAczkFDNzlgm4nl3qPl3HiSDCKtcGXudhl4UhUkJr6
JFuj4/u5hglS92CB1XnUzPc5ZBe8u8+fyTyAHOrBcJvhslIM= student@server1
- 再回到原本的主機 student 帳號,直接使用ssh client登入 guest1@192.168.3.249 主機
# 直接使用ssh client登入
[student@server1 ~]$ ssh guest1@192.168.3.249
Last login: Fri Jun 11 05:00:13 2010 from 192.168.3.250
[guest1@station249 ~]$
# 就這樣,不用密碼就登入了。
ssh-agent
- ssh-agent 工具可以使用來儲存您的通行密碼(passphrase),以便不需在每次啟動 ssh 或 scp 連線時輸入通行密碼。
- 假如您是使用 GNOME,openssh-askpass-gnome 工具可以用來當您登入 GNOME 時提示您輸入您的通行密碼,並儲存它直到您登出 GNOME。
- 在該 GNOME 作業階段中所作的任何 ssh 或 scp 連線,都將不需要您輸入您的密碼或通行密碼。
- http://freesf.tnc.edu.tw/docs/rh/rhl-cg-zh_TW-9/s1-openssh-client-config.html
- http://mah.everybody.org/docs/ssh
沒有留言:
張貼留言