This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
minimal.sh
344 lines (310 loc) · 8.27 KB
/
minimal.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
cd $(dirname $0)
###############
## Variables ##
###############
# Disable BASH History For Session
unset HISTFILE
#############################
## Configuration Functions ##
#############################
# Run Through Configuration Functions
function configure_basic {
# Configure Defaults
configure_defaults
# Remove Useless Gettys
configure_getty
# Ask If BASH History Should Be Disabled
echo -n "Do you wish to disable BASH history? (Y/n): "
read -e OPTION_HISTORY
# Check User Input
if [ "$OPTION_HISTORY" != "n" ]; then
# Execute Function
configure_history
fi
# Ask If Logging Should Be Simplified
echo -n "Simplify logging configuration? (Y/n): "
read -e OPTION_LOGGING
# Check User Input
if [ "$OPTION_LOGGING" != "n" ]; then
# Execute Function
configure_logging
fi
# Ask If SSH Port Should Be Changed
echo -n "Do you wish to run SSH on different ports? (y/N): "
read -e OPTION_SSHPORT
# Check User Input
if [ "$OPTION_SSHPORT" == "y" ]; then
# Execute Function
configure_sshport
fi
# Ask If Root SSH Should Be Disabled
echo -n "Do you wish to disable root SSH logins? Keep enabled if you don't plan on making any users! (Y/n): "
read -e OPTION_SSHROOT
# Check User Input
if [ "$OPTION_SSHROOT" != "n" ]; then
# Execute Function
configure_sshroot
fi
# Ask If Time Zone Should Be Set
echo -n "Do you wish to set the timezone? (Y/n): "
read -e OPTION_TZ
# Check User Input
if [ "$OPTION_TZ" != "n" ]; then
# Execute Function
configure_timezone
fi
# Ask If User Should Be Made
echo -n "Do you wish to create a user account? (Y/n): "
read -e OPTION_USER
# Check User Input
if [ "$OPTION_USER" != "n" ]; then
# Execute Function
configure_user
fi
# Reconfigure Dash
dpkg-reconfigure dash
# Clean Up
configure_final
}
# Clean Dotfiles
function configure_defaults {
echo \>\> Configuring: Defaults
# Remove Home Dotfiles
rm -rf ~/.??*
# Remove Skel Dotfiles
rm -rf /etc/skel/.??*
# Update Home Dotfiles
cp -a -R settings/skel/.??* ~
# Update Skel Dotfiles
cp -a -R settings/skel/.??* /etc/skel
# Append Umask
echo -e "\numask o=" >> /etc/skel/.bashrc
}
# Clean Home
function configure_final {
echo \>\> Configuring: Finalizing
# Remove All Home Files
rm -rf ~/*
}
# Clean Getty
function configure_getty {
echo \>\> Configuring: Gettys
# Remove Unneeded Getty Instances
sed -e 's/\(^[2-6].*getty.*\)/#\1/' -i /etc/inittab
}
# Disable BASH History
function configure_history {
echo \>\> Configuring: BASH History
# Disable System BASH History
echo -e "\nunset HISTFILE" >> /etc/profile
}
# Simplify Logging
function configure_logging {
echo \>\> Configuring: Simplified Logging
# Stop Logging Daemon
/etc/init.d/inetutils-syslogd stop
# Remove Log Files
rm /var/log/* /var/log/*/*
rm -rf /var/log/news
# Create New Log Files
touch /var/log/{auth,daemon,kernel,mail,messages}
# Copy Simplified Logging Configuration
cp settings/syslog /etc/syslog.conf
# Copy Simplified Log Rotation Configuration
cp settings/logrotate /etc/logrotate.d/inetutils-syslogd
# Start Logging Daemon
/etc/init.d/inetutils-syslogd start
}
# Add Additional SSH Port
function configure_sshport {
echo \>\> Configuring: Changing SSH Ports
# Take User Input
echo -n "Please enter an additional SSH Port: "
read -e SSHPORT
# Add Extra SSH Port To OpenSSH
sed -i 's/#Port/Port '$SSHPORT'/g' /etc/ssh/sshd_config
# Add Extra SSH Port To Dropbear
sed -i 's/DROPBEAR_EXTRA_ARGS="-w/DROPBEAR_EXTRA_ARGS="-w -p '$SSHPORT'/g' /etc/default/dropbear
}
# Disable Root SSH Login
function configure_sshroot {
echo \>\> Configuring: Disabling Root SSH Login
# Disable Root SSH Login For OpenSSH
sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
# Disable Root SSH Login For Dropbear
sed -i 's/DROPBEAR_EXTRA_ARGS="/DROPBEAR_EXTRA_ARGS="-w/g' /etc/default/dropbear
}
# Set Time Zone
function configure_timezone {
echo \>\> Configuring: Time Zone
# Configure Time Zone
dpkg-reconfigure tzdata
}
# Add User Account
function configure_user {
echo \>\> Configuring: User Account
# Take User Input
echo -n "Please enter a user name: "
read -e USERNAME
# Add User Based On Input
useradd -m -s /bin/bash $USERNAME
# Set Password For Newly Added User
passwd $USERNAME
}
############################
## Installation Functions ##
############################
# Execute Install Functions
function install_basic {
packages_update
packages_purge
packages_create
packages_clean
packages_purge
}
# Install Lightweight Dropbear SSH Server & OpenSSH For SFTP Support
function install_dropbear {
echo \>\> Configuring Dropbear
# Install Dropbear
apt-get install dropbear
# Update Configuration Files
cp settings/dropbear /etc/default/dropbear
# Install OpenSSH For SFTP Support
install_ssh
# Remove OpenSSH Daemon
update-rc.d -f ssh remove
# Clean Package List
packages_purge
}
# Install Extra Packages Defined In List
function install_extra {
# Loop Through Package List
while read package; do
# Install Currently Selected Package
apt-get -q -y install $package
done < extra
# Clean Cached Packages
apt-get clean
}
# Install OpenSSH & Sets Configuration
function install_ssh {
echo \>\> Configuring SSH
# Install OpenSSH
apt-get install openssh-server
# Copy SSH Configuration Files
cp settings/sshd /etc/ssh/sshd_config
cp settings/ssh /etc/ssh/ssh_config
# Restart OpenSSH Daemon
/etc/init.d/ssh restart
# Clean Package List
packages_purge
}
#######################
## Package Functions ##
#######################
# Use DPKG To Remove Packages
function packages_clean {
echo \>\> Cleaning Packages
# Clear DPKG Package Selections
dpkg --clear-selections
# Set Package Selections
dpkg --set-selections < lists/temp
# Get Selections & Set To Purge
dpkg --get-selections | sed -e 's/deinstall/purge/' > /tmp/dpkg
# Set Package Selections
dpkg --set-selections < /tmp/dpkg
# Update DPKG
apt-get dselect-upgrade
# Upgrade Any Outdated Packages
apt-get upgrade
}
# Create Package List
function packages_create {
echo \>\> Creating Package List
# Copy Base Package List
cp lists/base lists/temp
# OpenVZ Check
if [ -f /proc/user_beancounters ] || [ -d /proc/bc ]; then
echo Detected OpenVZ!
# Physical Hardware/Hardware Virtualisation
else
# Copy Base Package List
cat lists/base-hw >> lists/temp
# Detect x86
if [ $(uname -m) == "i686" ]; then
echo Detected i686!
# Append Platform Relevent Packages To Package List
cat lists/kernel-i686 >> lists/temp
fi
# Detect x86_64
if [ $(uname -m) == "x86_64" ]; then
echo Detected x86_64!
# Append Platform Relevent Packages To Package List
cat lists/kernel-x86_64 >> lists/temp
fi
# Detect XEN PV x86
if [[ $(uname -r) == *xen* ]] && [ $(uname -m) == "i686" ]; then
echo Detected XEN PV i686!
# Append Platform Relevent Packages To Package List
cat lists/kernel-xen-i686 >> lists/temp
fi
# Detect XEN PV x86_64
if [[ $(uname -r) == *xen* ]] && [ $(uname -m) == "x86_64" ]; then
echo Detected XEN PV x86_64!
# Append Platform Relevent Packages To Package List
cat lists/kernel-xen-x86_64 >> lists/temp
fi
fi
# Sort Package List
sort -o lists/temp lists/temp
}
# Purge APT Package Lists
function packages_purge {
echo \>\> Cleaning Package States
# Empty Package List Files
echo -n > /var/lib/apt/extended_states
# Clean Cached Packages
apt-get clean
}
# Update Sources List & APT
function packages_update {
echo \>\> Setting Up APT Sources
# Copy Sources
cp settings/sources /etc/apt/sources.list
# Add DotDeb Source Key
wget http://www.dotdeb.org/dotdeb.gpg -qO - | apt-key add -
# Update Package Lists
apt-get update
}
#################
## Init Script ##
#################
case "$1" in
# Minimise System & Install Dropbear
dropbear)
install_basic
install_dropbear
;;
# Install Extra Packages
extra)
install_extra
;;
# Configure Install
configure)
configure_basic
;;
# Minimise System & Install OpenSSH
ssh)
install_basic
install_ssh
;;
# Show Help
*)
echo \>\> You must run this script with options. They are outlined below:
echo For a minimal Dropbear based install: bash minimal.sh dropbear
echo For a minimal OpenSSH based install: bash minimal.sh ssh
echo To install extra packages defined in the extra file: bash minimal.sh extra
echo To set the clock, clean files and create a user: bash minimal.sh configure
;;
esac