2023-06-06 01:54:42 -04:00
#!/bin/bash
2024-01-20 03:09:48 -05:00
# Copyright (C) 2021-2024 Thien Tran
2023-09-27 03:56:28 -04:00
#
# 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.
2023-06-06 01:54:42 -04:00
#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
2023-12-01 21:54:20 -05:00
output( ) {
2023-12-04 18:17:55 -05:00
echo -e '\e[36m' " $1 " '\e[0m' ;
2023-12-01 21:54:20 -05:00
}
2023-12-02 06:56:58 -05:00
unpriv( ) {
sudo -u nobody " $@ "
}
2024-01-20 01:48:59 -05:00
# Compliance and updates
2023-12-12 03:20:52 -05:00
sudo systemctl mask debug-shell.service
2024-01-24 18:48:16 -05:00
## Avoid phased updates
sudo apt install curl -y
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/apt/apt.conf.d/99sane-upgrades | sudo tee /etc/apt/apt.conf.d/99sane-upgrades
sudo chmod 644 /etc/apt/apt.conf.d/99sane-upgrades
2023-08-22 20:46:56 -04:00
sudo apt update -y
sudo apt full-upgrade -y
sudo apt autoremove -y
2023-06-06 18:04:04 -04:00
2023-12-07 19:26:53 -05:00
# Make home directory private
2023-12-31 16:40:36 -05:00
sudo chmod 700 /home/*
2023-12-07 19:26:53 -05:00
2023-08-16 06:22:28 -04:00
# Setting umask to 077
2023-06-06 01:54:42 -04:00
umask 077
sudo sed -ie '/^DIR_MODE=/ s/=[0-9]*\+/=0700/' /etc/adduser.conf
sudo sed -ie '/^UMASK\s\+/ s/022/077/' /etc/login.defs
sudo sed -i 's/USERGROUPS_ENAB yes/USERGROUPS_ENAB no/g' /etc/login.defs
2023-12-06 15:47:53 -05:00
echo 'umask 077' | sudo tee --append /etc/profile
2023-06-06 01:54:42 -04:00
2023-08-16 06:22:28 -04:00
# Setup NTS
sudo systemctl disable systemd-timesyncd
2024-01-24 18:48:16 -05:00
sudo apt install -y chrony
2023-12-02 06:56:58 -05:00
unpriv curl https://raw.githubusercontent.com/GrapheneOS/infrastructure/main/chrony.conf | sudo tee /etc/chrony/chrony.conf
2023-08-16 06:22:28 -04:00
sudo systemctl restart chronyd
2024-01-20 07:52:02 -05:00
# Setup Networking
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/NetworkManager/conf.d/00-macrandomize.conf | sudo tee /etc/NetworkManager/conf.d/00-macrandomize.conf
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/NetworkManager/conf.d/01-transient-hostname.conf | sudo tee /etc/NetworkManager/conf.d/01-transient-hostname.conf
sudo nmcli general reload conf
sudo hostnamectl hostname 'localhost'
sudo hostnamectl --transient hostname ''
2023-08-16 06:22:28 -04:00
# Setup UFW
#UFW Snap is strictly confined, unlike its .deb counterpart
sudo apt purge -y ufw
sudo snap install ufw
sudo ufw enable
# Harden SSH
2024-02-07 11:52:52 -05:00
unpriv curl 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
2023-08-16 06:22:28 -04:00
sudo chmod 644 /etc/ssh/ssh_config.d/10-custom.conf
# Kernel hardening
2023-12-02 06:56:58 -05:00
unpriv curl https://raw.githubusercontent.com/Kicksecure/security-misc/master/etc/modprobe.d/30_security-misc.conf | sudo tee /etc/modprobe.d/30_security-misc.conf
2024-01-03 19:39:59 -05:00
sudo chmod 644 /etc/modprobe.d/30_security-misc.conf
2023-12-02 06:56:58 -05:00
unpriv curl https://raw.githubusercontent.com/Kicksecure/security-misc/master/usr/lib/sysctl.d/990-security-misc.conf | sudo tee /etc/sysctl.d/990-security-misc.conf
2024-01-03 19:39:59 -05:00
sudo chmod 644 /etc/sysctl.d/990-security-misc.conf
2023-12-02 06:56:58 -05:00
unpriv curl https://raw.githubusercontent.com/Kicksecure/security-misc/master/usr/lib/sysctl.d/30_silent-kernel-printk.conf | sudo tee /etc/sysctl.d/30_silent-kernel-printk.conf
2024-01-03 19:39:59 -05:00
sudo chmod 644 /etc/sysctl.d/30_silent-kernel-printk.conf
2023-12-02 06:56:58 -05:00
unpriv curl https://raw.githubusercontent.com/Kicksecure/security-misc/master/usr/lib/sysctl.d/30_security-misc_kexec-disable.conf | sudo tee /etc/sysctl.d/30_security-misc_kexec-disable.conf
2024-01-03 19:39:59 -05:00
sudo chmod 644 /etc/sysctl.d/30_security-misc_kexec-disable.conf
2023-11-15 16:12:25 -05:00
sudo sed -i 's/kernel.yama.ptrace_scope=2/kernel.yama.ptrace_scope=3/g' /etc/sysctl.d/990-security-misc.conf
2023-09-27 02:03:35 -04:00
sudo sysctl -p
# Rebuild initramfs
sudo update-initramfs -u
2024-02-13 19:16:33 -05:00
# Disable coredump
unpriv curl 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
2024-01-21 03:23:50 -05:00
# Update GRUB config
2024-03-25 15:41:40 -04:00
# shellcheck disable=SC2016
2024-02-26 23:37:40 -05:00
echo 'GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX mitigations=auto,nosmt spectre_v2=on spec_store_bypass_disable=on tsx=off kvm.nx_huge_pages=force nosmt=force l1d_flush=on spec_rstack_overflow=safe-ret 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"' | sudo tee -a /etc/grub.d/40_custom
2024-01-21 03:23:50 -05:00
sudo update-grub
2023-08-16 06:22:28 -04:00
# Systemd Hardening
2023-06-06 01:54:42 -04:00
sudo mkdir -p /etc/systemd/system/NetworkManager.service.d
2023-12-02 06:56:58 -05:00
unpriv curl 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
2023-06-06 01:54:42 -04:00
sudo mkdir -p /etc/systemd/system/irqbalance.service.d
2023-12-02 06:56:58 -05:00
unpriv curl 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
2023-06-06 02:36:44 -04:00
2024-01-20 01:48:59 -05:00
# Setup dconf
2024-01-20 19:06:07 -05:00
umask 022
2024-01-20 07:15:18 -05:00
sudo mkdir -p /etc/dconf/db/local.d/locks
2024-01-20 07:12:27 -05:00
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/dconf/db/local.d/automount-disable | sudo tee /etc/dconf/db/local.d/automount-disable
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/dconf/db/local.d/locks/automount-disable | sudo tee /etc/dconf/db/local.d/locks/automount-disable
2024-01-20 01:41:53 -05:00
2024-01-20 07:15:18 -05:00
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/dconf/db/local.d/apport-disable | sudo tee /etc/dconf/db/local.d/apport-disable
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/dconf/db/local.d/locks/apport-disable | sudo tee /etc/dconf/db/local.d/locks/apport-disable
2024-01-20 01:41:53 -05:00
2024-01-20 03:16:29 -05:00
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/dconf/db/local.d/touchpad | sudo tee /etc/dconf/db/local.d/touchpad
2024-01-20 19:06:07 -05:00
2023-06-06 02:57:50 -04:00
sudo dconf update
2024-01-20 19:06:07 -05:00
umask 077
2023-06-06 02:57:50 -04:00
2023-06-06 01:54:42 -04:00
ubuntu-report -f send no
sudo systemctl stop apport.service
2024-01-22 06:20:45 -05:00
sudo systemctl disable --now apport.service
2023-06-06 01:54:42 -04:00
sudo systemctl mask apport.service
2024-01-22 06:20:45 -05:00
sudo systemctl stop --now whoopsie.service
2023-06-06 01:54:42 -04:00
sudo systemctl disable whoopsie.service
sudo systemctl mask whoopsie.service
2024-01-22 06:20:45 -05:00
sudo systemctl disable whoopsie.path
sudo systemctl mask whoopsie.path
2023-06-06 01:54:42 -04:00
2023-08-16 06:22:28 -04:00
# Update packages and firmware
2023-06-06 02:00:57 -04:00
sudo apt update -y
sudo apt full-upgrade -y
sudo fwupdmgr get-devices
sudo fwupdmgr refresh --force
sudo fwupdmgr get-updates -y
sudo fwupdmgr update -y
2023-08-16 06:22:28 -04:00
# Remove unneeded packages
2024-01-21 01:29:30 -05:00
sudo apt purge -y apport baobab cups* eog gedit firefox* gnome-calculator gnome-characters* gnome-clocks gnome-font-viewer gnome-logs gnome-power-manager gnome-shell-extension-prefs gnome-text-editor libreoffice* seahorse tcpdump whoopsie
2023-06-06 05:13:25 -04:00
sudo apt autoremove -y
2024-01-21 01:29:30 -05:00
sudo snap remove firefox snap-store
2023-06-06 01:54:42 -04:00
2024-01-20 06:14:21 -05:00
sudo rm -rf /usr/share/hplip
2023-08-16 06:22:28 -04:00
# Install packages that I use
2024-01-21 01:29:30 -05:00
sudo apt install -y gnome-console gnome-software-plugin-flatpak
2024-01-21 01:33:06 -05:00
sudo snap install gnome-text-editor
2023-06-06 02:06:15 -04:00
2024-01-21 01:29:30 -05:00
# Setup Flatpak
2024-04-02 19:39:13 -04:00
sudo flatpak override --system --nosocket= x11 --nosocket= fallback-x11 --nosocket= pulseaudio --unshare= network --unshare= ipc --nofilesystem= host:reset --nodevice= shm --nodevice= all --no-talk-name= org.freedesktop.Flatpak
flatpak override --user --nosocket= x11 --nosocket= fallback-x11 --nosocket= pulseaudio --unshare= network --unshare= ipc --nofilesystem= host:reset --nodevice= shm --nodevice= all --no-talk-name= org.freedesktop.Flatpak
2024-01-21 01:29:30 -05:00
flatpak remote-add --if-not-exists --user flathub https://dl.flathub.org/repo/flathub.flatpakrepo
flatpak --user install org.gnome.Extensions com.github.tchx84.Flatseal org.gnome.Loupe -y
flatpak --user override com.github.tchx84.Flatseal --filesystem= /var/lib/flatpak/app:ro --filesystem= xdg-data/flatpak/app:ro --filesystem= xdg-data/flatpak/overrides:create
flatpak update -y
2024-01-20 08:01:41 -05:00
# Rosetta setup
2024-01-22 07:12:05 -05:00
if [ -f /media/psf/RosettaLinux/rosetta ] || [ -f /media/rosetta/rosetta ] ; then
if [ -f /media/rosetta/rosetta ] ; then
sudo /usr/sbin/update-binfmts --install rosetta /media/rosetta/rosetta --magic "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00" --mask "\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff" --credentials yes --preserve no --fix-binary yes
fi
2024-01-20 09:28:52 -05:00
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/apt/rosetta-sources.list | sudo tee /etc/apt/sources.list
2024-01-20 22:38:03 -05:00
rm -rf /etc/apt/sources.list.d/proposed.list
2024-01-22 06:22:35 -05:00
sudo dpkg --add-architecture amd64
sudo apt update
sudo apt full-upgrade -y
2024-01-20 09:28:52 -05:00
fi
2024-01-20 08:01:41 -05:00
2024-01-20 07:52:02 -05:00
# Install Microsoft Edge if x86_64
MACHINE_TYPE = $( uname -m)
2024-01-22 07:12:05 -05:00
if [ " ${ MACHINE_TYPE } " = = 'x86_64' ] || [ -f /media/psf/RosettaLinux/rosetta ] || [ -f /media/rosetta/rosetta ] ; then
2024-01-20 07:52:02 -05:00
output 'x86_64 machine, installing Microsoft Edge.'
2024-01-20 10:11:44 -05:00
unpriv curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /usr/share/keyrings/microsoft.gpg
sudo chmod 644 /usr/share/keyrings/microsoft.gpg
unpriv curl https://raw.githubusercontent.com/TommyTran732/Linux-Setup-Scripts/main/etc/apt/sources.list.d/microsoft-edge.list | sudo tee /etc/apt/sources.list.d/microsoft-edge.list
sudo chmod 644 /etc/apt/sources.list.d/microsoft-edge.list
sudo apt update
sudo apt full-upgrade -y
2024-01-20 07:52:02 -05:00
sudo apt install -y microsoft-edge-stable
sudo mkdir -p /etc/opt/edge/policies/managed/ /etc/opt/edge/policies/recommended/
sudo chmod -R 755 /etc/opt/edge
unpriv curl https://raw.githubusercontent.com/TommyTran732/Microsoft-Edge-Policies/main/Linux/managed.json | sudo tee /etc/opt/edge/policies/managed/managed.json
unpriv curl https://raw.githubusercontent.com/TommyTran732/Microsoft-Edge-Policies/main/Linux/recommended.json | sudo tee /etc/opt/edge/policies/recommended/recommended.json
2024-01-20 10:15:27 -05:00
sudo chmod 644 /etc/opt/edge/policies/managed/managed.json /etc/opt/edge/policies/recommended/recommended.json
2024-01-20 07:52:02 -05:00
fi
# Enable fstrim.timer
sudo systemctl enable --now fstrim.timer
2023-07-07 05:41:36 -04:00
2024-01-20 02:04:03 -05:00
# Installing tuned first here because virt-what is 1 of its dependencies anyways
2023-08-22 20:46:56 -04:00
sudo apt install tuned -y
2024-01-20 02:04:03 -05:00
virt_type = $( virt-what)
if [ " $virt_type " = '' ] ; then
output 'Virtualization: Bare Metal.'
elif [ " $virt_type " = 'openvz lxc' ] ; then
output 'Virtualization: OpenVZ 7.'
elif [ " $virt_type " = 'xen xen-hvm' ] ; then
output 'Virtualization: Xen-HVM.'
elif [ " $virt_type " = 'xen xen-hvm aws' ] ; then
output 'Virtualization: Xen-HVM on AWS.'
else
output " Virtualization: $virt_type . "
fi
# Setup tuned
if [ " $virt_type " = '' ] ; then
2024-01-22 10:55:42 -05:00
# Don't know whether using tuned would be a good idea on a laptop, power-profiles-daemon should be handling performance tuning IMO.
sudo apt remove tuned -y
sudo apt autoremove -y
2024-01-20 02:04:03 -05:00
else
2024-01-22 10:55:42 -05:00
if [ " $virt_type " = 'kvm' ] ; then
sudo apt install qemu-guest-agent -y
fi
sudo tuned-adm profile virtual-guest
2024-01-20 07:52:02 -05:00
fi