-
Notifications
You must be signed in to change notification settings - Fork 0
Setting up SSH Server
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
choco install openssh --pre
# Start service
Start-Service *sshd*
# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'
# Confirm the firewall rule is configured. It should be created automatically by setup.
Get-NetFirewallRule -Name *ssh*
# There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled
# If the firewall does not exist, create one
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
ssh user@your_ipv4
You can get your IPV4 by using ipconfig
command or use this :
(Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -eq 'Dhcp' }).IPAddress
Notice: It may not work for you
ssh-keygen -b 2048 -t rsa
If ssh host is standard user place your Public Key in ~\.ssh\authorized_keys
You can also do this with a command :
scp C:\Users\username\.ssh\id_rsa.pub user1@domain1:C:\Users\username\.ssh\authorized_keys
The contents of your public key ~\.ssh\id_rsa.pub)
needs to be placed on the server into a text file called administrators_authorized_keys in C:\ProgramData\ssh\
. The ACL on this file needs to be configured to only allow access to administrators and System.
Add line to C:\ProgramData\ssh\sshd_config
:
Match Group administratorzy
AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
Place your Public Key into C:\ProgramData\ssh\administrators_authorized_keys
. Then setup permissions.
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administratorzy:F" /grant "SYSTEM:F"
Restart-Service *sshd*
sudo apt remove openssh-server
sudo apt install openssh-server
- Edit the sshd_config file by running the command
sudo vim /etc/ssh/sshd_config
- In the sshd_config file:
- Change
PasswordAuthentication
to yes - Add your login user to the bottom of the file by using this command:
AllowUsers yourusername
. Don't forget to replaceyourusername
with your actually username. - Do
:wq
to Save and Exit
- Change
- Check the status of the ssh service:
service ssh status
- Start ssh server
sudo service ssh start
- Restart ssh server
sudo service ssh --full-restart
- Edit visudo
sudo visudo
- Add the following line
%sudo ALL=NOPASSWD: /usr/sbin/sshd
after %sudo ALL=(ALL:ALL) ALL
You can test that you don't need a sudo password when you start ssh by running sudo service ssh --full-restart (if ssh is already running) or sudo service ssh start(if ssh is not running)
Now you need to set up port forwarding to be able to connect to your WSL server and not interfere in any SSH Servers on your Windows machine.
Note: You DO NOT need to do this if you don't have any SSH servers on your Windows machine
-
listenport=
- could be any opened and unused port -
connectport
- is your ssh server port on WSL (by default 22) -
connectaddress=
- is your WSL address (ip addr | grep 'eth0' | grep 'inet ' | awk '{print $2}'
). FromWSL Build 18945
you can simply uselocalhost
. -
Example :
netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=22 connectaddress=localhost
ssh username@your_computers_ipv4