Mode de traduction activé, modification en cours

Script d'installation d'un service SFTP sous Windows Server

Mise à jour : par
PECHEUR Daniel
35 vues

# Install OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

# Start and configure the OpenSSH Server service
Set-Service -Name sshd -StartupType 'Automatic'
Start-Service -Name sshd

# Open port 22 in the firewall
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' `
-Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

# Create SFTP user
New-LocalUser -Name "sftpuser" -Password (ConvertTo-SecureString "P@ssw0rd!" -AsPlainText -Force) -FullName "SFTP User" -Description "User for SFTP" -PasswordNeverExpires
Add-LocalGroupMember -Group "Utilisateurs" -Member "sftpuser"

# Create directory for SFTP user
New-Item -Path "C:\SFTP\sftpuser" -ItemType Directory
icacls "C:\SFTP\sftpuser" /grant 'Administrators:F'
icacls "C:\SFTP\sftpuser" /grant 'sftpuser:RX'

# Configure sshd_config for SFTP
$sshd_config_path = 'C:\ProgramData\ssh\sshd_config'
(Get-Content $sshd_config_path) -replace '#?Subsystem sftp.*', 'Subsystem sftp sftp-server.exe' | Set-Content $sshd_config_path
Add-Content $sshd_config_path "`nMatch User sftpuser`n ChrootDirectory C:\SFTP\sftpuser`n ForceCommand internal-sftp`n AllowTcpForwarding no"

# Restart the SSH service
Restart-Service sshd