-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-pi.sh
executable file
·176 lines (143 loc) · 3.95 KB
/
setup-pi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# ################################################################
#
# Setup Script for a Raspberry Pi in my Cluster at home
#
# ################################################################
#
# After the execution a couple of manual steps are required:
#
# * Add public key authentication with ssh-copy-id
# * Add Wireguard configuration /etc/wireguard/wg0.conf
#
# ################################################################
# ################################
# Pre work
# ################################
do_help() {
echo "Raspberry Pi Setup Script"
echo ""
echo "Usage: $0 HOSTNAME USERNAME"
echo ""
echo "Script is supposed to minimize the setup time of a Raspberry Pi"
echo "Applied configurations meet the demands of the developers needs"
echo ""
echo " -h, --help display this help and exit"
echo ""
echo "Examples"
echo " $0 stack00 max"
echo ""
echo "Written by Max Resing, <https://www.maxresing.de>"
echo "Questions or feedback appreciated."
}
if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]] ; then
do_help
exit 0
fi
if [[ -z "$2" ]] ; then
do_help
exit 0
fi
#
# Initialize variables
#
echo "Initialize variables"
HOST=${1:-stack00}
USER=${2:-max}
# ################################
# Main setup
# ################################
#
# Update package list
#
echo "Update system"
apt-get -y update
apt-get -y dist-upgrade
apt-get -y autoremove
#
# Setup hostname
#
echo "Change hostname to $HOST"
echo "$HOST" > /etc/hostname
#
# Setup user
#
echo "Setup user $USER"
useradd --create-home --shell /bin/bash "$USER"
usermod -aG sudo "$USER"
echo "Setup password for $USER."
passwd "$USER"
#
# Raspi-config
#
echo "Raspi-config -> Boot Options -> Console"
raspi-config nonint do_boot_behaviour B1
echo "Raspi-config -> Expand File System"
raspi-config nonint do_expand_rootfs
#
# Setup SSH
#
echo "Setup SSH configuration"
echo "" >> /etc/ssh/sshd_config
echo "# Setup script" >> /etc/ssh/sshd_config
echo "AllowUsers $USER" >> /etc/ssh/sshd_config
#echo "PermitRootLogin prohibit-password" >> /etc/ssh/sshd_config
#echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config
#
# Install general set of packages
#
echo "Install packages"
apt-get -y update
apt-get -y install vim
apt-get -y install libelf-dev raspberrypi-kernel-headers build-essential pkg-config git
apt-get -y install openvpn
apt-get -y install unattended-upgrades apt-listchanges
apt-get -y install nfs-common
# Load openvpn connection
# (expects a configuration file on reboot: /etc/openvpn/clients/maxresing.conf
systemctl enable [email protected]
#
# Setup unattended upgrades
#
echo "Setup unattended upgrades"
#printf "APT::Periodic::Update-Package-Lists \"1\";\nAPT::Periodic::Unattended-Upgrade \"1\";" > /etc/apt/apt.conf.d/20auto-upgrades
echo unattended-upgrades unattended-upgrades/enable_auto_updates boolean true | debconf-set-selections
dpkg-reconfigure -f noninteractive unattended-upgrades
#
# Setup nfs
#
# TODO: Ensure that NFS server does not mount it's own shared NFS dirs
echo "Create and mount NFS"
mkdir -p /mnt/data
echo "" >> /etc/fstab
echo "# setup-pi script" >> /etc/fstab
echo "10.10.10.16:/mnt/data/share /mnt/data nfs rw,soft,retrans=4,timeo=15,intr 0 0" >> /etc/fstab
echo "" >> /etc/fstab
mount /mnt/data
#
# .bashrc creation for $USER and podman if exists
#
echo "Create symlinks for .bashrc"
# Global bashrc
rm /etc/bash.bashrc
ln -s /mnt/data/all/bashrc/etc/bash.bashrc /etc/bash.bashrc
# $USER bashrc
rm /home/"$USER"/.bashrc
ln -s /mnt/data/all/bashrc/bashrc /home/"$USER"/.bashrc
# If podman user exists, also replace their .bashrc
if id "podman" &>/dev/null; then
rm /home/podman/.bashrc
ln -s /mnt/data/all/bashrc/bashrc /home/podman/.bashrc
fi
# ################################
# Post work
# ################################
#
# Delete user 'pi'
#
echo "Delete insecure user accounts"
userdel -f pi
rm -R /home/pi/
# Reboot
echo "Rebooting"
reboot