« httpd 升級與 limitipconn 模組安裝 | 首頁 | HDD Regenerator 操作紀實 »

2006年04月21日

在 WinXP 使用 Cygwin 建置 sshd & squid proxy server

目的: 讓 Windows 成為 sshd server, 作為 ssh tunnel 運用

安裝 Cygwin

http://www.cygwin.com/ 下載並安裝 Cygwin 於 c:\cygwin

  • 建立 c:\cygwin 目錄
  • 將 setup.exe 置入 c:\cygwin
  • 執行 setup.exe
  • Choose A Download Source → Install from Internet
  • Select Root Install Directory → c:\cygwin
    Install For → All Users
    Default Text File Type → Unix / binary
  • Select Local Package Directory → Local Package Directory: c:\cygwin
  • Select Your Internet Connection → Direct Connection
  • Choose A Download Site → ftp://ftp.nctu.edu.twftp://ftp.ntu.edu.tw

本篇所使用的 Package:

  • Admin → cygrunsrv
  • Net → openssh
  • Web → squid

設置 sshd service

啟用 sshd

ssh-host-config

Should privilege separation be used? (yes/no) no
Do you want to install sshd as service? (yes/no) yes
CYGWIN= (直接按 Enter 鍵接受預設值 "ntsec")

cygrunsrv --start sshd 啟動 sshd 服務

將 sshd 預設 tcp 22 port 變更成其他埠號的方法

cd /etc
notepad sshd_config
→ 將 Port 22 改成 Port 80
cygrunsrv --stop sshd && cygrunsrv --start sshd 重新啟動 sshd service

若 /etc/sshd_config 的 owner 是 system, 須先變更 owner: chown $USER sshd_config 才能編輯 sshd_config. 修改完後記得將 owner 回復為 system: chown system sshd_config, 最後再重新啟動 sshd service 即可

調整 Windows 防火牆設定

控制台 → Windows 防火牆 → 例外 → 新增連接埠

名稱: cygwin-sshd (或其他自己喜歡的名稱), 連接埠編號: 22 (TCP) (若已改成 80 port, 就填入 80)

如要限制連入 sshd 的來源 IP, 可另外加裝 tcp_wrappers package (在 Net 類別裡)

以 ssh 認證金鑰進行連線

適用情況:

  1. 連線時不想輸入密碼
  2. 安裝 Cygwin 的那台電腦, Windows 帳號沒有密碼

ssh-keygen -t rsa 過程皆按 Enter (ssh-user-config 亦可建立金鑰)
cd ~/.ssh
mv id_rsa.pub authorized_key
將 public key 更名為 authorized_key

將 id_rsa (private key) 複製到想以 ssh 連入 Cygwin 的電腦 (client), 使用 puttygen.exe 將 id_rsa 轉成 PuTTY Private Key File:

  • 執行 puttygen.exe
  • 點選 File → Load private key (剛剛的 id_rsa 檔案)
  • 載入後點選 Save private key, 另存成副檔名為 .ppk 的檔案

Client 使用 PuTTY, 以 ssh protocol 連入 Cygwin sshd 時, 點選 Connection → SSH → Auth → Private key file for authentication: Browser 載入剛剛存好的 .ppk 檔, 再進行 ssh 連線, 輸入帳號後即可登入.

也就是說, Cygwin + sshd 這台電腦 (server), 保留 public key 在 $HOME/.ssh/authorized_key
其他想使用 PuTTY 以 ssh 連入 Cygwin + sshd 的電腦 (client), 連線時載入 .ppk (private key) 檔案

為 Cygwin 帳號設置密碼

passwd

  • 效用等同在控制台設定 Windows 使用者密碼
  • 密碼設置後, 下次開機進入 Windows 時, 系統會詢問密碼
  • 若想開機直接登入桌面, 請參考: 關於 Windows XP 的三個備忘
  • 若已完成直接登入的設置, 但又變更密碼時的處理:
     
    1. 密碼變更後, 立即執行 control userpasswords2 (Win 2000 沒有最後的 "2")
    2. 先勾選「必須輸入使用者名稱和密碼,才能使用這台電腦」
    3. 再點選預設登入帳號
    4. 最後再取消勾選「必須輸入使用者名稱和密碼,才能使用這台電腦」
    5. 按「確定」後, 系統就會再詢問一次密碼, 此時輸入剛剛變更的密碼即可

設置 squid service

修正 squid 2.4.STABLE7-1 的 RLIMIT_NOFILE: Too many open files 問題

  1. 執行 UltraEdit
  2. 開啟 c:\cygwin\bin\squid.exe
  3. 按 Ctrl + G 出現 "HEX Goto" 對話框
  4. 輸入 0x74420
  5. 將 00 20 00 00 改成 00 0C 00 00
  6. 存檔後離開 UltraEdit

參考資料: http://article.gmane.org/gmane.os.cygwin/40135

初始化 squid

echo "nameserver your.dns.server.ip" > /etc/resolv.conf
cd /etc
notepad squid.conf

cache_effective_user your_account
cache_effecitve_group none
http_access allow all

squid -z
squid -d 1 -N 若沒問題, 按 Ctrl + C 終止運行

若出現 Windows 防火牆訊息:

  • 如果 squid 只是用在 ssh tunnel, 直接按 X 或 "稍後詢問我" 直接跳過即可, 這樣只能本機使用 (127.0.0.1:3128)
  • 如果想讓其他人使用 squid proxy server, 按 "解除封鎖", 或稍後再到 Windows 防火牆設定亦可

以系統服務的方式啟動 squid

cygrunsrv --install squid --path /usr/bin/squid.exe --disp "CYGWIN squid" --args "-N" --env CYGWIN="ntsec"
cygrunsrv --start squid

若無法以服務的方式啟動 squid (以 netstat -na | grep 3128 驗證), 且在事件檢視器 (eventvwr /s → 應用程式) 中發現 Cygwin 的錯誤內容為:

Cannot open '/usr/logs/access.log' for writing.
The parent directory must be writeable by the
user 'xxxx', which is the cache_effective_user
set in squid.conf..

此時只要針對相關檔案與目錄變更擁有權即可解決:

chown -R system:root /usr/cache /usr/logs/*

其他備忘

  • cygrunsrv --install service 時, 若不加 --type 參數, 預設為開機自動啟動. 若要變成手動啟動可加上 --type manual 參數
  • cygrunsrv --list -V 可顯示 Cygwin Service 詳細資訊
  • 安裝 UltraEdit 時若勾選 "Add UltraEdit-32 to path environment variable" 者, 可於本機使用 uedit32 替代 notepad 以及 vi
  • 若要讓遠端 http server 無法使用 HTTP_X_FORWARDED_FOR 反查透過 squid proxy server 連線的真實 ip, 可於 squid.conf 設定 forwarded_for off

讓 Cygwin 顯示中文

vi ~/.inputrc

取消註解以下幾行:

set meta-flag on
set convert-meta off
set input-meta on
set output-meta on

vi ~/.bashrc

加入以下資料:

alias ls='ls -hF --show-control-chars --color=tty'


參考資料

延伸閱讀

Posted by Jamyy at 2006年04月21日 08:58

Trackback Pings

TrackBack URL for this entry:
http://cha.homeip.net/cgi-bin/mt/mt-tb.cgi/190

Listed below are links to weblogs that reference 在 WinXP 使用 Cygwin 建置 sshd & squid proxy server:

» 在 WinXP 使用 Cygwin 建置 sshd & squid proxy server from Qiang's Homepage
在 WinXP 使用 Cygwin 建置 sshd & squid proxy server 目的: 讓 Windows 成為 sshd server, 作為 ssh tunnel 運用 在 Cygwin 設置 sshd service 變更 sshd 埠號 建立 ssh 認證金鑰免密碼登入 在 Cygwin 設置 squid proxy server... [Read More]

Tracked on 2006年05月12日 01:04

Comments

您好:
我有照您網頁上的指示照做, 目的也是想從公司走ssh從家裡上msn, 但是有些問題想請問您.
ssh將port改成80的方法我照做了(應該要把原先sshd_config裡面# port 22 前面有#號拿掉吧?)
squid proxy server我也設定好了, 結果公司遠端可以直接走 proxy 出去, 卻無法經由 ssh. 我很好奇 若是 squid 也走 port 80, 那這樣不會有衝突嗎? 因為公司只開port 80,
我也很好奇 ssd 與 squid 都走 port 80, 那...? 而 ssh 與 squid 之間怎麼 routing packet 的也是很神奇. 希望大哥能指點一二. 感恩 ^^

Posted by: 鸚鵡螺 at 2007年01月01日 23:36

您好! 關於您的問題:
1. 沒錯, port 那行的 # 要拿掉
2. squid 使用原本的 3128 port 即可, 不用變更為 80
3. 所有的 net server 都不可以重複使用 port, 會出現 "Address already in use" 的錯誤. 只有先佔用該 port 的軟體可以運作.
4. 最理想的作法是: 那台當 server 的 XP, 防火牆設定只允許以 80 port 連入 sshd, 公司 client pc 的 proxy 以及 sock 設定方式請參考: /blog/archives/2006/01/putty_ssh_tunne.html
從本機經 ssh tunnel 到 sshd 之後的轉向 (forwarded ports) 都可在 putty 設定畫面中完成設置. 至於原理是什麼, 我也沒研究 :P

Posted by: Jamyy at 2007年01月02日 15:20

您好
我在設定cygwin的時候發生了問題

$ ssh-host-config
Generating /etc/ssh_host_key
Generating /etc/ssh_host_rsa_key
Generating /etc/ssh_host_dsa_key
Generating /etc/ssh_config file
Privilege separation is set to yes by default since OpenSSH 3.3.
However, this requires a non-privileged account called 'sshd'.
For more info on privilege separation read /usr/share/doc/openssh/README.privsep
.

Should privilege separation be used? (yes/no) yes
Generating /etc/sshd_config file
grep: /ssh-host-config.1948/services: No such file or directory
grep: /ssh-host-config.1948/services: No such file or directory
/usr/bin/ssh-host-config: line 367: /ssh-host-config.1948/services: No such file
or directory
WARNING: Adding ssh to C:\cygwin\ssh-host-config.1948\services failed!
umount: /ssh-host-config.1948: No such file or directory

Host configuration finished. Have fun!

請問一下要怎樣才能正確的設定SSHD呢?

Posted by: dreams at 2007年11月26日 15:31

您好
這應該是 cygwin 操作者權限不足所致
請確定您的 windows 帳號屬於 Administrators 群組

Posted by: Jamyy at 2007年11月26日 16:34