-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_install_firewall.sh
More file actions
executable file
·468 lines (426 loc) · 8.99 KB
/
linux_install_firewall.sh
File metadata and controls
executable file
·468 lines (426 loc) · 8.99 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/bin/bash
#
# Install Firewall [Linux]
#
# Install the system default firewall
#
# Supports:
# Linux-All
#
# Category:
# Firewall
#
# License:
# AGPLv3
#
# Author:
# Charlie Powell <cdp1337@bitsnbytes.dev>
#
# Link:
# https://github.com/eVAL-Agency/ScriptsCollection
#
# Changelog:
# 20260316 - Initial version
##
# Simple wrapper to emulate `which -s`
#
# The -s flag is not available on all systems, so this function
# provides a consistent way to check for command existence
# without having to include '&>/dev/null' everywhere.
#
# Returns 0 on success, 1 on failure
#
# Arguments:
# $1 - Command to check
#
# CHANGELOG:
# 2025.12.15 - Initial version (for a regression fix)
#
function cmd_exists() {
local CMD="$1"
which "$CMD" &>/dev/null
return $?
}
##
# Get which firewall is enabled,
# or "none" if none located
function get_enabled_firewall() {
if [ "$(systemctl is-active firewalld)" == "active" ]; then
echo "firewalld"
elif [ "$(systemctl is-active ufw)" == "active" ]; then
echo "ufw"
elif [ "$(systemctl is-active iptables)" == "active" ]; then
echo "iptables"
else
echo "none"
fi
}
##
# Get which firewall is available on the local system,
# or "none" if none located
#
# CHANGELOG:
# 2025.12.15 - Use cmd_exists to fix regression bug
# 2025.04.10 - Switch from "systemctl list-unit-files" to "which" to support older systems
function get_available_firewall() {
if cmd_exists firewall-cmd; then
echo "firewalld"
elif cmd_exists ufw; then
echo "ufw"
elif systemctl list-unit-files iptables.service &>/dev/null; then
echo "iptables"
else
echo "none"
fi
}
##
# Check if the OS is "like" a certain type
#
# Returns 0 if true, 1 if false
#
# Usage:
# if os_like debian; then ... ; fi
#
function os_like() {
local OS="$1"
if [ -f '/etc/os-release' ]; then
ID="$(egrep '^ID=' /etc/os-release | sed 's:ID=::')"
LIKE="$(egrep '^ID_LIKE=' /etc/os-release | sed 's:ID_LIKE=::')"
if [[ "$LIKE" =~ "$OS" ]] || [ "$ID" == "$OS" ]; then
return 0;
fi
fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_debian)" -eq 1 ]; then ... ; fi
# if os_like_debian -q; then ... ; fi
#
function os_like_debian() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like debian || os_like ubuntu; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_ubuntu)" -eq 1 ]; then ... ; fi
# if os_like_ubuntu -q; then ... ; fi
#
function os_like_ubuntu() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like ubuntu; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_rhel)" -eq 1 ]; then ... ; fi
# if os_like_rhel -q; then ... ; fi
#
function os_like_rhel() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like rhel || os_like fedora || os_like rocky || os_like centos; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_suse)" -eq 1 ]; then ... ; fi
# if os_like_suse -q; then ... ; fi
#
function os_like_suse() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like suse; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_arch)" -eq 1 ]; then ... ; fi
# if os_like_arch -q; then ... ; fi
#
function os_like_arch() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if os_like arch; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
fi
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_bsd)" -eq 1 ]; then ... ; fi
# if os_like_bsd -q; then ... ; fi
#
function os_like_bsd() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if [ "$(uname -s)" == 'FreeBSD' ]; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
else
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
fi
}
##
# Check if the OS is "like" a certain type
#
# ie: "ubuntu" will be like "debian"
#
# Returns 0 if true, 1 if false
# Prints 1 if true, 0 if false
#
# Usage:
# if [ "$(os_like_macos)" -eq 1 ]; then ... ; fi
# if os_like_macos -q; then ... ; fi
#
function os_like_macos() {
local QUIET=0
while [ $# -ge 1 ]; do
case $1 in
-q)
QUIET=1;;
esac
shift
done
if [ "$(uname -s)" == 'Darwin' ]; then
if [ $QUIET -eq 0 ]; then echo 1; fi
return 0;
else
if [ $QUIET -eq 0 ]; then echo 0; fi
return 1
fi
}
##
# Get the operating system version
#
# Just the major version number is returned
#
function os_version() {
if [ "$(uname -s)" == 'FreeBSD' ]; then
local _V="$(uname -K)"
if [ ${#_V} -eq 6 ]; then
echo "${_V:0:1}"
elif [ ${#_V} -eq 7 ]; then
echo "${_V:0:2}"
fi
elif [ -f '/etc/os-release' ]; then
local VERS="$(egrep '^VERSION_ID=' /etc/os-release | sed 's:VERSION_ID=::')"
if [[ "$VERS" =~ '"' ]]; then
# Strip quotes around the OS name
VERS="$(echo "$VERS" | sed 's:"::g')"
fi
if [[ "$VERS" =~ \. ]]; then
# Remove the decimal point and everything after
# Trims "24.04" down to "24"
VERS="${VERS/\.*/}"
fi
if [[ "$VERS" =~ "v" ]]; then
# Remove the "v" from the version
# Trims "v24" down to "24"
VERS="${VERS/v/}"
fi
echo "$VERS"
else
echo 0
fi
}
##
# Install a package with the system's package manager.
#
# Uses Redhat's yum, Debian's apt-get, and SuSE's zypper.
#
# Usage:
#
# ```syntax-shell
# package_install apache2 php7.0 mariadb-server
# ```
#
# @param $1..$N string
# Package, (or packages), to install. Accepts multiple packages at once.
#
#
# CHANGELOG:
# 2026.01.09 - Cleanup os_like a bit and add support for RHEL 9's dnf
# 2025.04.10 - Set Debian frontend to noninteractive
#
function package_install (){
echo "package_install: Installing $*..."
if os_like_bsd -q; then
pkg install -y $*
elif os_like_debian -q; then
DEBIAN_FRONTEND="noninteractive" apt-get -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" install -y $*
elif os_like_rhel -q; then
if [ "$(os_version)" -ge 9 ]; then
dnf install -y $*
else
yum install -y $*
fi
elif os_like_arch -q; then
pacman -Syu --noconfirm $*
elif os_like_suse -q; then
zypper install -y $*
else
echo 'package_install: Unsupported or unknown OS' >&2
echo 'Please report this at https://github.com/eVAL-Agency/ScriptsCollection/issues' >&2
exit 1
fi
}
##
# Install UFW
#
function install_ufw() {
if [ "$(os_like_rhel)" == 1 ]; then
# RHEL/CentOS requires EPEL to be installed first
package_install epel-release
fi
package_install ufw
# Auto-enable a newly installed firewall
ufw --force enable
systemctl enable ufw
systemctl start ufw
# Auto-add the current user's remote IP to the whitelist (anti-lockout rule)
local TTY_IP="$(who am i | awk '{print $NF}' | sed 's/[()]//g')"
if [ -n "$TTY_IP" ]; then
ufw allow from $TTY_IP comment 'Anti-lockout rule based on first install of UFW'
fi
}
##
# Install firewalld
#
# CHANGELOG:
# 2026.03.16 - Switch awk to use $NF for better support
#
function install_firewalld() {
package_install firewalld
# Auto-add the current user's remote IP to the whitelist (anti-lockout rule)
local TTY_IP="$(who am i | awk '{print $NF}' | sed 's/[()]//g')"
if [ -n "$TTY_IP" ]; then
# Anti-lockout rule based on first install of firewalld
firewall-cmd --zone=trusted --add-source=$TTY_IP --permanent
fi
}
##
# Install the system default firewall based on the OS type
#
# For Debian/Ubuntu, this installs UFW
# For RHEL/CentOS, this installs firewalld
# For SUSE, this installs firewalld
# For other OS types, this defaults to installing UFW
#
function firewall_install() {
local FIREWALL
FIREWALL=$(get_available_firewall)
if [ "$FIREWALL" != "none" ]; then
return
fi
if os_like_debian -q; then
install_ufw
elif os_like_rhel -q; then
install_firewalld
elif os_like_suse -q; then
install_firewalld
else
install_ufw
fi
}
firewall_install