2024-06-26 01:24:45 -04:00
#!/bin/sh
2024-05-17 01:08:26 -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.
#Please note that this is how I PERSONALLY setup my computer - I do some stuff such as not using anything to download GNOME extensions from extensions.gnome.org and installing the extensions as a package instead
2024-07-20 21:23:38 -04:00
set -eu
2024-07-02 19:48:15 -04:00
2024-05-17 01:08:26 -04:00
output( ) {
2024-06-26 01:24:45 -04:00
printf '\e[1;34m%-6s\e[m\n' " ${ @ } "
2024-05-17 01:08:26 -04:00
}
unpriv( ) {
sudo -u nobody " $@ "
}
2024-05-29 17:52:09 -04:00
virtualization = $( systemd-detect-virt)
2024-05-17 01:08:26 -04:00
# Increase compression level
2024-05-31 00:57:03 -04:00
sudo sed -i 's/zstd:1/zstd/g' /etc/fstab
2024-05-17 01:08:26 -04:00
# Compliance
sudo systemctl mask debug-shell.service
sudo systemctl mask kdump.service
# Setting umask to 077
umask 077
2024-05-29 15:27:45 -04:00
sudo sed -i 's/^UMASK.*/UMASK 077/g' /etc/login.defs
sudo sed -i 's/^HOME_MODE/#HOME_MODE/g' /etc/login.defs
2024-05-17 01:08:26 -04:00
sudo sed -i 's/umask 022/umask 077/g' /etc/bashrc
# Make home directory private
sudo chmod 700 /home/*
# Setup NTS
sudo rm -rf /etc/chrony.conf
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-07-08 14:10:27 -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:42:12 -04:00
sudo chmod 544 /etc/sysconfig/chronyd
2024-05-17 01:08:26 -04:00
sudo systemctl restart chronyd
# 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-06-06 00:42:12 -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-07-02 18:14:43 -04:00
sudo chmod 644 /etc/ssh/ssh_config.d/10-custom.conf
2024-05-17 18:39:05 -04:00
sudo mkdir -p /etc/systemd/system/sshd.service.d/
2024-07-26 07:59:01 -04:00
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-06-06 00:42:12 -04:00
sudo chmod 644 /etc/systemd/system/sshd.service.d/override.conf
2024-05-17 18:34:33 -04:00
sudo systemctl daemon-reload
sudo systemctl restart sshd
2024-05-17 01:08:26 -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 07:23:30 -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 07:23:30 -04:00
sudo chmod 644 /etc/sysctl.d/99-server.conf
2024-05-17 01:08:26 -04:00
sudo dracut -f
sudo sysctl -p
2024-05-19 21:26:54 -04:00
if [ -d /usr/lib/systemd/boot/efi ] ; then
2024-05-27 13:22:47 -04:00
sudo sed -i 's/quiet root/quiet 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 root/g' /etc/kernel/cmdline
2024-05-27 13:40:07 -04:00
sudo dnf reinstall -y kernel-core
2024-05-17 01:08:26 -04:00
else
2024-05-27 13:22:47 -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-17 01:08:26 -04:00
fi
# Disable coredump
2024-06-06 00:42:12 -04:00
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:42:12 -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-06-06 00:42:12 -04:00
sudo chmod 755 /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:42:12 -04:00
sudo chmod 644 /etc/systemd/coredump.conf.d/disable.conf
2024-05-17 01:08:26 -04:00
# Setup ZRAM
2024-07-03 05:23:28 -04:00
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/systemd/zram-generator.conf | sudo tee /etc/systemd/zram-generator.conf > /dev/null
2024-06-06 00:42:12 -04:00
sudo chmod 644 /etc/systemd/zram-generator.conf
2024-05-17 01:08:26 -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:42:12 -04:00
sudo chmod 644 /etc/dnf/dnf.conf
2024-05-17 01:08:26 -04:00
sudo sed -i 's/^metalink=.*/&\&protocol=https/g' /etc/yum.repos.d/*
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-17 16:23:25 -04:00
# Remove unnecessary packages
sudo dnf remove -y cockpit*
2024-06-04 07:23:30 -04:00
# Install hardened_malloc
sudo dnf copr enable secureblue/hardened_malloc -y
sudo dnf install -y hardened_malloc
2024-06-07 00:58:28 -04:00
echo 'libhardened_malloc.so' | sudo tee /etc/ld.so.preload
2024-06-07 00:59:38 -04:00
sudo chmod 644 /etc/ld.so.preload
2024-06-04 07:23:30 -04:00
2024-05-29 17:52:09 -04:00
# Install appropriate virtualization drivers
if [ " $virtualization " = 'kvm' ] ; then
sudo dnf install -y qemu-guest-agent
fi
2024-05-29 19:45:12 -04:00
# Setup unbound
sudo dnf install unbound -y
2024-07-03 05:23:28 -04:00
unpriv curl -s https://raw.githubusercontent.com/TommyTran732/Fedora-CoreOS-Ignition/main/etc/unbound/unbound.conf | sudo tee /etc/unbound/unbound.conf > /dev/null
2024-06-24 21:22:42 -04:00
sudo sed -i 's; ip-transparent: yes;# ip-transparent: yes;g' /etc/unbound/unbound.conf
sudo sed -i 's; interface: 127.0.0.1;# interface: 127.0.0.1;g' /etc/unbound/unbound.conf
sudo sed -i 's; interface: ::1;# interface: ::1;g' /etc/unbound/unbound.conf
sudo sed -i 's; interface: 242.242.0.1;# interface: 242.242.0.1;g' /etc/unbound/unbound.conf
sudo sed -i 's; access-control: 242.242.0.0/16 allow;# access-control: 242.242.0.0/16 allow;g' /etc/unbound/unbound.conf
2024-06-06 00:42:12 -04:00
sudo chmod 644 /etc/unbound/unbound.conf
2024-05-29 19:45:12 -04:00
sudo mkdir /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:42:12 -04:00
sudo chmod 644 /etc/systemd/system/unbound.service.d/override.conf
2024-05-29 19:45:12 -04:00
sudo systemctl enable --now unbound
2024-05-29 21:06:18 -04:00
sudo systemctl disable systemd-resolved
2024-05-29 19:45:12 -04:00
2024-06-09 08:34:13 -04:00
### Differentiating bare metal and virtual installs
# Enable auto TRIM
sudo systemctl enable fstrim.timer
2024-05-17 01:08:26 -04:00
# Setup fwupd
2024-05-29 17:52:09 -04:00
if [ " $virtualization " = 'none' ] ; then
2024-05-29 18:21:11 -04:00
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:42:12 -04:00
sudo chmod 644 /etc/systemd/system/fwupd-refresh.service.d/override.conf
2024-05-29 18:21:11 -04:00
sudo systemctl daemon-reload
sudo systemctl enable --now fwupd-refresh.timer
2024-05-29 17:52:09 -04:00
fi
2024-05-17 01:08:26 -04:00
2024-05-29 17:52:09 -04:00
# Setup tuned
sudo dnf install -y tuned
2024-05-17 18:22:28 -04:00
sudo systemctl enable --now tuned
2024-05-17 01:08:26 -04:00
2024-05-29 17:52:09 -04:00
if [ " $virtualization " = 'none' ] ; then
2024-05-17 01:08:26 -04:00
sudo tuned-adm profile latency-performance
else
sudo tuned-adm profile virtual-guest
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-17 07:12:35 -04:00
sudo firewall-cmd --permanent --remove-service= cockpit
2024-05-17 01:08:26 -04:00
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:42:12 -04:00
sudo chmod 644 /etc/systemd/system/NetworkManager.service.d/99-brace.conf
2024-05-17 01:08:26 -04:00
sudo systemctl daemon-reload
2024-05-17 02:28:26 -04:00
sudo systemctl restart NetworkManager
2024-05-29 20:49:12 -04:00
# irqbalance hardening
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:42:12 -04:00
sudo chmod 644 /etc/systemd/system/irqbalance.service.d/99-brace.conf
2024-05-29 20:49:12 -04:00
sudo systemctl daemon-reload
sudo systemctl restart irqbalance
2024-05-17 02:28:26 -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-07-08 14:10:27 -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:42:12 -04:00
sudo chmod 644 /etc/issue.net
2024-05-29 21:16:17 -04:00
# Final notes to the user
2024-07-08 14:10:27 -04:00
output 'Server setup complete. To use unbound for DNS, you need to reboot.'