terça-feira, 28 de junho de 2011

Cygwin - Openssh server no Windows

Solução simples para acessar um servidor Windows sem a necessidade do Terminal Server.

Baixe o Cygwin:
http://www.cygwin.com/setup.exe

Instalação:

Selecione instalação via Internet
Directorio: C:\cygwin
Instalar para: All Users
Escolha uma pasta para Donwload dos pacotes
Tipo de conexão: Direct Connection
Escolha o local do Download: cygwin.sixsigmaonline.org
Se Aparecer "Setup Alert - This is the first time you've installed Cygwin 1.7.x" window, click OK
Selecione o pacote (expandindo)
Net: openssh


O resto é o Next-Next-Finish de sempre.

Configuração:

Feito isso, abra o cygwin.


$ ssh-host-config
Generating /etc/ssh_host_key
Generating /etc/ssh_host_rsa_key
Generating /etc/ssh_host_dsa_key
Overwrite existing /etc/ssh_config file? (yes/no) yes
Generating /etc/ssh_config file
Overwrite existing /etc/sshd_config file? (yes/no) yes
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
Warning: The following function requires administrator privileges!
Should this script create a local user 'sshd' on this machine? (yes/no) yes
Generating /etc/sshd_config file
Added ssh to C:\WINDOWS\system32\drivers\etc\services


Warning: The following functions require administrator privileges!

Do you want to install sshd as service?
(Say "no" if it's already installed as service) (yes/no) yes

You appear to be running Windows 2003 Server or later. On 2003 and
later systems, it's not possible to use the LocalSystem account
if sshd should allow passwordless logon (e. g. public key authentication).
If you want to enable that functionality, it's required to create a new
account 'sshd_server' with special privileges, which is then used to run
the sshd service under.

Should this script create a new local account 'sshd_server' which has
the required privileges? (yes/no) yes

Please enter a password for new user 'sshd_server'. Please be sure that
this password matches the password rules given on your system.
Entering no password will exit the configuration. PASSWORD=SUA-SENHA

User 'sshd_server' has been created with password 'SUA-SENHA'.
If you change the password, please keep in mind to change the password
for the sshd service, too.

Also keep in mind that the user sshd_server needs read permissions on all
users' .ssh/authorized_keys file to allow public key authentication for
these users!. (Re-)running ssh-user-config for each user will set the
required permissions correctly.


Which value should the environment variable CYGWIN have when
sshd starts? It's recommended to set at least "ntsec" to be
able to change user context without password.
Default is "ntsec". CYGWIN=binmode ntsec tty

The service has been installed under sshd_server account.
To start the service, call net start sshd' or cygrunsrv -S sshd'.

Host configuration finished. Have fun!


Criando Usuários:

Essa é a parte importante, no ítem acima o serviço foi incializado, mas para acessar com um usuário local, ou do Active Directory deveremos colocá-lo no /etc/passwd do cygwin.

Usando mkpasswd - verifique se seu usuário foi cadastrado:

$ mkpasswd.exe -l | grep joaocep
joaocep:unused:1003:513:Privileged server,U-VM\joaocep,S-1-5-21-2545356729-1468247204-1501536967-1003:/var/empty:/bin/bash

note que "joaocep" contém um /bin/bash - isso dá acesso ao shell, caso ele não tenha.

$ mkpasswd.exe -d DOMINIO -u joaocep >> /etc/passwd

OBS: caso aparece um "/bin/false" altere sem problemas usando o wordpad mesmo

c:\cygwin\etc\passwd

Acessando:

Vamos conferir se o sshd está ouvindo a porta padrão
$ netstat -an | grep 22
$ netstat -an | grep :22
TCP 0.0.0.0:22 0.0.0.0:0 LISTENING
TCP [::]:22 [::]:0 LISTENING

De um outro host
$ ssh joaocep@IP-do-Windows

E um abraço!

Observações:
- Aceita-se ssh-copy-id ;)
- Faça scripts de backup com rsync Linux -> Windows -> BSD ...
- Libere a porta 22 tcp no Firewall se for usar Windows 2008



Referência:
http://cygwin.com
https://cwiki.apache.org/VCL/install-configure-cygwin-sshd.html
http://ist.uwaterloo.ca/~kscully/CygwinSSHD_W2K3.html