Skip to content

Commit f759b39

Browse files
committed
Added NTP configuration
1 parent a82bf4f commit f759b39

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

setup.sh

+2
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ addSSHKey "${username}" "${sshKey}"
4040
changeSSHConfig
4141
setupUfw
4242
setupSwap
43+
setTimezone "Asia/Singapore"
44+
configureNTP
4345

4446
sudo service ssh restart

setupLibrary.sh

+39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/bin/bash
22

3+
# Add the new user account
4+
# Arguments:
5+
# Account Username
6+
# Account Password
7+
# Flag to determine if user account is added silently. (With / Without GECOS prompt)
38
function addUserAccount() {
49
local username=${1}
510
local password=${2}
@@ -15,6 +20,10 @@ function addUserAccount() {
1520
sudo usermod -aG sudo ${username}
1621
}
1722

23+
# Add the local machine public SSH Key for the new user account
24+
# Arguments:
25+
# Account Username
26+
# Public SSH Key
1827
function addSSHKey() {
1928
local username=${1}
2029
local sshKey=${2}
@@ -24,23 +33,30 @@ function addSSHKey() {
2433
execAsUser "${username}" "chmod 600 ~/.ssh/authorized_keys"
2534
}
2635

36+
# Execute a command as a certain user
37+
# Arguments:
38+
# Account Username
39+
# Command to be executed
2740
function execAsUser() {
2841
local username=${1}
2942
local exec_command=${2}
3043

3144
sudo -u "${username}" -H sh -c "${exec_command}"
3245
}
3346

47+
# Modify the sshd_config file
3448
function changeSSHConfig() {
3549
sudo sed -re 's/^(\#?)(PasswordAuthentication)([[:space:]]+)yes/\2\3no/' -i.$(echo 'old') /etc/ssh/sshd_config
3650
sudo sed -re 's/^(\#?)(PermitRootLogin)([[:space:]]+)(.*)/PermitRootLogin no/' -i /etc/ssh/sshd_config
3751
}
3852

53+
# Setup the Uncomplicated Firewall
3954
function setupUfw() {
4055
sudo ufw allow OpenSSH
4156
yes y | sudo ufw enable
4257
}
4358

59+
# Create the swap file based on amount of physical memory on machine (Maximum size of swap is 4GB)
4460
function createSwap() {
4561
local swapmem=$(($(getPhysicalMemory) * 2))
4662

@@ -55,11 +71,16 @@ function createSwap() {
5571
sudo swapon /swapfile
5672
}
5773

74+
# Mount the swapfile
5875
function mountSwap() {
5976
sudo cp /etc/fstab /etc/fstab.bak
6077
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
6178
}
6279

80+
# Modify the swapfile settings
81+
# Arguments:
82+
# new vm.swappiness value
83+
# new vm.vfs_cache_pressure value
6384
function tweakSwapSettings() {
6485
local swappiness=${1}
6586
local vfs_cache_pressure=${2}
@@ -68,11 +89,29 @@ function tweakSwapSettings() {
6889
sudo sysctl vm.vfs_cache_pressure=${vfs_cache_pressure}
6990
}
7091

92+
# Save the modified swap settings
7193
function saveSwapSettings() {
7294
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
7395
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
7496
}
7597

98+
# Set the machine's timezone
99+
# Arguments:
100+
# tz data timezone
101+
function setTimezone() {
102+
local timezone=${1}
103+
echo "${1}" | sudo tee /etc/timezone
104+
sudo ln -fs "/usr/share/zoneinfo/${timezone}" /etc/localtime # https://bugs.launchpad.net/ubuntu/+source/tzdata/+bug/1554806
105+
sudo dpkg-reconfigure -f noninteractive tzdata
106+
}
107+
108+
# Configure Network Time Protocol
109+
function configureNTP() {
110+
sudo apt-get update
111+
sudo apt-get --assume-yes install ntp
112+
}
113+
114+
# Gets the amount of physical memory in GB (rounded up) installed on the machine
76115
function getPhysicalMemory() {
77116
local phymem=$(free -g|awk '/^Mem:/{print $2}')
78117
if [[ ${phymem} == '0' ]]; then

tests/tests.sh

+19-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,27 @@ function testSwapSettings() {
6969

7070
tweakSwapSettings 10 50
7171

72-
assertEquals "$(cat /proc/sys/vm/swappiness)" "10"
73-
assertEquals "$(cat /proc/sys/vm/vfs_cache_pressure)" "50"
72+
assertEquals "10" "$(cat /proc/sys/vm/swappiness)"
73+
assertEquals "50" "$(cat /proc/sys/vm/vfs_cache_pressure)"
7474

7575
tweakSwapSettings "${swappiness}" "${cache_pressure}"
7676
}
7777

78+
function testTimezone() {
79+
local timezone="$(cat /etc/timezone)"
80+
81+
setTimezone "America/New_York"
82+
assertEquals "America/New_York" "$(cat /etc/timezone)"
83+
setTimezone "${timezone}"
84+
}
85+
86+
function testNTP() {
87+
local timedatectl="$(timedatectl status)"
88+
89+
configureNTP
90+
assertContains "NTP synchronized: yes" "${timedatectl}"
91+
}
92+
7893
function testTeardown () {
7994
echo "Test Teardown"
8095

@@ -83,6 +98,8 @@ function testTeardown () {
8398
revertSSHConfig
8499
revertUfw
85100
deleteSwap
101+
102+
sudo apt-get --purge --assume-yes autoremove ntp
86103
}
87104

88105
### Helper Functions ###

0 commit comments

Comments
 (0)