Installing Cygwin on Windows 7 And Configure SSH

Execute  ssh –V to check version.
Configure ssh daemon
  1. Open Cygwin command prompt
  2. Execute the following command

    ssh-host-configwindows

  3. When asked if privilege separation should be used, answer no.
  4. When asked if sshd should be installed as a service, answer yes.
  5. When asked about the value of CYGWIN environment variable enter ntsec.
  6. Here is the example session of this command, note that the input typed by the user is shown in pink and output from the system is shown in gray.

    Example of using ssh-host-config服務器

Start SSH daemon
  1. Find my computer icon either on your desktop or in the start-up menu, right-click on it and select Manage from the context menu.
  2. Open Services and Applications in the left-hand panel then select theServices item.
  3. Find the CYGWIN sshd item in the main section and right-click on it.
  4. Select Start from the context menu.

     
    Start SSHD servicesession

  5. A small window should pop-up indicating the progress of the service start-up. After that window disappears the status of CYGWIN sshd service should change to Started.
Setup authorization keys

Eclipse plugins and hadoop scripts require ssh authentication to be performed through authorization keys rather than through passwords. To enable key based authorization you have to setup authorization keys. The following steps describe how to do it. app

  1. Open cygwin command prompt
  2. Execute the following command to generate keys

    ssh-keygenssh

  3. When prompted for filenames and pass phrases press ENTER to accept default values.
  4. After command has finished generating they key, enter the following command to change into your .ssh directory

    cd ~/.ssh工具

  5. Check if the keys where indeed generated by executing the following command

    ls -l oop

    You should see two file id_rsa.pub and id_rsa with the recent creation dates. These files contain authorization keys.測試

  6. To register the new authorization keys enter the following command. Note that double brackets, they are very important.

    cat id_rsa.pub >> authorized_keysui

  7. Now check if the keys where set-up correctly by executing the following command

    ssh localhost this

    Since it is a new ssh installation you warned that authenticity of the host could not be established and will be prompted whether you really want to connect, answer yes and press ENTER. You should see the cygwin prompt again, which means that you have successfully connected.

  8. Now execute the command again

    ssh localhost

    This time you should not be prompted for anything.

Setting up authorization keys

 

3. 建立用戶組與密碼,在cygwin的控制檯中輸入以下命令:

mkpasswd –cl > /etc/passwd

mkgroup --local > /etc/group

至此服務器的配置已經完成,能夠基於用戶與密碼實現從遠程登錄SSH服務器。

經過密碼登錄SSH服務器

在cygwin的控制檯輸入以下命令:

ssh username@hostname

其中hostname爲創建SSH服務器的計算機IP地址或機器名,username爲用於登錄該計算機的用戶名稱,若是根據前面的服務器配置步驟,應當就是該計算現有的某個帳戶名稱。

若是是所用的客戶端計算機是第一次登錄SSH服務器,命令行中會提示「Are you sure you want to continue connecting (yes/no) ?」,只要輸入yes便可,SSH服務器會自動將此次的登錄信息存儲在/.ssh/known_host文件中。

接着屏幕中會提示輸入登錄帳戶的密碼,也就是ssh服務器中登錄帳戶的密碼,當顯示「Fanfare!!! You are successfully logged in to this server!!!」時,說明已經成功登錄到ssh服務器計算機內了,須要注意的是,此時在當前控制檯內輸入的命令都將在ssh服務器計算機裏運行。

若是要退出登錄,只要在命令行中輸入

exit

便可,屏幕中會顯示「logout Connection to [hostname] closed.」

經過密鑰登錄ssh服務器

1. 在客戶端計算機上安裝好包含openssh的cygwin;

2. 爲客戶端生成rsa密鑰,在cygwin命令行中輸入:

ssh-keygen –t rsa

對於提示輸入密鑰文件名或密鑰的包含密碼時均可以直接回車,從而將無密碼保護的密鑰保存在缺省文件中,兩個密鑰文件都保存在客戶端當前用戶的cygwin帳戶目錄的/.ssh文件夾中,其中公鑰的名稱比私鑰的名稱只多一個pub後綴。

3. 將客戶端的公鑰上傳到ssh服務器,這時能夠經過前面已經成功的基於密碼的方式完成文件的上傳,即在客戶端的cygwin命令行中輸入以下命令:

cd ~/.ssh

scp id_rsa.pub username@hostname:~/.ssh/newkey

4. 在ssh服務器中將客戶端的公鑰添加到authorized_keys中,在服務器的cygwin命令行中輸入以下命令:

cd ~/.ssh

ls (這時能夠在目錄中看到newkey文件)

cat newkey >> authorized_keys

rm newkey

chmod 600 authorized_keys (更改文件的屬性)

5. 這時已經能夠從客戶端經過密鑰對登錄到ssh服務器上了,只需在客戶端的cygwin命令行中輸入:

ssh username@hostname

在沒有密碼輸入提示的狀況下,應該能夠看到「Ranfare!!! ……」,這說明已經成功登錄,若是在此時繼續輸入

whoami

能夠看到在服務器上的登錄用戶名。

須要注意的事情

1. 因爲cygwin不能直接支持包含空格的文件路徑,所以在cygwin中執行的應用盡可能不要有包含空格的路徑,若是必定要用,須要在空格前添加「/」來標註;

2. windows的帳戶必定要有密碼,不然只能經過修改sshd_conf來容許無密碼的帳戶登錄;

3. 默認的sshd服務不容許其與桌面進行交互,所以當經過ssh執行基於窗口的命令時,將沒法看到建立的窗口,而只能在「任務管理器」中看到該進程已經被建立。爲此,可經過進入「控制面板-管理工具-服務」窗口中打開「CYGWIN sshd」服務,在其彈出窗口的「登錄」屬性頁中選中「容許服務與桌面交互」複選框,若是此時在客戶端輸入

ssh username@host ‘notepad’

將能夠看到對話框窗口可以正常彈出。

cygwin command for ssh:

Start sshd service

cygrunsrv -S ssh or cygrunsrv –start sshd or net start sshd

Stop sshd service

cygrunsrv –stop sshd or net stop sshd

Delete sshd service

cygrunsrv -R sshd or sc delete sshd

一、安裝SSH

默認的Cygwin沒有安裝ssh,因此從新運行http://www.cygwin.com/setup.exe

在Select Packages的時候,在search輸入ssh,選擇openssh:The OpenSSH server and client programs

二、配置SSH服務(以管理員身份運行cygwin)

執行:ssh-host-config

Should privilege separation be used?   yes

Do you want to install sshd as a service?  yes

默認確認

Do you want to use a different name?  no

Create new privileged user account 'cyg_server'?  yes

輸入密碼

啓動SSH服務:cygrunsrv  -S  sshd

三、生成SSH Key

ssh-keygen  -t  rsa(密碼爲空,路徑默認)

cp  .ssh/id_rsa.pub  .ssh/authorized_keys

四、登錄測試

ssh  localhost

The solution of 「Connection closed by ::1」  or 「Connection closed by 127.0.0.1」 when executed ssh localhost

問題描述:

在Win7下Cygwin中,使用ssh localhost命令,出現Connection closed by 127.0.0.1的問題。

在google上看到cygwin的一個官方mail問答。參考Installing the Cygwin SSH daemon 。問題關鍵就是一個特殊的權限問題,在SYSTEM帳戶下運行的sshd服務須要有改變user id的特權(關於權限的問題,在cygwin的/usr/share/doc/Cygwin/openssh.README 中已經說得很明確了:2003 Server has a funny new feature.  When starting services under SYSTEM  account, these services have nearly all user rights which SYSTEM holds...except for the "Create a token object" right, which is needed to allow public key authentication :-( )。因此事件日誌會有seteuid的錯誤。

解決方案一:就是讓cygwin幫你建立兩個特殊用戶sshd和cyg_server,其中sshd服務在cyg_server用戶下運行,cyg_server有相應的權限(在/usr/share/doc/Cygwin/openssh.README 中也有說明)。

***** 關鍵的就是這兩個用戶都要建立。

在配置的時候注意一下幾點:

1.當屏幕顯示關於「privilege seperation」,輸入yes後回車;

當屏幕顯示關於「create local user sshd」,輸入yes後回車;

*** Query: Should privilege separation be used? (yes/no) yes #輸入yes

*** Info: Updating /etc/sshd_config file

*** Warning: The following functions require administrator privileges!

*** Query: Do you want to install sshd as a service?

*** Query: (Say "no" if it is already installed as a service) (yes/no) yes #輸入yes

*** Info: Note that the CYGWIN variable must contain at least "ntsec"

*** Info: for sshd to be able to change user context without password.

*** Query: Enter the value of CYGWIN for the daemon: []

*** Info: On Windows Server 2003, Windows Vista, and above, the

設置的 cyg_server密碼要記清楚,在win的服務裏面用這個用戶啓動ssh服務,密碼和這裏設置的要同樣

2. ssh-host-config從新配置

若是初始配置錯誤,大部分網上都建議所有刪除再從新安裝,但cygwin一旦安裝很難徹底卸載,只須要把ssh服務中止再從新執行這個命令,再把以前的覆蓋掉就能夠。

步驟:1.sc delete sshd

             2.系統重啓,就再配置就ok了

3.ssh-host-config關鍵最後兩個輸入
*** Info: This script plans to use 'cyg_server'.
*** Info: 'cyg_server' will only be used by registered services.
*** Query: Do you want to use a different name? (yes/no) no
*** Query: Create new privileged user account 'cyg_server'? (yes/no) yes
*** Info: Please enter a password for new user cyg_server.  Please be sure
*** Info: that this password matches the password rules given on your system.
*** Info: Entering no password will exit the configuration.
*** Query: Please enter the password:
*** Query: Reenter:

解決方案二:

一、開始——運行——services.msc

二、右鍵CYGWIN sshd——屬性——登陸選項卡——選擇「此帳戶」——瀏覽——高級——當即查找——選擇你的帳戶名(必須爲管理員權限)——輸入密碼(必需要有,空密碼不接受)——肯定。

三、重啓CYGWIN sshd服務便可。

這樣就以你的帳戶的名義啓動了這個服務。然後ssh localhost成功。

Windows 平臺下安裝Cygwin後,sshd服務沒法啓動

系統日誌記錄信息:

事件 ID ( 0 )的描述(在資源( sshd )中)沒法找到。本地計算機可能沒有必要的註冊信息或消息 DLL 文件來從遠程計算機顯示消息。您可能可使用 /AUXSOURCE= 標識來檢索詞描述;查看幫助和支持以瞭解詳細信息。下列信息是事件的一部分: sshd: PID 7340: `sshd' service stopped, exit status: 255.

從新配置sshd服務

解決方案:

按上圖配置命令的提示說明,須要執行mkpasswd 和 mkgroup從新生成權限信息

依次執行下面命令

$ mkpasswd -l > /etc/passwd
$ mkgroup -l > /etc/group

##########  如何解決啓動過程當中亂碼問題,不支持中文  ###########

#######################################################

使用ls能夠正常查看中文格式的文件名,可是在執行net start sshd的時候就出現如下狀況:

查看系統編碼:

查看cygwin的編碼:

更改一下cygwin的編碼格式就ok了~

再次啓動就能夠正常顯示了:

相關文章
相關標籤/搜索