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

2014年9月16日 星期二

SSH 加密技術介紹

來源:http://blog.yam.com/mingrass/article/17327933
相關:[教學] CentOS 6.3 設定 – (1) 基本調效與 SSH 連線
還記得在第十一章介紹過的遠端連線指令 telnet 及 ssh 吧 ! 由於 telnet 在資料傳輸過程的不安全,因此都鼓勵大家使用 ssh。那要提供 SSH 服務來給 Client 端做遠端連線時,當然就要架設 SSH Server 了。

 SSH 可以取代傳統的 r 指令 (rsh、rlogin、rcp)、finger 及 telnet 這一類較不安全的連線工具。使用 SSH (Secure SHell),可讓我們所傳輸的資料都事先經過加密,因此 Client 端可以使用 scp 指令在兩台電腦間很安全的複製資料,也可以使用 sftp 指令來安心的存取 ftp 服務。

SSH 協定的版本
 SSH 協定目前有 SSH1 ( SSH protocol version 1 ) 及 SSH2 ( SSH protocol version 1 ) 兩種版本,當然現在的主流是 SSH2,因為它提供了比 SSH1 更安全的資料傳輸。

 在 SSH 上頭,常見到的加密演算法有 RSA、DSA 及 Diffie-Hellman 等。SSH1 主要是使用 RSA 的加密技術,而 SSH2 除了 RSA 以外,還有 DSA 及 Diffie-Hellman 等等。

SSH 加密流程

SSH1 加密技術
˙ Client 向 SSH Server 的 22 port 做出連線請求。
˙ Server 傳送 public key 給 Client:
關於這個地方,您需先了解什麼是 host key pair 及 server key pair。
host key pair 是指存在於 SSH Server 上的一對公鑰 (host public key) 及私鑰 (host private key)。
server key pair 是指在每次 SSH 服務啟動時,所隨機產生的一對公鑰 (server public key) 及私鑰 (server private key),且這個 server key 預設每小時會更換一次。
而 SSH Server 所回應給 Client 的 public key,就是指 host public key 及 server public key。
˙ Client 端比對 host public key:
Client 端對 SSH Server 所傳送過來的 host public key 進行比對,如果與 ~/.ssh/known_hosts 檔案內容相符,則繼續進行下一步,否則會詢問您是否要接受這個 key,此時輸入 yes 就對了。
當 使用者第一次與 Server 做連線時,由於之前並沒有取得這個 key,因此會詢問您是否要接受 Server 所傳來的 host public key,那萬一先前已做過連線,現在還是看到這個訊息,這就表示 Server 上的 host public key 與 Client 的 known_hosts 比對不符合的結果,比如像 Server 的 SSH 套件重新安裝過後就會有這種情形發生。那萬一您遇到的狀況是在 key 比對不符後,直接拒絕您連線,這該怎麼辦呢 ? 很簡單,只要把 Client 的 ~/.ssh/ 目錄下的 known_hosts 檔刪除後,再與 Server 做連線,以重新取得 host public key 就行了。
˙ Client 隨機產生一個 256 bit 的亂數 (session key):
當第三步驟通過後,Client 隨即產生這個亂數,並使用剛剛從 Server 那裡所得到的那兩把 key 來一起做加密,完成後就傳回給 Server。
˙ Server 使用 private key 來解密。
˙ 爾後就透過這 session key 來作對稱式的加密使用。
˙ Client 端開始進行認證,通過後,SSH 連線正式建立起來。
SSH2 加密技術
˙ Client 向 SSH Server 的 22 port 做出連線請求。
˙ Server 傳送 host public key 給 Client:
在 SSH2 裡,sshd daemon 啟動時,並不會產生 server key,所以只會傳送 host public key 給 Client。
˙ Client 端比對 host public key:
與 SSH1 的做法相同,不再贅述。
˙ 使用 Diffie-Hellman 的演算法來產生 session key,以作為連線加密使用。
˙ Client 端開始進行認證,通過後,SSH 連線正式建立起來。

2014年2月14日 星期五

SSH命令行快捷鍵

直接 run 可以跑 debug 模式 (要先停掉 sshd  #service sshd stop)
/usr/sbin/sshd -d


命令列快捷鍵

ctrl + d 刪除游標所在位置上的字符相當於VIM裡x或者dl
ctrl + h 刪除游標所在位置前的字符相當於VIM裡hx或者dh
ctrl + k 刪除游標後面所有字符相當於VIM裡d shift+$
ctrl + u 刪除游標前面所有字符相當於VIM裡d shift+^
ctrl + w 刪除游標前一個單字相當於VIM裡db
ctrl + y  恢復ctrl+u上次執行時刪除的字符
ctrl + ? 撤消前一次輸入
alt + r  撤消前一次動作
alt + d  刪除游標所在位置的後單字

移動
ctrl + a 將游標移動到命令行開頭相當於VIM裡shift+^
ctrl + e 將游標移動到命令行結尾處相當於VIM裡shift+$
ctrl + f 游標向後移動一個字符相當於VIM裡
ctrl + b 游標向前移動一個字符相當於VIM裡h
ctrl +方向鍵左鍵 游標移動到前一個單字開頭
ctrl +方向鍵右鍵 游標移動到後一個單字結尾
ctrl + x 在上次游標所在字符和當前游標所在字符之間跳轉
alt + f  跳到游標所在位置單字尾部

替換
ctrl + t 將游標當前字符與前面一個字符替換
alt + t  交換兩個游標當前所處位置單字和游標前一個單字
alt + u  把游標當前位置單字變為大寫
alt + l  把游標當前位置單字變為小寫
alt + c  把游標當前位置單字頭一個字母變為大寫
^oldstr^newstr  替換前一次命令中字符串 

歷史命令編輯
ctrl + p 返回上一次輸入命令字符
ctrl + r 輸入單字搜索歷史命令
alt + p 輸入字符查找與字符相接近的歷史命令
alt + > 返回上一次執行命令

其它
ctrl + s 鎖住終端機
ctrl + q 解鎖終端機
ctrl + l 清除畫面相當於命令clear
ctrl + c 另一行
ctrl + i 類似TAB健補全功能
ctrl + o 重複執行命令
alt +數字鍵 操作的次數

2013年11月11日 星期一

Setting ssh private key forwarding .

設定 private key 透過 ssh forwarding 到其他 server .

例如 A ->  B  ->  C  那麼 A and B 都需要設定.


Make sure ssh forwarding works   (確定ssh forwarding works )
$ eval `ssh-agent -s`

# ssh-add
#ps aux | grep ssh                 
Ensure ssh-agent is running

#vim ~/.ssh/config 
加入下列兩行:

ServerAliveInterval 90
ForwardAgent yes

2011年12月19日 星期一

[Scp 指令使用] 透過 SSH 傳送檔案

在兩台主機間傳送檔案時,用 ftp 指令很方便,但另一個較為安全的做法是透過 SSH 來傳送,而且使用上十分簡單 ,只要使用 scp 指令則可。
假如你要把 /home/user/ 目錄下所有 .php 副檔名的檔案,傳送到主機 192.168.10.10 的 /home/user/ 下面,登入名稱是 user,可以輸入:


scp /home/user/*.php user@192.168.10.10:/home/user/



PS: 紅色: 來源目錄    藍色: 目的目錄
另外要注意, 就是目的主機是否有開啟 22 port 去對外, 若是沒有會回傳錯誤訊息.


然後系統會詢問你連接密碼,輸入正確密碼後就可以傳送了,但當然用來傳送的帳號需要有合法權限在遠端目錄寫入資料。

若是目的主機不是使用預設22 Port而是用 22222 , 那就必須要用的 -P 參數了.

scp -P 22222 /home/user/*.php user@192.168.10.10:/home/user/ 


如果要傳輸一個遠端的檔案到本機系統,一般的語法如下:

scp user@192.168.10.10:/home/user/*.php /home/user/ 
 
 
使用 -r 將遠端整個目錄都 copy 過來, 這樣 xxx 就會被放到 本地端的 /htdocs/ 下面了.
scp -P 22222 -r root@xxx.xxx.xxx.xxx:/htdocs/xxx/ /htdocs/ 

2011年10月4日 星期二

[Linux] Telnet tftpput ssh - expect操作

首先先安裝EXPECT
sudo apt-get install expect

Login telnet : #!/usr/bin/expect spawn telnet 192.168.1.1 expect Login: send root\r expect Password: send password\r interact tftpput : #!/usr/bin/expect spawn tftp 192.168.1.1 expect tftp> send bin\r expect tftp> send "put $argv\r" expect tftp> send quit\r interact 這樣使用 tftpput FILENAME SSH LOGIN : #!/usr/bin/expect -f # Expect script to supply root/admin password for remote ssh server # and execute command. # This script needs three argument to(s) connect to remote server: # password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # ------------------------------------------------------------------------ # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- # set Variables set password [lindex $argv 0] set ipaddr [lindex $argv 1] set scriptname [lindex $argv 2] set arg1 [lindex $argv 3] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh root@$ipaddr $scriptname $arg1 match_max 100000 # Look for passwod prompt expect "*?assword:*" # Send password aka $password send -- "$password\r" # send blank line (\r) to make sure we get back to gui send -- "\r" #expect eof expect "#"

這樣使用 ./ssh.sh password ipaddr
解釋: expect "#": 結束expect 返回.
interact : 最後加這一行, 把控制權交給控制台,這個時候就可以手動操作了.
proc Login {username server password} { set prompt "(%|>|\#|\\\$) $" spawn /usr/bin/ssh $username@$server expect { -re "Are you sure you want to continue connecting (yes/no)?" { exp_send "yes\r" exp_continue #continue to match statements within this expect {} } -nocase "password: " { exp_send "$password\r" interact } } }
#!/usr/bin/expect set timeout 1 set cmd {uname -a} spawn ssh root@$argv expect_after eof { exit 0 } ## interact with SSH expect { "(yes/no)?" {send -- "yes\r"; exp_continue} "password:" {send -- "123456\r"} } expect "# " send "$cmd\r" expect "$cmd\r" expect "(.*)\r" send "exit\r
參考網站:
不少範例
http://linux.sheup.com/linux/linux5440.htm
http://hi.baidu.com/%B7%D7%E7%A1/blog/item/fcb146fb66b95068024f567b.html
http://www.thegeekstuff.com/2011/01/expect-expressions-loops-conditions/
http://www.thegeekstuff.com/2010/10/expect-examples/
Help Expect

2011年3月2日 星期三

[引用]Linux 透過 SSH SOCKS 連線 來 使用 Firefox / Pidgin(MSN, GTalk..)

文章來源

現在不少醫院都有無線網路, 但是使用後, 體驗被限制只有開 80, 443 Port 的感覺.
但是我在這邊最少要待三天, 陪產(n天) + 生產完(3~7天), 至少要讓 SSH、MSN 能通才行.
  • 註1: 於 Ubuntu Linux, Windows 可以利用 Putty 來達成.
  • 註2: 此篇重點 sudo ssh -D 80 user@example.com -p 443
  • 註3: 在醫院連國外網站(ex: YouTube 被擋掉的話),透過自己的 Server 當 Proxy 會快很多. XD

基礎環境

首先, 要先找好朋友, 幫忙弄台 Server, 讓 ssh port 加開 80 或 443 Port. (沒有這個, 後續就不用玩了. XD)
  1. vim /etc/ssh/sshd_config
    Port 22 # 於這下面加上下述即可.
    Port 443
  2. /etc/init.d/ssh restart # 22, 443 就都可以連進此 Server. (可同時開兩個 Port)

使用 ssh -D 來建立 SOCKS

於 Localhost 開啟 80 Port, 與遠端(example.com)的 443 Port 串起來.
  1. netstat -tln | grep 80 # 先看看 localhost 有沒有佔用 80 Port, 若有佔用, 需要停掉.
  2. sudo ssh -D 80 user@example.com -p 443 # 80 Port 需要 root 權限.
  3. 連線進去後, 只要將此連線留著, 不要關掉即可.

Firefox 透過 SOCKS 連線

  1. 編輯 -> 偏好設定
  2. 進階 -> 網路 -> 設定(連線, 設定 Firefox 要如何連到網路)
  3. 手動設定 Proxy -> SOCKS 主機
    • 127.0.0.1
    • 80 # 看 localhost 設得 Port
    • 確定 # 測試是否可以連線, 若可以正常瀏覽網頁, 代表透過 SSH 是通得.

Pidgin 透過 SOCKS5 連線

  1. 帳號 -> 編輯帳號 # MSN / Gtalk 都一樣設定法.
  2. 代理伺服器
    • SOCKS 5
    • 127.0.0.1
    • 80 # 看 localhost 設得 Port
    • 儲存 # 即可連線

2011年2月24日 星期四

SSH 遠端登入免密碼 by RSA

$ ssh-keygen -t rsa 再client 端產生密鑰 .  持續enter 一直按就可以產生.
  另外 若是打這樣 連後面 enter 都不用按 ssh-keygen -t rsa -P ''
# id_rsa.pub = public key.
# id_rsa = private key.

$ cat .ssh/id_rsa.pub | ssh user@Server "cat - >> .ssh/authorized_keys"
# user= 登入user 帳號
# Server = Domain name or IP address.


參考來源: http://jonny.ubuntu-tw.net/2010/06/ssh-by-rsa.html