2015-08-04 16:29:49 -07:00
#!/bin/bash
2022-03-29 22:11:43 -07:00
# This script is run on the base ubuntu. Put things here which are managed by ubuntu
2022-07-12 16:14:40 +05:30
# This script is also run after ubuntu upgrade
2022-03-29 22:11:43 -07:00
2015-08-04 16:29:49 -07:00
set -euv -o pipefail
2015-08-12 19:52:43 -07:00
readonly SOURCE_DIR = " $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd ) "
2015-12-23 13:27:33 +01:00
2020-06-16 14:09:55 +02:00
readonly arg_infraversionpath = " ${ SOURCE_DIR } /../src "
2017-01-09 09:22:22 -08:00
2016-01-22 10:33:34 -08:00
function die {
echo $1
exit 1
}
2015-08-04 16:29:49 -07:00
export DEBIAN_FRONTEND = noninteractive
2020-06-26 14:52:52 -07:00
readonly ubuntu_codename = $( lsb_release -cs)
2020-07-16 16:42:15 +02:00
readonly ubuntu_version = $( lsb_release -rs)
2020-06-26 14:52:52 -07:00
2018-09-06 09:16:11 -07:00
# hold grub since updating it breaks on some VPS providers. also, dist-upgrade will trigger it
2018-08-28 14:01:47 -07:00
apt-mark hold grub* >/dev/null
2016-11-08 15:35:51 +05:30
apt-get -o Dpkg::Options::= "--force-confdef" update -y
2018-09-06 09:16:11 -07:00
apt-get -o Dpkg::Options::= "--force-confdef" upgrade -y
2018-08-28 14:01:47 -07:00
apt-mark unhold grub* >/dev/null
2015-08-26 09:23:30 -07:00
2024-05-04 17:36:26 +02:00
vendor = $( cat /sys/devices/virtual/dmi/id/sys_vendor || true )
if [ [ " ${ vendor } " = = "netcup" && ! -L /etc/resolv.conf ] ] ; then
echo "==> Fix netcup DNS setup"
nameservers = $( sed -ne 's/nameserver \(.*\)/"\1"/p' /etc/resolv.conf | paste -sd "," -) # json array
if lsattr -l /etc/resolv.conf 2>/dev/null | grep -q Immutable; then
chattr -i /etc/resolv.conf # this code is also in cloudron-setup. that code can be removed much after 8.0
fi
ln -frs /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
netplan set --origin-hint 50-cloud-init " ethernets.eth0.nameservers.addresses=[ ${ nameservers } ] "
netplan apply # generates /run/systemd/resolve/resolv.conf
systemctl restart systemd-resolved
2024-01-31 17:24:29 +01:00
fi
2016-12-27 14:12:31 -08:00
echo "==> Installing required packages"
debconf-set-selections <<< 'mysql-server mysql-server/root_password password password'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password password'
2017-01-11 22:22:17 -08:00
# this enables automatic security upgrades (https://help.ubuntu.com/community/AutomaticSecurityUpdates)
2022-04-21 11:07:43 -07:00
case " ${ ubuntu_version } " in
16.04)
gpg_package = "gnupg"
mysql_package = "mysql-server-5.7"
ntpd_package = ""
python_package = "python2.7"
2022-04-27 10:22:31 -07:00
nginx_package = "" # we use custom package for TLS v1.3 support
2022-04-21 11:07:43 -07:00
; ;
18.04)
gpg_package = "gpg"
mysql_package = "mysql-server-5.7"
ntpd_package = ""
python_package = "python2.7"
2022-04-27 10:22:31 -07:00
nginx_package = "" # we use custom package for TLS v1.3 support
2022-04-21 11:07:43 -07:00
; ;
20.04)
gpg_package = "gpg"
mysql_package = "mysql-server-8.0"
ntpd_package = "systemd-timesyncd"
python_package = "python3.8"
2022-04-27 10:22:31 -07:00
nginx_package = "nginx-full"
2022-04-21 11:07:43 -07:00
; ;
22.04)
gpg_package = "gpg"
mysql_package = "mysql-server-8.0"
ntpd_package = "systemd-timesyncd"
python_package = "python3.10"
2022-04-27 10:22:31 -07:00
nginx_package = "nginx-full"
2024-04-28 11:31:39 +02:00
; ;
24.04)
gpg_package = "gpg"
mysql_package = "mysql-server-8.0"
ntpd_package = "systemd-timesyncd"
python_package = "python3.12"
nginx_package = "nginx-full"
2022-04-21 11:07:43 -07:00
; ;
esac
2020-11-25 10:02:43 -08:00
2021-01-04 23:30:41 -08:00
apt-get -y install --no-install-recommends \
2016-12-27 14:12:31 -08:00
acl \
2021-01-10 20:00:49 +01:00
apparmor \
2016-12-27 14:12:31 -08:00
build-essential \
2019-09-23 12:21:34 +02:00
cifs-utils \
2016-12-27 14:12:31 -08:00
cron \
curl \
2019-03-27 14:16:53 -07:00
debconf-utils \
2017-02-06 11:32:08 -08:00
dmsetup \
2019-01-11 09:43:27 -08:00
$gpg_package \
2020-08-31 18:22:33 -07:00
ipset \
2016-12-27 14:12:31 -08:00
iptables \
2022-04-21 11:07:43 -07:00
lib${ python_package } \
2019-09-23 12:21:34 +02:00
linux-generic \
2016-12-27 14:12:31 -08:00
logrotate \
2020-09-15 21:53:16 -07:00
$mysql_package \
2021-05-12 18:00:43 -07:00
nfs-common \
2022-04-27 13:11:20 -07:00
$nginx_package \
2021-08-22 17:22:47 +02:00
$ntpd_package \
2016-12-27 14:12:31 -08:00
openssh-server \
2023-04-21 20:25:18 +02:00
python3-magic \
2016-12-27 14:12:31 -08:00
pwgen \
2021-06-18 14:46:54 -07:00
sshfs \
2016-12-27 14:12:31 -08:00
swaks \
2019-06-06 12:42:06 -07:00
tzdata \
2017-01-11 22:22:17 -08:00
unattended-upgrades \
2017-08-13 23:15:23 -07:00
unbound \
2024-04-28 11:18:37 +02:00
unbound-anchor \
2021-01-03 15:09:58 -08:00
unzip \
2023-12-14 17:05:22 +01:00
whois \
2017-08-13 23:15:23 -07:00
xfsprogs
2016-12-27 14:12:31 -08:00
2019-06-11 13:30:15 +02:00
# on some providers like scaleway the sudo file is changed and we want to keep the old one
2021-01-04 23:30:41 -08:00
apt-get -o Dpkg::Options::= "--force-confold" install -y --no-install-recommends sudo
2019-06-11 13:30:15 +02:00
2017-07-28 19:48:56 -07:00
# this ensures that unattended upgades are enabled, if it was disabled during ubuntu install time (see #346)
# debconf-set-selection of unattended-upgrades/enable_auto_updates + dpkg-reconfigure does not work
2023-04-17 09:40:31 +02:00
# logs of upgrades are at /var/log/apt/history.log and /var/log/unattended-upgrades/unattended-upgrades-dpkg.log
2024-07-25 17:09:46 +02:00
# apt-daily-upgrade.service (timer) runs the unattended-upgrades script depending on APT::Periodic::Unattended-Upgrade
2022-12-24 11:54:30 +01:00
echo "==> Enabling automatic upgrades"
2017-07-28 19:48:56 -07:00
cp /usr/share/unattended-upgrades/20auto-upgrades /etc/apt/apt.conf.d/20auto-upgrades
2022-12-24 11:54:30 +01:00
echo " ==> Ensuring python $python_package "
2022-04-21 11:07:43 -07:00
apt-get install -y --no-install-recommends $python_package # Install python which is required for npm rebuild
2015-08-04 16:29:49 -07:00
2018-09-06 09:16:11 -07:00
# do not upgrade grub because it might prompt user and break this script
echo "==> Enable memory accounting"
2021-01-04 23:30:41 -08:00
apt-get -y --no-upgrade --no-install-recommends install grub2-common
2018-09-06 09:16:11 -07:00
sed -e 's/^GRUB_CMDLINE_LINUX="\(.*\)"$/GRUB_CMDLINE_LINUX="\1 cgroup_enable=memory swapaccount=1 panic_on_oops=1 panic=5"/' -i /etc/default/grub
update-grub
2017-01-12 00:57:19 -08:00
2021-01-04 17:26:21 -08:00
# some hosts like atlantic install ntp which conflicts with timedatectl. https://serverfault.com/questions/1024770/ubuntu-20-04-time-sync-problems-and-possibly-incorrect-status-information
2018-10-26 10:57:19 -07:00
echo "==> Configuring host"
sed -e 's/^#NTP=/NTP=0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org 3.ubuntu.pool.ntp.org/' -i /etc/systemd/timesyncd.conf
2021-01-04 17:26:21 -08:00
if systemctl is-active ntp; then
systemctl stop ntp
apt purge -y ntp
fi
2018-10-26 10:57:19 -07:00
timedatectl set-ntp 1
2019-03-22 15:12:30 -07:00
# mysql follows the system timezone
2018-10-26 10:57:19 -07:00
timedatectl set-timezone UTC
2019-12-13 11:32:36 -08:00
echo "==> Adding sshd configuration warning"
2023-11-24 15:46:24 +01:00
sed -e '/Port 22/ i # NOTE: Read https://docs.cloudron.io/security/#securing-ssh-access before changing this' -i /etc/ssh/sshd_config
2019-12-13 11:32:36 -08:00
2020-01-05 15:25:13 -08:00
# https://bugs.launchpad.net/ubuntu/+source/base-files/+bug/1701068
echo "==> Disabling motd news"
2022-05-25 15:10:04 -07:00
if [ [ -f "/etc/default/motd-news" ] ] ; then
2020-09-04 10:49:07 +02:00
sed -i 's/^ENABLED=.*/ENABLED=0/' /etc/default/motd-news
fi
2020-01-05 15:25:13 -08:00
2022-02-09 12:17:42 -08:00
# If privacy extensions are not disabled on server, this breaks IPv6 detection
# https://bugs.launchpad.net/ubuntu/+source/procps/+bug/1068756
if [ [ ! -f /etc/sysctl.d/99-cloudimg-ipv6.conf ] ] ; then
echo "==> Disable temporary address (IPv6)"
echo -e "# See https://bugs.launchpad.net/ubuntu/+source/procps/+bug/1068756\nnet.ipv6.conf.all.use_tempaddr = 0\nnet.ipv6.conf.default.use_tempaddr = 0\n\n" > /etc/sysctl.d/99-cloudimg-ipv6.conf
fi
2021-12-07 09:42:25 -08:00
# Disable exim4 (1blu.de)
systemctl stop exim4 || true
systemctl disable exim4 || true
2020-11-22 23:51:21 -08:00
# Disable bind for good measure (on online.net, kimsufi servers these are pre-installed)
2017-03-25 17:36:08 -07:00
systemctl stop bind9 || true
systemctl disable bind9 || true
2017-05-16 16:33:43 -07:00
2020-11-25 10:02:43 -08:00
# on ovh images dnsmasq seems to run by default
systemctl stop dnsmasq || true
systemctl disable dnsmasq || true
2018-02-07 09:07:49 -08:00
# on ssdnodes postfix seems to run by default
systemctl stop postfix || true
systemctl disable postfix || true
2022-02-09 19:50:51 -08:00
# on vultr, ufw is enabled by default. we have our own firewall
2022-03-02 19:36:20 -08:00
ufw disable || true
2022-02-09 19:50:51 -08:00
2024-06-27 14:34:37 +02:00
# nfs-common depends on rpcbind which is only needed for NFS v2/v3 . systemctl list-sockets | grep 111
systemctl disable rpcbind.socket rpcbind.service || true
systemctl stop rpcbind.socket rpcbind.service || true
2022-04-27 17:49:29 -07:00
# Ubuntu 22 has private home directories by default (https://discourse.ubuntu.com/t/private-home-directories-for-ubuntu-21-04-onwards/)
sed -e 's/^HOME_MODE\([[:space:]]\+\).*$/HOME_MODE\10755/' -i /etc/login.defs
2022-03-30 14:27:39 -07:00
# create the yellowtent user. system user has different numeric range, no age and won't show in login/gdm UI
2023-08-08 23:17:11 +05:30
# the nologin will also disable su/login. hardcoding uid helps in restoring
2022-03-29 21:41:46 -07:00
if ! id yellowtent 2>/dev/null; then
2023-08-08 23:17:11 +05:30
useradd --system --uid 808 --comment "Cloudron Box" --create-home --shell /usr/sbin/nologin yellowtent
2022-03-30 14:27:39 -07:00
fi
# add support user (no password, sudo)
if ! id cloudron-support 2>/dev/null; then
useradd --system --comment "Cloudron Support (support@cloudron.io)" --create-home --no-user-group --shell /bin/bash cloudron-support
2022-03-29 21:41:46 -07:00
fi