2024-06-26 01:24:45 -04:00
|
|
|
#!/bin/sh
|
2024-05-03 19:48:16 -04:00
|
|
|
|
|
|
|
# Copyright (C) 2021-2024 Thien Tran
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
# use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
# the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations under
|
|
|
|
# the License.
|
|
|
|
|
2024-07-20 21:23:38 -04:00
|
|
|
set -eu
|
2024-07-02 19:48:15 -04:00
|
|
|
|
2024-05-03 19:48:16 -04:00
|
|
|
output(){
|
2024-06-26 01:24:45 -04:00
|
|
|
printf '\e[1;34m%-6s\e[m\n' "${@}"
|
2024-05-03 19:48:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unpriv(){
|
2024-06-26 01:24:45 -04:00
|
|
|
sudo -u nobody "$@"
|
2024-05-03 19:48:16 -04:00
|
|
|
}
|
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
virtualization=$(systemd-detect-virt)
|
|
|
|
|
2024-05-03 19:48:16 -04:00
|
|
|
# Compliance
|
|
|
|
sudo systemctl mask debug-shell.service
|
|
|
|
sudo systemctl mask kdump.service
|
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Setting umask to 077
|
|
|
|
umask 077
|
|
|
|
sudo sed -i 's/^UMASK.*/UMASK 077/g' /etc/login.defs
|
|
|
|
sudo sed -i 's/^HOME_MODE/#HOME_MODE/g' /etc/login.defs
|
|
|
|
sudo sed -i 's/umask 022/umask 077/g' /etc/bashrc
|
2024-05-03 19:48:16 -04:00
|
|
|
|
|
|
|
# Make home directory private
|
|
|
|
sudo chmod 700 /home/*
|
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Setup NTS
|
2024-06-08 00:09:39 -04:00
|
|
|
sudo dnf install -y chrony
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/GrapheneOS/infrastructure/main/chrony.conf | sudo tee /etc/chrony.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/chrony.conf
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/sysconfig/chronyd | sudo tee /etc/sysconfig/chronyd > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/sysconfig/chronyd
|
2024-05-03 19:48:16 -04:00
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo systemctl restart chronyd
|
2024-05-03 19:48:16 -04:00
|
|
|
|
|
|
|
# Remove nullok
|
|
|
|
sudo /usr/bin/sed -i 's/\s+nullok//g' /etc/pam.d/system-auth
|
|
|
|
|
|
|
|
# Harden SSH
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/ssh/sshd_config.d/10-custom.conf | sudo tee /etc/ssh/sshd_config.d/10-custom.conf > /dev/null
|
2024-05-03 19:48:16 -04:00
|
|
|
sudo chmod 644 /etc/ssh/sshd_config.d/10-custom.conf
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/ssh/ssh_config.d/10-custom.conf | sudo tee /etc/ssh/ssh_config.d/10-custom.conf > /dev/null
|
2024-05-03 19:48:16 -04:00
|
|
|
sudo chmod 644 /etc/ssh/ssh_config.d/10-custom.conf
|
2024-07-26 07:59:01 -04:00
|
|
|
sudo mkdir -p /etc/systemd/system/sshd.service.d/
|
|
|
|
sudo chmod 755 /etc/systemd/system/sshd.service.d/
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/GrapheneOS/infrastructure/main/systemd/system/sshd.service.d/local.conf | sudo tee /etc/systemd/system/sshd.service.d/override.conf > /dev/null
|
2024-05-03 19:48:16 -04:00
|
|
|
sudo systemctl daemon-reload
|
|
|
|
sudo systemctl restart sshd
|
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Security kernel settings
|
2024-07-26 07:59:01 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/secureblue/secureblue/live/files/system/usr/etc/modprobe.d/blacklist.conf | sudo tee /etc/modprobe.d/server-blacklist.conf > /dev/null
|
2024-06-04 08:33:52 -04:00
|
|
|
sudo chmod 644 /etc/modprobe.d/server-blacklist.conf
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/sysctl.d/99-server.conf | sudo tee /etc/sysctl.d/99-server.conf > /dev/null
|
2024-06-04 08:33:52 -04:00
|
|
|
sudo chmod 644 /etc/sysctl.d/99-server.conf
|
2024-05-03 19:48:16 -04:00
|
|
|
sudo dracut -f
|
|
|
|
sudo sysctl -p
|
2024-05-29 19:00:50 -04:00
|
|
|
|
|
|
|
sudo grubby --update-kernel=ALL --args='mitigations=auto,nosmt spectre_v2=on spectre_bhi=on spec_store_bypass_disable=on tsx=off kvm.nx_huge_pages=force nosmt=force l1d_flush=on spec_rstack_overflow=safe-ret gather_data_sampling=force reg_file_data_sampling=on random.trust_bootloader=off random.trust_cpu=off intel_iommu=on amd_iommu=force_isolation efi=disable_early_pci_dma iommu=force iommu.passthrough=0 iommu.strict=1 slab_nomerge init_on_alloc=1 init_on_free=1 pti=on vsyscall=none ia32_emulation=0 page_alloc.shuffle=1 randomize_kstack_offset=on debugfs=off lockdown=confidentiality module.sig_enforce=1 console=tty0 console=ttyS0,115200'
|
2024-05-03 19:48:16 -04:00
|
|
|
|
|
|
|
# Disable coredump
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/security/limits.d/30-disable-coredump.conf | sudo tee /etc/security/limits.d/30-disable-coredump.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/security/limits.d/30-disable-coredump.conf
|
2024-06-04 17:29:50 -04:00
|
|
|
sudo mkdir -p /etc/systemd/coredump.conf.d
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/systemd/coredump.conf.d/disable.conf | sudo tee /etc/systemd/coredump.conf.d/disable.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/systemd/coredump.conf.d/disable.conf
|
2024-05-03 19:48:16 -04:00
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Setup DNF
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/dnf/dnf.conf | sudo tee /etc/dnf/dnf.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/dnf/dnf.conf
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo sed -i 's/^metalink=.*/&\&protocol=https/g' /etc/yum.repos.d/*
|
2024-05-03 19:48:16 -04:00
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Setup automatic updates
|
2024-05-29 19:05:07 -04:00
|
|
|
sudo dnf install -y dnf-automatic
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo sed -i 's/apply_updates = no/apply_updates = yes\nreboot = when-needed/g' /etc/dnf/automatic.conf
|
|
|
|
sudo systemctl enable --now dnf-automatic.timer
|
2024-05-03 19:48:16 -04:00
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Remove unnecessary packages
|
|
|
|
sudo dnf remove -y cockpit*
|
2024-05-03 19:48:16 -04:00
|
|
|
|
2024-06-09 08:31:16 -04:00
|
|
|
# Install hardened_malloc
|
|
|
|
sudo dnf copr enable secureblue/hardened_malloc -y
|
|
|
|
sudo dnf install -y hardened_malloc
|
|
|
|
echo 'libhardened_malloc.so' | sudo tee /etc/ld.so.preload
|
|
|
|
sudo chmod 644 /etc/ld.so.preload
|
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Install appropriate virtualization drivers
|
|
|
|
if [ "$virtualization" = 'kvm' ]; then
|
|
|
|
sudo dnf install -y qemu-guest-agent
|
|
|
|
fi
|
2024-05-03 19:48:16 -04:00
|
|
|
|
|
|
|
# Setup unbound
|
|
|
|
sudo dnf install unbound -y
|
|
|
|
|
|
|
|
echo 'server:
|
|
|
|
chroot: ""
|
|
|
|
auto-trust-anchor-file: "/var/lib/unbound/root.key"
|
|
|
|
trust-anchor-signaling: yes
|
|
|
|
root-key-sentinel: yes
|
2024-06-08 01:11:31 -04:00
|
|
|
tls-cert-bundle: "/etc/ssl/cert.pem"
|
2024-05-03 19:48:16 -04:00
|
|
|
tls-ciphers: "PROFILE=SYSTEM"
|
|
|
|
hide-http-user-agent: yes
|
|
|
|
hide-identity: yes
|
|
|
|
hide-trustanchor: yes
|
|
|
|
hide-version: yes
|
|
|
|
deny-any: yes
|
|
|
|
harden-algo-downgrade: yes
|
|
|
|
harden-large-queries: yes
|
|
|
|
harden-referral-path: yes
|
|
|
|
ignore-cd-flag: yes
|
|
|
|
max-udp-size: 3072
|
|
|
|
module-config: "validator iterator"
|
|
|
|
qname-minimisation-strict: yes
|
|
|
|
unwanted-reply-threshold: 10000000
|
|
|
|
use-caps-for-id: yes
|
|
|
|
outgoing-port-permit: 1024-65535
|
|
|
|
prefetch: yes
|
|
|
|
prefetch-key: yes
|
2024-06-21 18:20:41 -04:00
|
|
|
|
2024-06-24 21:22:42 -04:00
|
|
|
# ip-transparent: yes
|
|
|
|
# interface: 127.0.0.1
|
|
|
|
# interface: ::1
|
|
|
|
# interface: 242.242.0.1
|
|
|
|
# access-control: 242.242.0.0/16 allow
|
2024-06-21 18:20:41 -04:00
|
|
|
|
2024-05-03 19:48:16 -04:00
|
|
|
forward-zone:
|
|
|
|
name: "."
|
|
|
|
forward-tls-upstream: yes
|
|
|
|
forward-addr: 1.1.1.2@853#security.cloudflare-dns.com
|
|
|
|
forward-addr: 1.0.0.2@853#security.cloudflare-dns.com
|
|
|
|
forward-addr: 2606:4700:4700::1112@853#security.cloudflare-dns.com
|
|
|
|
forward-addr: 2606:4700:4700::1002@853#security.cloudflare-dns.com' | sudo tee /etc/unbound/unbound.conf
|
|
|
|
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/unbound/unbound.conf
|
|
|
|
|
2024-06-08 00:29:56 -04:00
|
|
|
sudo mkdir -p /etc/systemd/system/unbound.service.d
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/systemd/system/unbound.service.d/override.conf | sudo tee /etc/systemd/system/unbound.service.d/override.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/systemd/system/unbound.service.d/override.conf
|
|
|
|
|
2024-05-03 19:48:16 -04:00
|
|
|
sudo systemctl enable --now unbound
|
|
|
|
|
|
|
|
# Setup yara
|
2024-06-07 23:41:02 -04:00
|
|
|
#sudo dnf install -y yara
|
|
|
|
#sudo insights-client --collector malware-detection
|
|
|
|
#sudo sed -i 's/test_scan: true/test_scan: false/' /etc/insights-client/malware-detection-config.yml
|
2024-05-03 19:48:16 -04:00
|
|
|
|
2024-06-09 08:34:13 -04:00
|
|
|
# Enable auto TRIM
|
|
|
|
sudo systemctl enable fstrim.timer
|
|
|
|
|
|
|
|
### Differentiating bare metal and virtual installs
|
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Setup fwupd
|
|
|
|
if [ "$virtualization" = 'none' ]; then
|
|
|
|
sudo dnf install -y fwupd
|
|
|
|
echo 'UriSchemes=file;https' | sudo tee -a /etc/fwupd/fwupd.conf
|
|
|
|
sudo systemctl restart fwupd
|
|
|
|
mkdir -p /etc/systemd/system/fwupd-refresh.service.d
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/systemd/system/fwupd-refresh.service.d/override.conf | sudo tee /etc/systemd/system/fwupd-refresh.service.d/override.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/systemd/system/fwupd-refresh.service.d/override.conf
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo systemctl daemon-reload
|
|
|
|
sudo systemctl enable --now fwupd-refresh.timer
|
|
|
|
fi
|
2024-05-03 19:48:16 -04:00
|
|
|
|
|
|
|
# Setup tuned
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo dnf install -y tuned
|
|
|
|
sudo systemctl enable --now tuned
|
|
|
|
|
|
|
|
if [ "$virtualization" = 'none' ]; then
|
|
|
|
sudo tuned-adm profile latency-performance
|
2024-05-03 19:48:16 -04:00
|
|
|
else
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo tuned-adm profile virtual-guest
|
2024-05-03 19:48:16 -04:00
|
|
|
fi
|
|
|
|
|
2024-05-29 20:48:45 -04:00
|
|
|
# Setup networking
|
2024-07-26 07:59:01 -04:00
|
|
|
sudo systemctl enable --now firewalld
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo firewall-cmd --permanent --remove-service=cockpit
|
|
|
|
sudo firewall-cmd --reload
|
|
|
|
sudo firewall-cmd --lockdown-on
|
|
|
|
|
|
|
|
sudo mkdir -p /etc/systemd/system/NetworkManager.service.d
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://gitlab.com/divested/brace/-/raw/master/brace/usr/lib/systemd/system/NetworkManager.service.d/99-brace.conf | sudo tee /etc/systemd/system/NetworkManager.service.d/99-brace.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/systemd/system/NetworkManager.service.d/99-brace.conf
|
2024-05-29 19:00:50 -04:00
|
|
|
sudo systemctl daemon-reload
|
|
|
|
sudo systemctl restart NetworkManager
|
|
|
|
|
2024-05-29 20:48:45 -04:00
|
|
|
# irqbalance hardening
|
2024-05-29 20:23:29 -04:00
|
|
|
sudo mkdir -p /etc/systemd/system/irqbalance.service.d
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://gitlab.com/divested/brace/-/raw/master/brace/usr/lib/systemd/system/irqbalance.service.d/99-brace.conf | sudo tee /etc/systemd/system/irqbalance.service.d/99-brace.conf > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/systemd/system/irqbalance.service.d/99-brace.conf
|
2024-05-29 20:23:29 -04:00
|
|
|
sudo systemctl daemon-reload
|
|
|
|
sudo systemctl restart irqbalance
|
|
|
|
|
2024-05-29 19:00:50 -04:00
|
|
|
# Setup notices
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/issue | sudo tee /etc/issue > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/issue
|
2024-07-03 05:23:28 -04:00
|
|
|
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/issue | sudo tee /etc/issue.net > /dev/null
|
2024-06-06 00:46:24 -04:00
|
|
|
sudo chmod 644 /etc/issue.net
|
2024-05-29 20:11:34 -04:00
|
|
|
|
|
|
|
# Final notes to the user
|
|
|
|
output 'Server setup complete. To use unbound for DNS, you need to run the following commands:'
|
|
|
|
output 'nmcli con mod <interface name> ipv4.dns 127.0.0.1'
|
|
|
|
output 'nmcli con mod <interface name> ipv6.dns ::1'
|