1
1
#! /bin/bash
2
2
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)
3
8
function addUserAccount() {
4
9
local username=${1}
5
10
local password=${2}
@@ -15,6 +20,10 @@ function addUserAccount() {
15
20
sudo usermod -aG sudo ${username}
16
21
}
17
22
23
+ # Add the local machine public SSH Key for the new user account
24
+ # Arguments:
25
+ # Account Username
26
+ # Public SSH Key
18
27
function addSSHKey() {
19
28
local username=${1}
20
29
local sshKey=${2}
@@ -24,23 +33,30 @@ function addSSHKey() {
24
33
execAsUser " ${username} " " chmod 600 ~/.ssh/authorized_keys"
25
34
}
26
35
36
+ # Execute a command as a certain user
37
+ # Arguments:
38
+ # Account Username
39
+ # Command to be executed
27
40
function execAsUser() {
28
41
local username=${1}
29
42
local exec_command=${2}
30
43
31
44
sudo -u " ${username} " -H sh -c " ${exec_command} "
32
45
}
33
46
47
+ # Modify the sshd_config file
34
48
function changeSSHConfig() {
35
49
sudo sed -re ' s/^(\#?)(PasswordAuthentication)([[:space:]]+)yes/\2\3no/' -i.$( echo ' old' ) /etc/ssh/sshd_config
36
50
sudo sed -re ' s/^(\#?)(PermitRootLogin)([[:space:]]+)(.*)/PermitRootLogin no/' -i /etc/ssh/sshd_config
37
51
}
38
52
53
+ # Setup the Uncomplicated Firewall
39
54
function setupUfw() {
40
55
sudo ufw allow OpenSSH
41
56
yes y | sudo ufw enable
42
57
}
43
58
59
+ # Create the swap file based on amount of physical memory on machine (Maximum size of swap is 4GB)
44
60
function createSwap() {
45
61
local swapmem=$(( $(getPhysicalMemory) * 2 ))
46
62
@@ -55,11 +71,16 @@ function createSwap() {
55
71
sudo swapon /swapfile
56
72
}
57
73
74
+ # Mount the swapfile
58
75
function mountSwap() {
59
76
sudo cp /etc/fstab /etc/fstab.bak
60
77
echo ' /swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
61
78
}
62
79
80
+ # Modify the swapfile settings
81
+ # Arguments:
82
+ # new vm.swappiness value
83
+ # new vm.vfs_cache_pressure value
63
84
function tweakSwapSettings() {
64
85
local swappiness=${1}
65
86
local vfs_cache_pressure=${2}
@@ -68,11 +89,29 @@ function tweakSwapSettings() {
68
89
sudo sysctl vm.vfs_cache_pressure=${vfs_cache_pressure}
69
90
}
70
91
92
+ # Save the modified swap settings
71
93
function saveSwapSettings() {
72
94
echo ' vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
73
95
echo ' vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf
74
96
}
75
97
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
76
115
function getPhysicalMemory() {
77
116
local phymem=$( free -g| awk ' /^Mem:/{print $2}' )
78
117
if [[ ${phymem} == ' 0' ]]; then
0 commit comments