Skip to content
Snippets Groups Projects
Commit 49109834 authored by Goik Martin's avatar Goik Martin
Browse files

Fail2ban ssh login limit, mlocate and vim

parent cee0826d
No related branches found
No related tags found
No related merge requests found
Showing
with 132 additions and 1 deletion
# Improving ssh handling and security
- Creating local `bin/ssh` and related `gen/known_hosts` file from templates
- Enhancing `sshd` security settings
- Enhancing `sshd` security settings:
- Disallow password based logins
- Disallow `root` login
- Allow `devops` private key based login
......@@ -13,6 +13,7 @@ users:
- ${devopsSshPublicKey}
runcmd:
# ssh daemon defaults
- sed -ie '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
- sed -ie '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
- sed -ie '/^#AuthorizedKeysFile/s/^.*$/AuthorizedKeysFile .ssh/authorized_keys/' /etc/ssh/sshd_config
......
# System update, enhanced security and useful helpers
- Package upgrade and reboot if so required
- Installing and configuring `fail2ban` limiting ssh connection attempts.
- Installing enhanced vim
- Installing mlocate file indexer
*
!.gitignore
\ No newline at end of file
*
!.gitignore
\ No newline at end of file
terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
}
}
required_version = ">= 0.13"
}
provider "hcloud" {
token = var.hcloud_token
}
resource "tls_private_key" "host" {
algorithm = "ED25519"
}
resource "hcloud_ssh_key" "loginUser" {
name = "goik@hdm-stuttgart.de"
public_key = file("~/.ssh/id_ed25519.pub")
}
resource "hcloud_server" "helloServer" {
name = "hello"
image = "debian-12"
server_type = "cx11"
user_data = templatefile("tpl/userData.yml", {
host_ed25519_private = indent(4, tls_private_key.host.private_key_openssh) # yaml format parsing quirk, sigh!
host_ed25519_public = tls_private_key.host.public_key_openssh
devopsSshPublicKey = hcloud_ssh_key.loginUser.public_key
})
ssh_keys = [hcloud_ssh_key.loginUser.id]
firewall_ids = [hcloud_firewall.sshFw.id]
}
resource "local_file" "known_hosts" {
content = "${hcloud_server.helloServer.ipv4_address} ${tls_private_key.host.public_key_openssh}"
filename = "gen/known_hosts"
file_permission = "644"
}
resource "local_file" "ssh_script" {
content = templatefile("tpl/ssh.sh", {
ip = hcloud_server.helloServer.ipv4_address
})
filename = "bin/ssh"
file_permission = "700"
depends_on = [local_file.known_hosts]
}
resource "hcloud_firewall" "sshFw" {
name = "www-firewall"
rule {
direction = "in"
protocol = "tcp"
port = "22"
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}
output "hello_ip_addr" {
value = hcloud_server.helloServer.ipv4_address
description = "The server's IPv4 address"
}
output "hello_datacenter" {
value = hcloud_server.helloServer.datacenter
description = "The server's datacenter"
}
hcloud_token="your_api_token_goes_here"
#!/usr/bin/env bash
GEN_DIR=$(dirname "$0")/../gen
ssh -o UserKnownHostsFile="$GEN_DIR/known_hosts" devops@${ip} "$@"
#cloud-config
ssh_keys:
ed25519_private: |
${host_ed25519_private}
ed25519_public: ${host_ed25519_public}
users:
- name: devops
groups: users, admin
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- ${devopsSshPublicKey}
package_update: true
package_upgrade: true
package_reboot_if_required: true
packages:
- fail2ban
- vim # Enhanced vi command
- mlocate
runcmd:
# Fail2ban activation
- printf "[sshd]\nenabled = true\nbanaction = iptables-multiport" > /etc/fail2ban/jail.local
- systemctl enable fail2ban
- systemctl start fail2ban
# ssh daemon defaults
- sed -ie '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
- sed -ie '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
- sed -ie '/^#AuthorizedKeysFile/s/^.*$/AuthorizedKeysFile .ssh/authorized_keys/' /etc/ssh/sshd_config
- sed -i '$a AllowUsers devops' /etc/ssh/sshd_config
- systemctl restart ssh
# Generation mlocate index
- updatedb
variable "hcloud_token" { # See secret.auto.tfvars
nullable = false
sensitive = true
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment