-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ubuntu.sh
1543 lines (1519 loc) · 74.9 KB
/
Ubuntu.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Current Version: 5.8.7
## How to get and use?
# curl "https://source.zhijie.online/AutoDeploy/main/Ubuntu.sh" | sudo bash
# wget -qO- "https://source.zhijie.online/AutoDeploy/main/Ubuntu.sh" | sudo bash
## Function
# Get System Information
function GetSystemInformation() {
function CheckDNSConfiguration() {
USE_GLOBAL_DNS="false"
if [ "${USE_GLOBAL_DNS}" == "true" ]; then
CUSTOM_DNS=(
"8.8.4.4"
"8.8.8.8"
"2001:4860:4860::8844"
"2001:4860:4860::8888"
)
else
CUSTOM_DNS=(
"223.5.5.5"
"223.6.6.6"
"2400:3200::1"
"2400:3200:baba::1"
)
fi
DHCP_DNS=()
CUSTOM_DNS_LINE="" && for CUSTOM_DNS_TASK in "${!CUSTOM_DNS[@]}"; do
CUSTOM_DNS_LINE="${CUSTOM_DNS_LINE} ${CUSTOM_DNS[$CUSTOM_DNS_TASK]}"
CUSTOM_DNS_LINE=$(echo "${CUSTOM_DNS_LINE}" | sed "s/^\ //g")
done && CURRENT_DNS_EXCLUDE="$(echo ${DHCP_DNS[*]} ${CUSTOM_DNS_LINE} | sed 's/\ /\\\|/g')\|127.0.0.53"
if [ -f "/etc/resolv.conf" ]; then
CURRENT_DNS=(${DHCP_DNS[*]} $(cat "/etc/resolv.conf" | grep "nameserver\ " | sed "s/nameserver\ //g" | grep -v "${CURRENT_DNS_EXCLUDE}" | awk "{print $2}"))
CURRENT_DNS_LINE="" && for CURRENT_DNS_TASK in "${!CURRENT_DNS[@]}"; do
CURRENT_DNS_LINE="${CURRENT_DNS_LINE} ${CURRENT_DNS[$CURRENT_DNS_TASK]}"
CURRENT_DNS_LINE=$(echo "${CURRENT_DNS_LINE}" | sed "s/^\ //g")
done && DNS_LINE=$(echo "${CURRENT_DNS_LINE} ${CUSTOM_DNS_LINE}" | cut -d ' ' -f 1-3)
fi
}
function CheckMachineEnvironment() {
function CheckHypervisorEnvironment() {
which "virt-what" > "/dev/null" 2>&1
if [ "$?" -eq "1" ]; then
sed -i "s/[a-z]\{0,\}[.]\{0,\}archive.ubuntu.com/mirrors.ustc.edu.cn/g;s/[a-z]\{0,\}[.]\{0,\}ports.ubuntu.com/mirrors.ustc.edu.cn/g;s/[a-z]\{0,\}[.]\{0,\}security.ubuntu.com/mirrors.ustc.edu.cn/g" "/etc/apt/sources.list" && apt update && apt install virt-what -qy
which "virt-what" > "/dev/null" 2>&1
if [ "$?" -eq "1" ]; then
echo "virt-what has not been installed!"
exit 1
fi
fi && hypervisor_environment=$(virt-what) && if [ "${hypervisor_environment}" == "" ]; then
hypervisor_environment="none"
elif [ "${hypervisor_environment}" == "kvm" ]; then
HYPERVISOR_AGENT=("ksmtuned" "qemu-guest-agent")
elif [ "${hypervisor_environment}" == "vmware" ]; then
HYPERVISOR_AGENT=("open-vm-tools")
elif [ "${hypervisor_environment}" == "virtualbox" ]; then
HYPERVISOR_AGENT=("virtualbox-guest-dkms")
fi
}
CheckHypervisorEnvironment
}
function GenerateDomain() {
RESET_DOMAIN="false"
if [ -f "/etc/resolv.conf" ]; then
NEW_DOMAIN=($(cat "/etc/resolv.conf" | grep "^search " | cut -d " " -f 2- | awk "{print $2}"))
fi
if [ $(echo "${NEW_DOMAIN[*]}" | sed "s/\ /\\n/g" | wc -l ) -lt 1 ] || [ "${RESET_DOMAIN}" == "true" ]; then
NEW_DOMAIN=("localdomain")
fi
}
function GenerateHostname() {
RESET_HOSTNAME="false"
if [ -f "/etc/hostname" ]; then
NEW_HOSTNAME=$(cat "/etc/hostname" | awk "{print $2}")
fi
if [ $(echo "${NEW_HOSTNAME}" | wc -l) -ne 1 ]; then
which "hostname" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
NEW_HOSTNAME=$(hostname)
fi
fi
if [ $(echo "${NEW_HOSTNAME}" | wc -l) -ne 1 ] || [ "${RESET_HOSTNAME}" == "true" ]; then
NEW_HOSTNAME="Ubuntu-$(date '+%Y%m%d%H%M%S')"
fi
}
function GetCPUpsABILevel() {
# https://dl.xanmod.org/check_x86-64_psabi.sh
psABILevel=$(awk 'BEGIN{while(!/flags/)if(getline<"/proc/cpuinfo"!=1)exit 0;if(/lm/&&/cmov/&&/cx8/&&/fpu/&&/fxsr/&&/mmx/&&/syscall/&&/sse2/)l=1;if(l==1&&/cx16/&&/lahf/&&/popcnt/&&/sse4_1/&&/sse4_2/&&/ssse3/)l=2;if(l==2&&/avx/&&/avx2/&&/bmi1/&&/bmi2/&&/f16c/&&/fma/&&/abm/&&/movbe/&&/xsave/)l=3;if(l==3&&/avx512f/&&/avx512bw/&&/avx512cd/&&/avx512dq/&&/avx512vl/)l=4;print l}')
}
function GetCPUVendorID() {
CPU_VENDOR_ID=$(cat '/proc/cpuinfo' | grep 'vendor_id' | uniq | awk -F ':' '{print $2}' | awk -F ' ' '{print $1}')
if [ "${CPU_VENDOR_ID}" == "AuthenticAMD" ]; then
CPU_VENDOR_ID="AMD"
ENABLE_IOMMU=" amd_iommu=on iommu=pt pci=assign-busses pcie_acs_override=downstream,multifunction"
INTEL_GVT_MODULES=()
INTEL_HDMI_AUDIO_MUDULE=()
MICROCODE=("amd64-microcode")
NESTED_MODULES=("kvm_amd")
elif [ "${CPU_VENDOR_ID}" == "GenuineIntel" ]; then
CPU_VENDOR_ID="Intel"
ENABLE_IOMMU=" intel_iommu=on iommu=pt pci=assign-busses pcie_acs_override=downstream,multifunction"
INTEL_GVT_MODULES=("i915" "kvmgt")
INTEL_HDMI_AUDIO_MUDULE=("snd_hda_intel")
MICROCODE=("intel-microcode")
NESTED_MODULES=("kvm_intel")
else
CPU_VENDOR_ID="Unknown"
ENABLE_IOMMU=""
INTEL_GVT_MODULES=()
INTEL_HDMI_AUDIO_MUDULE=()
MICROCODE=()
NESTED_MODULES=()
fi
}
function GetLSBCodename() {
ALWAYS_LATEST="false"
LSBCodename_LTS="noble"
LSBVersion_LTS="24.04"
LSBCodename_LTS_OLD="jammy"
LSBVersion_LTS_OLD="22.04"
LSBCodename_NON_LTS="mantic"
LSBVersion_NON_LTS="23.10"
which "lsb_release" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
LSBCodename_CURRENT=$(lsb_release -cs)
LSBVersion_CURRENT=$(lsb_release -rs)
if [ "$(lsb_release -ds | grep 'LTS')" == "" ]; then
WHETHER_LTS_NON_TLS="FALSE"
else
WHETHER_LTS_NON_TLS="TRUE"
fi
else
if [ -f '/etc/os-release' ]; then
LSBCodename_CURRENT=$(cat "/etc/os-release" | grep "UBUNTU\_CODENAME\=" | sed "s/UBUNTU\_CODENAME\=//g")
LSBVersion_CURRENT=$(cat "/etc/os-release" | grep "VERSION_ID\=" | sed "s/VERSION_ID\=//g" | tr -d "\"")
if [ "$(cat '/etc/os-release' | grep "VERSION\=" | grep 'LTS')" == "" ]; then
WHETHER_LTS_NON_TLS="FALSE"
else
WHETHER_LTS_NON_TLS="TRUE"
fi
fi
fi
if [ "$(awk -v NUM1=$LSBVersion_CURRENT -v NUM2=$LSBVersion_LTS 'BEGIN{print (NUM1 > NUM2) ? 1 : 0}')" -eq "1" ] && [ "$(awk -v NUM1=$LSBVersion_CURRENT -v NUM2=$LSBVersion_NON_LTS 'BEGIN{print (NUM1 > NUM2) ? 1 : 0}')" -eq "1" ]; then
LSBCodename="${LSBCodename_CURRENT}"
LSBVersion="${LSBVersion_CURRENT}"
else
if [ "${ALWAYS_LATEST}" == "true" ]; then
if [ "$(awk -v NUM1=$LSBVersion_LTS -v NUM2=$LSBVersion_NON_LTS 'BEGIN{print (NUM1 > NUM2) ? 1 : 0}')" -eq "1" ]; then
LSBCodename="${LSBCodename_LTS}"
LSBVersion="${LSBVersion_LTS}"
else
LSBCodename="${LSBCodename_NON_LTS}"
LSBVersion="${LSBVersion_NON_LTS}"
fi
else
if [ "${WHETHER_LTS_NON_TLS}" == "TRUE" ]; then
LSBCodename="${LSBCodename_LTS}"
LSBVersion="${LSBVersion_LTS}"
else
LSBCodename="${LSBCodename_NON_LTS}"
LSBVersion="${LSBVersion_NON_LTS}"
fi
fi
fi
}
function GetOSArchitecture() {
which "dpkg" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
OSArchitecture=$(dpkg --print-architecture)
else
if [ "$(uname -m)" == "aarch64" ]; then
OSArchitecture="arm64"
elif [ "$(uname -m)" == "x86_64" ]; then
OSArchitecture="amd64"
fi
fi
if [ "${OSArchitecture}" == "arm64" ]; then
MIRROR_URL="ubuntu-ports"
elif [ "${OSArchitecture}" == "amd64" ]; then
MIRROR_URL="ubuntu"
else
echo "Unsupported architecture."
exit 1
fi
}
function SetGHProxyDomain() {
GHPROXY_URL=""
if [ "${GHPROXY_URL}" != "" ]; then
export GHPROXY_URL="https://${GHPROXY_URL}/"
fi
}
CheckDNSConfiguration
GenerateDomain && CheckMachineEnvironment
GenerateHostname
GetCPUpsABILevel
GetCPUVendorID
GetLSBCodename
GetOSArchitecture
SetGHProxyDomain
}
# Set Repository Mirror
function SetRepositoryMirror() {
mirror_list=(
"deb ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename} main multiverse restricted universe"
"deb ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-backports main multiverse restricted universe"
"deb ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-proposed main multiverse restricted universe"
"deb ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-security main multiverse restricted universe"
"deb ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-updates main multiverse restricted universe"
"deb-src ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename} main multiverse restricted universe"
"deb-src ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-backports main multiverse restricted universe"
"deb-src ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-proposed main multiverse restricted universe"
"deb-src ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-security main multiverse restricted universe"
"deb-src ${transport_protocol}://mirrors.ustc.edu.cn/${MIRROR_URL} ${LSBCodename}-updates main multiverse restricted universe"
)
if [ ! -d "/etc/apt/sources.list.d" ]; then
mkdir "/etc/apt/sources.list.d"
else
rm -rf /etc/apt/sources.list.d/*.*
fi
rm -rf "/tmp/apt.autodeploy" && for mirror_list_task in "${!mirror_list[@]}"; do
echo "${mirror_list[$mirror_list_task]}" >> "/tmp/apt.autodeploy"
done && cat "/tmp/apt.autodeploy" > "/etc/apt/sources.list" && rm -rf "/tmp/apt.autodeploy"
}
# Set Readonly Flag
function SetReadonlyFlag() {
file_list=(
"/etc/apt/apt.conf.d/99autodeploy"
"/etc/apt/preferences"
"/etc/apt/sources.list"
"/etc/apt/sources.list.d/crowdsec.list"
"/etc/apt/sources.list.d/docker.list"
"/etc/apt/sources.list.d/frrouting.list"
"/etc/apt/sources.list.d/xanmod.list"
"/etc/chrony/chrony.conf"
"/etc/cockpit/cockpit.conf"
"/etc/default/lldpd"
"/etc/default/ufw"
"/etc/docker/daemon.json"
"/etc/fail2ban/fail2ban.local"
"/etc/fail2ban/filter.d/cockpit.conf"
"/etc/fail2ban/jail.local"
"/etc/fail2ban/jail.d/fail2ban_default.conf"
"/etc/gai.conf"
"/etc/hostname"
"/etc/hosts"
"/etc/netplan/netplan.yaml"
"/etc/sysctl.conf"
"/etc/systemd/resolved.conf.d/resolved.conf"
)
if [ "${read_only}" == "TRUE" ]; then
for file_list_task in "${!file_list[@]}"; do
if [ -d "${file_list[$file_list_task]}" ] || [ -f "${file_list[$file_list_task]}" ]; then
chattr +i "${file_list[$file_list_task]}" > "/dev/null" 2>&1
fi
done
elif [ "${read_only}" == "FALSE" ]; then
for file_list_task in "${!file_list[@]}"; do
if [ -d "${file_list[$file_list_task]}" ] || [ -f "${file_list[$file_list_task]}" ]; then
chattr -i "${file_list[$file_list_task]}" > "/dev/null" 2>&1
fi
done
fi
}
# Configure Packages
function ConfigurePackages() {
function ConfigureAPT() {
apt_conf_list=(
'# Disable HTTP Pipelining'
'Acquire::http::Pipeline-Depth "0";'
'# Disable Phased Updates'
'APT::Get::Always-Include-Phased-Updates true;'
'Update-Manager::Always-Include-Phased-Updates true;'
)
apt_preference_list=(
"${LSBCodename}-backports 990"
"${LSBCodename}-security 500"
"${LSBCodename}-updates 500"
"${LSBCodename} 500"
"${LSBCodename}-proposed 100"
)
if [ ! -d "/etc/apt/apt.conf.d" ]; then
mkdir "/etc/apt/apt.conf.d"
fi
rm -rf "/tmp/apt_conf_list.autodeploy" && for apt_conf_list_task in "${!apt_conf_list[@]}"; do
echo "${apt_conf_list[$apt_conf_list_task]}" >> "/tmp/apt_conf_list.autodeploy"
done && cat "/tmp/apt_conf_list.autodeploy" > "/etc/apt/apt.conf.d/99autodeploy" && rm -rf "/tmp/apt_conf_list.autodeploy"
if [ -d "/etc/apt/preferences.d" ]; then
rm -rf "/etc/apt/preferences.d"
fi && mkdir "/etc/apt/preferences.d"
rm -rf "/tmp/apt_preference_list.autodeploy" && for apt_preference_list_task in "${!apt_preference_list[@]}"; do
APT_PIN_RELEASE=$(echo "${apt_preference_list[$apt_preference_list_task]}" | cut -d " " -f 1)
APT_PIN_PRIORITY=$(echo "${apt_preference_list[$apt_preference_list_task]}" | cut -d " " -f 2)
if [ ! -z $(echo ${APT_PIN_PRIORITY} | grep "[a-z]\|[A-Z]\|-") ]; then
APT_PIN_PRIORITY="500"
fi
echo -e "Package: *\nPin: release a=${APT_PIN_RELEASE}\nPin-Priority: ${APT_PIN_PRIORITY}\n" >> "/tmp/apt_preference_list.autodeploy"
done && cat "/tmp/apt_preference_list.autodeploy" | sed '$d' > "/etc/apt/preferences"
}
function ConfigureBusybox() {
which "busybox" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
busybox --install -s
fi
}
function ConfigureChrony() {
chrony_list=(
"allow"
"clientloglimit 65536"
"driftfile /var/lib/chrony/chrony.drift"
"dumpdir /run/chrony"
"keyfile /etc/chrony/chrony.keys"
"leapsectz right/UTC"
"logdir /var/log/chrony"
"makestep 1 3"
"ratelimit burst 8 interval 3 leak 2"
"rtcsync"
)
DHCP_NTP=()
chrony_ntp_list=(
"${DHCP_NTP[@]}"
"ntp.ntsc.ac.cn"
"ntp1.nim.ac.cn"
"ntp2.nim.ac.cn"
"ntp.aliyun.com"
"ntp.tencent.com"
"time.apple.com"
"time.windows.com"
"time.cloudflare.com"
"time.nist.gov"
"pool.ntp.org"
)
which "chronyc" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
rm -rf "/tmp/chrony.autodeploy" && for chrony_list_task in "${!chrony_list[@]}"; do
echo "${chrony_list[$chrony_list_task]}" >> "/tmp/chrony.autodeploy"
done && for chrony_ntp_list_task in "${!chrony_ntp_list[@]}"; do
if [ "${chrony_ntp_list[$chrony_ntp_list_task]}" == "ntp.ntsc.ac.cn" ] || [ "${chrony_ntp_list[$chrony_ntp_list_task]}" == "ntp1.nim.ac.cn" ] || [ "${chrony_ntp_list[$chrony_ntp_list_task]}" == "ntp2.nim.ac.cn" ] || [ "$(echo ${DHCP_NTP[@]} | grep ${chrony_ntp_list[$chrony_ntp_list_task]})" != "" ]; then
echo "server ${chrony_ntp_list[$chrony_ntp_list_task]} iburst prefer" >> "/tmp/chrony.autodeploy"
else
echo "server ${chrony_ntp_list[$chrony_ntp_list_task]} iburst" >> "/tmp/chrony.autodeploy"
fi
done && cat "/tmp/chrony.autodeploy" > "/etc/chrony/chrony.conf" && rm -rf "/tmp/chrony.autodeploy" && systemctl restart chrony && sleep 5s && chronyc activity && chronyc tracking && chronyc clients && hwclock -w
fi
}
function ConfigureCockpit() {
cockpit_list=(
"[Session]"
"IdleTimeout = 60"
"[WebService]"
"MaxStartups = 3:75:5"
)
which "cockpit-bridge" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
rm -rf "/tmp/cockpit.autodeploy" && for cockpit_list_task in "${!cockpit_list[@]}"; do
echo "${cockpit_list[$cockpit_list_task]}" >> "/tmp/cockpit.autodeploy"
done && cat "/tmp/cockpit.autodeploy" > "/etc/cockpit/cockpit.conf" && rm -rf "/tmp/cockpit.autodeploy" && systemctl restart cockpit
fi
}
function ConfigureCrontab() {
crontab_list=(
"0 0 * * 7 sudo apt update && sudo apt full-upgrade -qy && sudo apt autoremove -qy --purge"
"# 0 4 * * 7 sudo reboot"
"@reboot sudo rm -rf /root/.*_history /root/.ssh/known_hosts*"
)
which "crontab" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
rm -rf "/tmp/crontab.autodeploy" && for crontab_list_task in "${!crontab_list[@]}"; do
echo "${crontab_list[$crontab_list_task]}" >> "/tmp/crontab.autodeploy"
done && crontab -u "root" "/tmp/crontab.autodeploy" && crontab -lu "root" && rm -rf "/tmp/crontab.autodeploy"
fi
}
function ConfigureCrowdSec() {
crowdsec_hub_list=(
"crowdsecurity/iptables"
"crowdsecurity/linux-lpe"
"crowdsecurity/linux"
"crowdsecurity/sshd"
)
which "cscli" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
for crowdsec_hub_list_task in "${!crowdsec_hub_list[@]}"; do
cscli collections install ${crowdsec_hub_list[$crowdsec_hub_list_task]}
done && systemctl restart crowdsec && cscli hub list
fi
}
function ConfigureDockerEngine() {
ENABLE_IPV6_ADDRESS="false"
DOCKER_REGISTRY_MIRRORS="https://docker.mirrors.ustc.edu.cn"
if [ "${ENABLE_IPV6_ADDRESS:-false}" == "true" ]; then
which "bc" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
which "sha1sum" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
which "uuidgen" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
UNIQUE_PREFIX=$(echo $(date "+%s%N")$(uuidgen | tr -d "-" | tr "A-Z" "a-z") | sha1sum | cut -c 31-)
DOCKER_PREFIX="fd$(echo ${UNIQUE_PREFIX} | cut -c 1-2):$(echo ${UNIQUE_PREFIX} | cut -c 3-6):$(echo ${UNIQUE_PREFIX} | cut -c 7-10)"
else
DOCKER_PREFIX="2001:db8:1"
fi
fi
fi
DOCKER_IPV6_LIST=(
" \"fixed-cidr-v6\": \"${DOCKER_PREFIX}::/64\","
" \"ipv6\": true,"
)
fi
docker_list=(
"{"
" \"experimental\": true,"
${DOCKER_IPV6_LIST[*]}
" \"registry-mirrors\": ["
" \"${DOCKER_REGISTRY_MIRRORS}\""
" ]"
"}"
)
which "docker" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
if [ ! -d "/docker" ]; then
mkdir "/docker"
fi && chown -R ${DEFAULT_USERNAME}:docker "/docker" && chmod -R 775 "/docker"
if [ ! -d "/etc/docker" ]; then
mkdir "/etc/docker"
fi
rm -rf "/tmp/docker.autodeploy" && for docker_list_task in "${!docker_list[@]}"; do
echo "${docker_list[$docker_list_task]}" >> "/tmp/docker.autodeploy"
done && cat "/tmp/docker.autodeploy" > "/etc/docker/daemon.json" && systemctl restart docker && rm -rf "/tmp/docker.autodeploy"
fi
}
function ConfigureFail2Ban() {
fail2ban_list=(
"[cockpit]"
"bantime = 604800"
"enabled = true"
"filter = cockpit"
"findtime = 60"
"logpath = /var/log/auth.log"
"maxretry = 5"
"port = 9090"
"[sshd]"
"bantime = 604800"
"enabled = true"
"filter = sshd"
"findtime = 60"
"logpath = /var/log/auth.log"
"maxretry = 5"
"port = 9022"
)
fail2ban_cockpit_list=(
"[Definition]"
"failregex = pam_unix\(cockpit:auth\): authentication failure; logname=.* uid=.* euid=.* tty=.* ruser=.* rhost=<HOST>"
"journalmatch = SYSLOG_FACILITY=10 PRIORITY=5"
)
which "fail2ban-client" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
if [ -d "/etc/fail2ban/jail.d" ]; then
rm -rf /etc/fail2ban/jail.d/*
else
mkdir "/etc/fail2ban/jail.d"
fi
if [ -f "/etc/fail2ban/fail2ban.conf" ]; then
cat "/etc/fail2ban/fail2ban.conf" > "/etc/fail2ban/fail2ban.local"
fi
if [ -f "/etc/fail2ban/jail.conf" ]; then
cat "/etc/fail2ban/jail.conf" | sed "s/action\ \=\ iptables\-allports/action\ \=\ ufw/g;s/banaction\ \=\ iptables\-multiport/banaction\ \=\ ufw/g;s/banaction\ \=\ iptables\-multiport\-log/banaction\ \=\ ufw/g;s/banaction\ \=\ ufw\-log/banaction\ \=\ ufw/g;s/banaction\_allports\ \=\ iptables\-allports/banaction\_allports\ \=\ ufw/g" > "/etc/fail2ban/jail.local"
fi
rm -rf "/tmp/fail2ban.autodeploy" && for fail2ban_cockpit_list_task in "${!fail2ban_cockpit_list[@]}"; do
echo "${fail2ban_cockpit_list[$fail2ban_cockpit_list_task]}" >> "/tmp/fail2ban.autodeploy"
done && cat "/tmp/fail2ban.autodeploy" > "/etc/fail2ban/filter.d/cockpit.conf" && rm -rf "/tmp/fail2ban.autodeploy"
rm -rf "/tmp/fail2ban.autodeploy" && for fail2ban_list_task in "${!fail2ban_list[@]}"; do
echo "${fail2ban_list[$fail2ban_list_task]}" >> "/tmp/fail2ban.autodeploy"
done && cat "/tmp/fail2ban.autodeploy" > "/etc/fail2ban/jail.d/fail2ban_default.conf" && rm -rf "/tmp/fail2ban.autodeploy" && systemctl enable fail2ban && fail2ban-client reload && sleep 5s && fail2ban-client status
fi
}
function ConfigureFRRouting() {
frrouting_list=(
"frr defaults datacenter"
"log syslog errors"
)
which "vtysh" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
rm -rf "/tmp/frrouting.autodeploy" && for frrouting_list_task in "${!frrouting_list[@]}"; do
echo "${frrouting_list[$frrouting_list_task]}" >> "/tmp/frrouting.autodeploy"
done && cat "/tmp/frrouting.autodeploy" > "/etc/frr/frr.conf" && rm -rf "/tmp/frrouting.autodeploy"
systemctl restart frr && sleep 5s && vtysh -c "show running-config" && vtysh -c "show ip route" && vtysh -c "show ipv6 route"
fi
}
function ConfigureGit() {
gitconfig_key_list=(
"commit.gpgsign"
"gpg.program"
"http.proxy"
"https.proxy"
"user.name"
"user.email"
"user.signingkey"
"url.${GHPROXY_URL}https://github.com/.insteadOf"
)
gitconfig_value_list=(
"${GIT_COMMIT_GPGSIGN:-false}"
"${GIT_GPG_PROGRAM:-gpg}"
"${GIT_HTTP_PROXY}"
"${GIT_HTTPS_PROXY}"
"${GIT_USER_NAME}"
"${GIT_USER_EMAIL}"
"${GIT_USER_SIGNINGKEY}"
"https://github.com/"
)
which "git" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
for gitconfig_list_task in "${!gitconfig_key_list[@]}"; do
git config --global --unset ${gitconfig_key_list[$gitconfig_list_task]}
if [ "${gitconfig_value_list[$gitconfig_list_task]}" != "" ]; then
git config --global ${gitconfig_key_list[$gitconfig_list_task]} "${gitconfig_value_list[$gitconfig_list_task]}"
fi
done
fi
if [ -f "/root/.gitconfig" ] && [ "${GIT_USER_CONFIG}" != "TRUE" ]; then
mv "/root/.gitconfig" "/root/.gitconfig.bak" && GIT_COMMIT_GPGSIGN="" && GIT_GPG_PROGRAM="" && GIT_HTTP_PROXY="" && GIT_HTTPS_PROXY="" && GIT_USER_NAME="" && GIT_USER_EMAIL="" && GIT_USER_SIGNINGKEY="" && GIT_USER_CONFIG="TRUE" && ConfigureGit && mv "/root/.gitconfig" "/home/${DEFAULT_USERNAME}/.gitconfig" && chown -R $DEFAULT_USERNAME:$DEFAULT_USERNAME "/home/${DEFAULT_USERNAME}/.gitconfig" && mv "/root/.gitconfig.bak" "/root/.gitconfig"
fi
}
function ConfigureGPG() {
GPG_PUBKEY=""
if [ "${GPG_PUBKEY}" == "" ]; then
GPG_PUBKEY="DD982DAAB9C71C78F9563E5207EB56787030D792"
fi
which "gpg" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
rm -rf "/home/${DEFAULT_USERNAME}/.gnupg" "/root/.gnupg" && gpg --keyserver hkps://keyserver.ubuntu.com:443 --recv ${GPG_PUBKEY} && echo "${GPG_PUBKEY}" | awk 'BEGIN { FS = "\n" }; { print $1":6:" }' | gpg --import-ownertrust && GPG_PUBKEY_ID_A=$(gpg --list-keys --keyid-format LONG | grep "pub\|sub" | awk '{print $2, $4}' | grep "\[A\]" | awk '{print $1}' | awk -F '/' '{print $2}') && GPG_PUBKEY_ID_C=$(gpg --list-keys --keyid-format LONG | grep "pub\|sub" | awk '{print $2, $4}' | grep "\[C\]" | awk '{print $1}' | awk -F '/' '{print $2}')
if [ "${GPG_PUBKEY_ID_A}" != "" ]; then
gpg_agent_list=(
"enable-ssh-support"
"pinentry-program /usr/bin/pinentry-tty"
)
rm -rf "/root/.gnupg/gpg-agent.conf" && for gpg_agent_list_task in "${!gpg_agent_list[@]}"; do
echo "${gpg_agent_list[$gpg_agent_list_task]}" >> "/root/.gnupg/gpg-agent.conf"
done && echo "${GPG_PUBKEY_ID_A}" > "/root/.gnupg/sshcontrol" && gpg --export-ssh-key ${GPG_PUBKEY_ID_C} > "/root/.gnupg/authorized_keys" && if [ -d "/root/.gnupg" ]; then
mv "/root/.gnupg" "/home/${DEFAULT_USERNAME}/.gnupg" && chown -R $DEFAULT_USERNAME:$DEFAULT_USERNAME "/home/${DEFAULT_USERNAME}/.gnupg"
fi
fi
fi
}
function ConfigureGrub() {
which "update-grub" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
if [ -f "/usr/share/grub/default/grub" ]; then
rm -rf "/tmp/grub.autodeploy" && cat "/usr/share/grub/default/grub" | sed "s/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"quiet splash\"/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"quiet splash${ENABLE_IOMMU}\"/g" > "/tmp/grub.autodeploy" && cat "/tmp/grub.autodeploy" > "/etc/default/grub" && update-grub && rm -rf "/tmp/grub.autodeploy"
fi
fi
}
function ConfigureLandscape() {
if [ -f "/usr/lib/python3/dist-packages/landscape/lib/network.py" ]; then
cat "/usr/lib/python3/dist-packages/landscape/lib/network.py" | sed "s/tostring/tobytes/g" > "/tmp/landscape.autodeploy" && cat "/tmp/landscape.autodeploy" > "/usr/lib/python3/dist-packages/landscape/lib/network.py" && rm -rf "/tmp/landscape.autodeploy"
fi
}
function ConfigureLLDPD() {
which "lldpcli" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
echo 'DAEMON_ARGS="-c -e -f -s -x"' > "/tmp/lldpd.autodeploy" && cat "/tmp/lldpd.autodeploy" > "/etc/default/lldpd" && rm -rf "/tmp/lldpd.autodeploy"
systemctl restart lldpd && lldpcli show neighbors detail
fi
}
function ConfigureModules() {
if [ -d "/etc/modprobe.d" ]; then
rm -rf "/etc/modprobe.d" && mkdir -p "/etc/modprobe.d"
fi
if [ -f "/etc/modules" ]; then
rm -rf "/etc/modules"
fi
if [ "${ENABLE_IOMMU}" != "" ]; then
IOMMU_MODULES=("vfio" "vfio_iommu_type1" "vfio_pci" "vfio_virqfd")
echo "options vfio_iommu_type1 allow_unsafe_interrupts=1" > "/etc/modprobe.d/vfio_iommu_type1.conf"
fi
module_list=(
"kvm"
${INTEL_GVT_MODULES[*]}
${INTEL_HDMI_AUDIO_MUDULE[*]}
${IOMMU_MODULES[*]}
${NESTED_MODULES[*]}
)
rm -rf "/tmp/module.autodeploy" && for module_list_task in "${!module_list[@]}"; do
echo "${module_list[$module_list_task]}" >> "/tmp/module.autodeploy"
done && cat "/tmp/module.autodeploy" | sort | uniq > "/etc/modules" && rm -rf "/tmp/module.autodeploy"
echo "options kvm ignore_msrs=1 report_ignored_msrs=0" >> "/etc/modprobe.d/kvm.conf"
if [ "${CPU_VENDOR_ID}" == "AMD" ]; then
echo "options kvm-amd nested=1" > "/etc/modprobe.d/kvm-amd.conf"
elif [ "${CPU_VENDOR_ID}" == "Intel" ]; then
i915_GUC_OPTION="" # 0 | 1 - GuC | 2 - HuC | 3 - GuC / HuC
echo "options i915 enable_guc=${i915_GUC_OPTION:-3} enable_gvt=1" > "/etc/modprobe.d/i915.conf"
echo "options kvm-intel nested=Y" > "/etc/modprobe.d/kvm-intel.conf"
echo "options snd-hda-intel enable_msi=1" > "/etc/modprobe.d/snd-hda-intel.conf"
fi
}
function ConfigureNetplan() {
STATIC_IP_CONFIG="" # enp6s18,10.192.31.254/19,10.192.0.1
netplan_list=(
"network:"
" version: 2"
" renderer: NetworkManager"
)
if [ -n "${STATIC_IP_CONFIG}" ]; then
netplan_list+=(
" ethernets:"
" $(echo "${STATIC_IP_CONFIG}" | cut -d ',' -f 1):"
" addresses:"
" - $(echo "${STATIC_IP_CONFIG}" | cut -d ',' -f 2)"
" routes:"
" - to: 0.0.0.0/0"
" via: $(echo "${STATIC_IP_CONFIG}" | cut -d ',' -f 3)"
)
fi
which "netplan" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
if [ ! -d "/etc/netplan" ]; then
mkdir "/etc/netplan"
else
rm -rf /etc/netplan/*.yaml
fi
rm -rf "/tmp/netplan.autodeploy" && for netplan_list_task in "${!netplan_list[@]}"; do
echo "${netplan_list[$netplan_list_task]}" >> "/tmp/netplan.autodeploy"
done && cat "/tmp/netplan.autodeploy" > "/etc/netplan/netplan.yaml" && rm -rf "/tmp/netplan.autodeploy" && chmod 600 "/etc/netplan/netplan.yaml" && netplan apply
fi
}
function ConfigureNut() {
which "upsmon" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
function Create_upsd_users() {
upsd_user_list=(
"admin,123456,master,FSD,SET"
"monuser,secret,slave,,"
)
}
function Generate_nut_conf() {
echo "MODE=${NUT_MODE:-none}" > "/etc/nut/nut.conf"
}
function Generate_ups_conf() {
ups_driver_list=(
"ups,usbhid-ups,auto"
)
ups_conf_list=(
"maxretry = 3"
"retrydelay = 5"
)
rm -rf "/tmp/ups.conf.autodeploy" && for ups_conf_list_task in "${!ups_conf_list[@]}"; do
echo "${ups_conf_list[$ups_conf_list_task]}" >> "/tmp/ups.conf.autodeploy"
done && for ups_driver_list_task in "${!ups_driver_list[@]}"; do
UPS_NAME=$(echo "${ups_driver_list[$ups_driver_list_task]}" | cut -d ',' -f 1)
UPS_DRIVER=$(echo "${ups_driver_list[$ups_driver_list_task]}" | cut -d ',' -f 2)
UPS_PORT=$(echo "${ups_driver_list[$ups_driver_list_task]}" | cut -d ',' -f 3)
echo -e "[${UPS_NAME}]\n driver = ${UPS_DRIVER}\n port = ${UPS_PORT}" >> "/tmp/ups.conf.autodeploy"
done && cat "/tmp/ups.conf.autodeploy" > "/etc/nut/ups.conf" && rm -rf "/tmp/ups.conf.autodeploy"
}
function Generate_upsd_conf() {
if [ "${NUT_MODE}" == "standalone" ]; then
upsd_config_list=(
"LISTEN 127.0.0.1 3493"
"LISTEN ::1 3493"
"MAXAGE 15"
"MAXCONN 1024"
"STATEPATH /var/run/nut"
)
else
upsd_config_list=(
"LISTEN 0.0.0.0 3493"
"MAXAGE 15"
"MAXCONN 1024"
"STATEPATH /var/run/nut"
)
fi
rm -rf "/tmp/upsd.conf.autodeploy" && for upsd_config_list_task in "${!upsd_config_list[@]}"; do
echo "${upsd_config_list[$upsd_config_list_task]}" >> "/tmp/upsd.conf.autodeploy"
done && cat "/tmp/upsd.conf.autodeploy" > "/etc/nut/upsd.conf" && rm -rf "/tmp/upsd.conf.autodeploy"
}
function Generate_upsd_users() {
rm -rf "/tmp/upsd.users.autodeploy" && for upsd_user_list_task in "${!upsd_user_list[@]}"; do
UPSD_USERNAME=$(echo "${upsd_user_list[$upsd_user_list_task]}" | cut -d ',' -f 1)
UPSD_PASSWORD=$(echo "${upsd_user_list[$upsd_user_list_task]}" | cut -d ',' -f 2)
UPSD_ROLE=$(echo "${upsd_user_list[$upsd_user_list_task]}" | cut -d ',' -f 3)
UPSD_ACTIONS=$(echo "${upsd_user_list[$upsd_user_list_task]}" | cut -d ',' -f 4-5 | tr ',' ' ' | sed 's/^ //g')
if [ "${UPSD_ACTIONS}" != "" ]; then
UPSD_ACTIONS=" actions = ${UPSD_ACTIONS}\n instcmds = ALL\n"
fi
echo -e "[${UPSD_USERNAME}]\n${UPSD_ACTIONS} password = ${UPSD_PASSWORD}\n upsmon ${UPSD_ROLE}" >> "/tmp/upsd.users.autodeploy"
done && cat "/tmp/upsd.users.autodeploy" > "/etc/nut/upsd.users" && rm -rf "/tmp/upsd.users.autodeploy"
}
function Generate_upsmon_conf() {
upsmon_list=(
"DEADTIME 15"
"FINALDELAY 5"
"HOSTSYNC 15"
"MINSUPPLIES 1"
"NOCOMMWARNTIME 300"
"POLLFREQ 5"
"POLLFREQALERT 5"
"POWERDOWNFLAG /etc/killpower"
"RBWARNTIME 43200"
'SHUTDOWNCMD "/sbin/shutdown -h +0"'
"MONITOR ${UPSMON_SYSTEM} 1 ${UPSMON_USERNAME} ${UPSMON_PASSWORD} ${UPSMON_ROLE}"
)
rm -rf "/tmp/upsmon.conf.autodeploy" && for upsmon_list_task in "${!upsmon_list[@]}"; do
echo "${upsmon_list[$upsmon_list_task]}" >> "/tmp/upsmon.conf.autodeploy"
done && cat "/tmp/upsmon.conf.autodeploy" > "/etc/nut/upsmon.conf" && rm -rf "/tmp/upsmon.conf.autodeploy"
}
function Generate_upssched_conf() {
upssched_conf=(
"CMDSCRIPT /bin/upssched-cmd"
)
}
NUT_MODE="" # netclient | netserver | none | standalone
NUT_HOST="" # localhost
rm -rf /etc/nut/*.* && case ${NUT_MODE:-none} in
netclient)
Create_upsd_users
UPSMON_USERNAME=$(echo "${upsd_user_list[*]}" | cut -d ' ' -f 2 | cut -d ',' -f 1)
UPSMON_PASSWORD=$(echo "${upsd_user_list[*]}" | cut -d ' ' -f 2 | cut -d ',' -f 2)
UPSMON_ROLE=$(echo "${upsd_user_list[*]}" | cut -d ' ' -f 2 | cut -d ',' -f 3)
UPSMON_SYSTEM="${UPS_NAME-ups}@${NUT_HOST:-localhost}"
nut_service_list=(
"nut-client,enabled"
"nut-driver,disabled"
"nut-monitor,enabled"
"nut-server,disabled"
)
Generate_nut_conf
Generate_upsmon_conf
Generate_upssched_conf
;;
netserver|standalone)
Create_upsd_users
UPSMON_USERNAME=$(echo "${upsd_user_list[*]}" | cut -d ' ' -f 1 | cut -d ',' -f 1)
UPSMON_PASSWORD=$(echo "${upsd_user_list[*]}" | cut -d ' ' -f 1 | cut -d ',' -f 2)
UPSMON_ROLE=$(echo "${upsd_user_list[*]}" | cut -d ' ' -f 1 | cut -d ',' -f 3)
UPSMON_SYSTEM="${UPS_NAME-ups}@localhost"
nut_service_list=(
"nut-client,enabled"
"nut-driver,enabled"
"nut-monitor,enabled"
"nut-server,enabled"
)
Generate_nut_conf
Generate_ups_conf
Generate_upsd_conf
Generate_upsd_users
Generate_upsmon_conf
Generate_upssched_conf
;;
none)
nut_service_list=(
"nut-client,disable"
"nut-driver,disable"
"nut-monitor,disable"
"nut-server,disable"
)
Generate_nut_conf
;;
esac && chown -R root:nut "/etc/nut" && chmod 640 /etc/nut/*.*
for nut_service_list_task in "${!nut_service_list[@]}"; do
NUT_SERVICE_NAME=$(echo "${nut_service_list[$nut_service_list_task]}" | cut -d ',' -f 1)
NUT_SERVICE_STATUS=$(echo "${nut_service_list[$nut_service_list_task]}" | cut -d ',' -f 2)
systemctl ${NUT_SERVICE_STATUS} ${NUT_SERVICE_NAME} > "/dev/null" 2>&1
done
fi
}
function ConfigureOpenSSH() {
OPENSSH_PASSWORD=""
which "ssh-keygen" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
if [ -d "/etc/ssh" ]; then
rm -rf /etc/ssh/ssh_host_* && ssh-keygen -t dsa -b 1024 -f "/etc/ssh/ssh_host_dsa_key" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t ecdsa -b 384 -f "/etc/ssh/ssh_host_ecdsa_key" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t ed25519 -f "/etc/ssh/ssh_host_ed25519_key" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t rsa -b 4096 -f "/etc/ssh/ssh_host_rsa_key" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && chmod 400 /etc/ssh/ssh_host_* && chmod 644 /etc/ssh/ssh_host_*.pub
fi
rm -rf "/root/.ssh" && mkdir "/root/.ssh" && touch "/root/.ssh/authorized_keys" && touch "/root/.ssh/known_hosts" && ssh-keygen -t dsa -b 1024 -f "/root/.ssh/id_dsa" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t ecdsa -b 384 -f "/root/.ssh/id_ecdsa" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t ed25519 -f "/root/.ssh/id_ed25519" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t rsa -b 4096 -f "/root/.ssh/id_rsa" -C "root@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && chmod 400 /root/.ssh/id_* && chmod 600 "/root/.ssh/authorized_keys" && chmod 644 "/root/.ssh/known_hosts" && chmod 644 /root/.ssh/id_*.pub && chmod 700 "/root/.ssh"
rm -rf "/home/${DEFAULT_USERNAME}/.ssh" && mkdir "/home/${DEFAULT_USERNAME}/.ssh" && if [ -f "/home/${DEFAULT_USERNAME}/.gnupg/authorized_keys" ]; then
mv "/home/${DEFAULT_USERNAME}/.gnupg/authorized_keys" "/home/${DEFAULT_USERNAME}/.ssh/authorized_keys"
else
touch "/home/${DEFAULT_USERNAME}/.ssh/authorized_keys"
fi && touch "/home/${DEFAULT_USERNAME}/.ssh/known_hosts" && ssh-keygen -t dsa -b 1024 -f "/home/${DEFAULT_USERNAME}/.ssh/id_dsa" -C "${DEFAULT_USERNAME}@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t ecdsa -b 384 -f "/home/${DEFAULT_USERNAME}/.ssh/id_ecdsa" -C "${DEFAULT_USERNAME}@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t ed25519 -f "/home/${DEFAULT_USERNAME}/.ssh/id_ed25519" -C "${DEFAULT_USERNAME}@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && ssh-keygen -t rsa -b 4096 -f "/home/${DEFAULT_USERNAME}/.ssh/id_rsa" -C "${DEFAULT_USERNAME}@${NEW_HOSTNAME}" -N "${OPENSSH_PASSWORD}" && chown -R $DEFAULT_USERNAME:$DEFAULT_USERNAME "/home/${DEFAULT_USERNAME}/.ssh" && chown -R $DEFAULT_USERNAME:$DEFAULT_USERNAME /home/${DEFAULT_USERNAME}/.ssh/* && chmod 400 /home/${DEFAULT_USERNAME}/.ssh/id_* && chmod 600 "/home/${DEFAULT_USERNAME}/.ssh/authorized_keys" && chmod 644 "/home/${DEFAULT_USERNAME}/.ssh/known_hosts" && chmod 644 /home/${DEFAULT_USERNAME}/.ssh/id_*.pub && chmod 700 "/home/${DEFAULT_USERNAME}/.ssh"
fi
}
function ConfigurePostfix() {
if [ -f "/etc/postfix/main.cf" ]; then
if [ "$(cat '/etc/postfix/main.cf' | grep 'myhostname\ \=\ ')" != "" ]; then
CURRENT_HOSTNAME=$(cat "/etc/postfix/main.cf" | grep "myhostname\ \=\ " | sed "s/myhostname\ \=\ //g")
cat "/etc/postfix/main.cf" | sed "s/$CURRENT_HOSTNAME/$NEW_HOSTNAME/g" > "/tmp/main.cf.autodeploy" && cat "/tmp/main.cf.autodeploy" > "/etc/postfix/main.cf" && rm -rf "/tmp/main.cf.autodeploy"
fi
fi
}
function ConfigurePythonPyPI() {
which "pip3" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
WHICH_PIP="pip3"
else
which "pip" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
WHICH_PIP="pip"
else
WHICH_PIP="null"
fi
fi
if [ "${WHICH_PIP}" != "null" ]; then
${WHICH_PIP} config set global.index-url "https://mirrors.ustc.edu.cn/pypi/web/simple"
fi
if [ -f "/root/.config/pip/pip.conf" ]; then
if [ ! -d "/home/${DEFAULT_USERNAME}/.config" ]; then
mkdir "/home/${DEFAULT_USERNAME}/.config"
fi
if [ ! -d "/home/${DEFAULT_USERNAME}/.config/pip" ]; then
mkdir "/home/${DEFAULT_USERNAME}/.config/pip"
fi
rm -rf "/home/${DEFAULT_USERNAME}/.config/pip/pip.conf" && cp -rf "/root/.config/pip/pip.conf" "/home/${DEFAULT_USERNAME}/.config/pip/pip.conf" && chown -R $DEFAULT_USERNAME:$DEFAULT_USERNAME "/home/${DEFAULT_USERNAME}/.config" && chown -R $DEFAULT_USERNAME:$DEFAULT_USERNAME "/home/${DEFAULT_USERNAME}/.config/pip" && chown -R $DEFAULT_USERNAME:$DEFAULT_USERNAME "/home/${DEFAULT_USERNAME}/.config/pip/pip.conf"
fi
}
function ConfigureResolved() {
resolved_list=(
"[Resolve]"
"DNS=${DNS_LINE}"
"DNSOverTLS=opportunistic"
"DNSSEC=allow-downgrade"
"DNSStubListener=false"
"Domains=${NEW_DOMAIN[*]}"
)
which "resolvectl" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
if [ ! -d "/etc/systemd/resolved.conf.d" ]; then
mkdir "/etc/systemd/resolved.conf.d"
else
rm -rf /etc/systemd/resolved.conf.d/*.conf
fi
rm -rf "/tmp/resolved.autodeploy" && for resolved_list_task in "${!resolved_list[@]}"; do
echo "${resolved_list[$resolved_list_task]}" | sed "s/DNS\=\ /DNS\=/g" >> "/tmp/resolved.autodeploy"
done && cat "/tmp/resolved.autodeploy" > "/etc/systemd/resolved.conf.d/resolved.conf" && systemctl restart systemd-resolved && rm -rf "/tmp/resolved.autodeploy" && if [ -f "/etc/resolv.conf" ]; then
chattr -i "/etc/resolv.conf" > "/dev/null" 2>&1
rm -rf "/etc/resolv.conf" && ln -s "/run/systemd/resolve/resolv.conf" "/etc/resolv.conf"
fi
else
resolv_conf_list=(
${CURRENT_DNS[@]}
${CUSTOM_DNS[@]}
)
rm -rf "/tmp/resolv.autodeploy" && DNS_COUNT="1" && for resolv_conf_list_task in "${!resolv_conf_list[@]}"; do
if [ "${DNS_COUNT}" -gt "3" ]; then
break
else
echo "nameserver ${resolv_conf_list[$resolv_conf_list_task]}" >> "/tmp/resolv.autodeploy"
fi && DNS_COUNT=$(( ${DNS_COUNT} + 1 ))
done && echo "search ${NEW_DOMAIN[*]}" >> "/tmp/resolv.autodeploy" && if [ -f "/etc/resolv.conf" ]; then
chattr -i "/etc/resolv.conf" > "/dev/null" 2>&1
if [ "$?" -eq "1" ]; then
rm -rf "/etc/resolv.conf"
fi
fi && rm -rf "/etc/resolv.conf" && cat "/tmp/resolv.autodeploy" > "/etc/resolv.conf" && rm -rf "/tmp/resolv.autodeploy" && chattr +i "/etc/resolv.conf"
fi
}
function ConfigureSNMP() {
SNMP_AUTH_PASS="${ROOT_PASSWORD}"
SNMP_PRIV_PASS="${NEW_HOSTNAME}"
SNMP_SYS_CONTACT="${DEFAULT_FULLNAME}"
SNMP_SYS_LOCATION="${NEW_HOSTNAME}"
SNMP_SYS_NAME="${NEW_FULL_DOMAIN}"
SNMP_USER="ubuntu"
snmp_list=(
"agentaddress udp:161,udp6:161"
"master agentx"
"rouser ${SNMP_USER}"
"sysContact ${SNMP_SYS_CONTACT}"
"sysLocation ${SNMP_SYS_LOCATION}"
"sysName ${SNMP_SYS_NAME}"
"sysServices 76"
)
which "snmpwalk" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
systemctl stop snmpd
kill $(ps -ef | grep snmp | grep -v 'grep' | cut -d ' ' -f 3) > "/dev/null" 2>&1
sed -i 's/^mibs :/# mibs :/g' "/etc/snmp/snmp.conf"
echo "createUser ${SNMP_USER} SHA \"${SNMP_AUTH_PASS}\" AES \"${SNMP_PRIV_PASS}\"" > "/var/lib/snmp/snmpd.conf"
rm -rf "/tmp/snmp.autodeploy" && for snmp_list_task in "${!snmp_list[@]}"; do
echo "${snmp_list[$snmp_list_task]}" >> "/tmp/snmp.autodeploy"
done && cat "/tmp/snmp.autodeploy" | sort > "/etc/snmp/snmpd.conf" && rm -rf "/tmp/snmp.autodeploy" && systemctl start snmpd && snmpwalk -v3 -a SHA -A ${SNMP_AUTH_PASS} -x AES -X ${SNMP_PRIV_PASS} -l authPriv -u ${SNMP_USER} 127.0.0.1 | head
fi
}
function ConfigureSshd() {
if [ -f "/usr/share/openssh/sshd_config" ]; then
cat "/usr/share/openssh/sshd_config" | sed "s/\#PasswordAuthentication\ yes/PasswordAuthentication\ yes/g;s/\#PermitRootLogin\ prohibit\-password/PermitRootLogin\ yes/g;s/\#Port\ 22/Port 9022/g;s/\#PubkeyAuthentication\ yes/PubkeyAuthentication\ yes/g" > "/tmp/sshd_config.autodeploy" && cat "/tmp/sshd_config.autodeploy" > "/etc/ssh/sshd_config" && rm -rf "/tmp/sshd_config.autodeploy" /etc/ssh/sshd_config.d/*
fi
}
function ConfigureSysctl() {
DISABLE_ICMP_ECHO="false"
if [ "${DISABLE_ICMP_ECHO}" == "true" ]; then
icmp_echo=(
"net.ipv4.icmp_echo_ignore_all = 1"
"net.ipv6.icmp.echo_ignore_all = 1"
)
fi
network_interface=(
"all"
"default"
$(cat "/proc/net/dev" | grep -v "docker0\|lo\|wg0" | grep "\:" | sed "s/[[:space:]]//g" | cut -d ":" -f 1 | sort | uniq | awk "{print $2}")
)
sysctl_list=(
"net.core.default_qdisc = fq_pie"
"net.core.rmem_max = 7500000"
"net.core.wmem_max = 7500000"
"net.ipv4.ip_forward = 1"
"net.ipv4.tcp_congestion_control = bbr"
"net.ipv4.tcp_fastopen = 3"
"vm.overcommit_memory = 1"
"vm.swappiness = 10"
${icmp_echo[@]}
)
which "sysctl" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
rm -rf "/tmp/sysctl.autodeploy" && for sysctl_list_task in "${!sysctl_list[@]}"; do
echo "${sysctl_list[$sysctl_list_task]}" >> "/tmp/sysctl.autodeploy"
done && for network_interface_task in "${!network_interface[@]}"; do
echo -e "net.ipv6.conf.${network_interface[$network_interface_task]}.accept_ra = 2\nnet.ipv6.conf.${network_interface[$network_interface_task]}.autoconf = 1\nnet.ipv6.conf.${network_interface[$network_interface_task]}.forwarding = 1" >> "/tmp/sysctl.autodeploy"
done && cat "/tmp/sysctl.autodeploy" | sort | uniq > "/etc/sysctl.conf" && sysctl -p && rm -rf "/tmp/sysctl.autodeploy"
fi
}
function ConfigureSystemd() {
systemd_list=(
"systemd-networkd-wait-online,disable"
)
for systemd_list_task in "${!systemd_list[@]}"; do
systemctl $(echo "${systemd_list[$systemd_list_task]}" | cut -d ',' -f 2) $(echo "${systemd_list[$systemd_list_task]}" | cut -d ',' -f 1)
done
}
function ConfigureTuned() {
which "tuned-adm" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
tuned-adm profile "$(tuned-adm recommend)" && tuned-adm active
fi
}
function ConfigureUfw() {
which "ufw" > "/dev/null" 2>&1
if [ "$?" -eq "0" ] && [ -f "/etc/default/ufw" ]; then
echo "$(cat '/etc/default/ufw' | sed 's/DEFAULT\_APPLICATION\_POLICY\=\"ACCEPT\"/DEFAULT\_APPLICATION\_POLICY\=\"REJECT\"/g;s/DEFAULT\_APPLICATION\_POLICY\=\"DROP\"/DEFAULT\_APPLICATION\_POLICY\=\"REJECT\"/g;s/DEFAULT\_APPLICATION\_POLICY\=\"SKIP\"/DEFAULT\_APPLICATION\_POLICY\=\"REJECT\"/g;s/DEFAULT\_FORWARD\_POLICY\=\"DROP\"/DEFAULT\_FORWARD\_POLICY\=\"ACCEPT\"/g;s/DEFAULT\_FORWARD\_POLICY\=\"REJECT\"/DEFAULT\_FORWARD\_POLICY\=\"ACCEPT\"/g;s/DEFAULT\_INPUT\_POLICY\=\"ACCEPT\"/DEFAULT\_INPUT\_POLICY\=\"REJECT\"/g;s/DEFAULT\_INPUT\_POLICY\=\"DROP\"/DEFAULT\_INPUT\_POLICY\=\"REJECT\"/g;s/DEFAULT\_OUTPUT\_POLICY\=\"DROP\"/DEFAULT\_OUTPUT\_POLICY\=\"ACCEPT\"/g;s/DEFAULT\_OUTPUT\_POLICY\=\"REJECT\"/DEFAULT\_OUTPUT\_POLICY\=\"ACCEPT\"/g;s/MANAGE\_BUILTINS\=yes/MANAGE\_BUILTINS\=no/g;s/IPV6\=no/IPV6\=yes/g')" > "/tmp/ufw.autodeploy" && cat "/tmp/ufw.autodeploy" > "/etc/default/ufw" && rm -rf "/tmp/ufw.autodeploy"
ufw reload && ufw reset && ufw allow 123/udp && ufw allow 161/udp && ufw limit 22/tcp && ufw allow 323/udp && ufw allow 3493/tcp && ufw allow 51820/udp && ufw limit 9022/tcp && ufw allow 9090/tcp && ufw allow in on wg0 && ufw enable && ufw status verbose
fi
}
function ConfigureWireGuard() {
ENABLE_IPV6_ADDRESS="false"
TUNNEL_CLIENT_V4="10.172.$(shuf -i '0-255' -n 1).$(shuf -i '0-255' -n 1)/32"
if [ "${ENABLE_IPV6_ADDRESS:-false}" == "true" ]; then
which "bc" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
which "sha1sum" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
which "uuidgen" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
UNIQUE_CLIENT=$(echo "obase=16;$(shuf -i '1-65535' -n 1)" | bc | tr "A-Z" "a-z")
UNIQUE_PREFIX=$(echo $(date "+%s%N")$(uuidgen | tr -d "-" | tr "A-Z" "a-z") | sha1sum | cut -c 31-)
TUNNEL_PREFIX="fd$(echo ${UNIQUE_PREFIX} | cut -c 1-2):$(echo ${UNIQUE_PREFIX} | cut -c 3-6):$(echo ${UNIQUE_PREFIX} | cut -c 7-10)"
TUNNEL_CLIENT_V6="${TUNNEL_PREFIX}::${UNIQUE_CLIENT}/128"
else
TUNNEL_CLIENT_V6=""
fi
fi
fi
TUNNEL_CLIENT_V6=", ${TUNNEL_CLIENT_V6}"
fi
if [ ! -d "/etc/wireguard" ]; then
mkdir "/etc/wireguard"
else
rm -rf /etc/wireguard/*
fi
which "wg" > "/dev/null" 2>&1
if [ "$?" -eq "0" ]; then
wireguard_list=(
"[Interface]"
"Address = ${TUNNEL_CLIENT_V4}${TUNNEL_CLIENT_V6}"
"# DNS = 127.0.0.1, ::1"
"ListenPort = 51820"
"MTU = 1280"
"PrivateKey = $(wg genkey | tee '/tmp/wireguard.autodeploy')"
"# [Peer]"
"# AllowedIPs = ${TUNNEL_CLIENT_V4}${TUNNEL_CLIENT_V6}"
"# Endpoint = 127.0.0.1:51820"
"# PersistentKeepalive = 5"
"# PresharedKey = $(wg genpsk)"
"# PublicKey = $(cat '/tmp/wireguard.autodeploy' | wg pubkey)"
)
rm -rf "/tmp/wireguard.autodeploy" && for wireguard_list_task in "${!wireguard_list[@]}"; do