-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path123.sh
More file actions
131 lines (107 loc) · 3.04 KB
/
123.sh
File metadata and controls
131 lines (107 loc) · 3.04 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
#!/bin/bash
# System related utilities
# My ref: http://www.linode.com/?r=aadfce9845055011e00f0c6c9a5c01158c452deb
# Thanks!
function lower {
# helper function
echo $1 | tr '[:upper:]' '[:lower:]'
}
function system_get_codename {
echo `lsb_release -sc`
}
function system_get_release {
echo `lsb_release -sr`
}
function system_add_user {
# $1 - username
# $2 - password
# $3 - groups
USERNAME=`lower $1`
PASSWORD=$2
SUDO_GROUP=$3
SHELL="/bin/bash"
useradd --create-home --shell "$SHELL" --user-group --groups "$SUDO_GROUP" "$USERNAME"
echo "$USERNAME:$PASSWORD" | chpasswd
}
function system_add_system_user {
# $1 - username
# $2 - home
USERNAME=`lower $1`
HOME_DIR=$2
if [ -z "$HOME_DIR" ]; then
useradd --system --no-create-home --user-group $USERNAME
else
useradd --system --no-create-home --home-dir "$HOME_DIR" --user-group $USERNAME
fi;
}
function system_get_user_home {
# $1 - username
cat /etc/passwd | grep "^$1:" | cut --delimiter=":" -f6
}
function system_user_add_ssh_key {
# $1 - username
# $2 - ssh key
USERNAME=`lower $1`
USER_HOME=`system_get_user_home "$USERNAME"`
sudo -u "$USERNAME" mkdir "$USER_HOME/.ssh"
sudo -u "$USERNAME" touch "$USER_HOME/.ssh/authorized_keys"
sudo -u "$USERNAME" echo "$2" >> "$USER_HOME/.ssh/authorized_keys"
chmod 0600 "$USER_HOME/.ssh/authorized_keys"
}
function system_sshd_edit_bool {
# $1 - param name
# $2 - Yes/No
VALUE=`lower $2`
if [ "$VALUE" == "yes" ] || [ "$VALUE" == "no" ]; then
sed -i "s/^#*\($1\).*/\1 $VALUE/" /etc/ssh/sshd_config
fi
}
function system_sshd_permitrootlogin {
system_sshd_edit_bool "PermitRootLogin" "$1"
}
function system_sshd_passwordauthentication {
system_sshd_edit_bool "PasswordAuthentication" "$1"
}
function system_sshd_pubkeyauthentication {
system_sshd_edit_bool "PubkeyAuthentication" "$1"
}
function system_sshd_passwordauthentication {
system_sshd_edit_bool "PasswordAuthentication" "$1"
}
function system_enable_universe {
sed -i 's/^#\(.*deb.*\) universe/\1 universe/' /etc/apt/sources.list
aptitude update
}
function system_update_locale_en_US_UTF_8 {
# locale-gen en_US.UTF-8
dpkg-reconfigure locales
update-locale LANG=en_US.UTF-8
}
function system_update_hostname {
# $1 - system hostname
if [ ! -n "$1" ]; then
echo "system_update_hostname() requires the system hostname as its first argument"
return 1;
fi
echo $1 > /etc/hostname
hostname -F /etc/hostname
echo -e "\n127.0.0.1 $1.local $1\n" >> /etc/hosts
}
function system_security_fail2ban {
aptitude -y install fail2ban
}
function system_security_ufw_install {
aptitude -y install ufw
}
function system_security_ufw_configure_basic {
# see https://help.ubuntu.com/community/UFW
ufw logging on
ufw default deny
ufw allow ssh
ufw allow http
ufw allow https
ufw enable
}
function system_security_logcheck {
aptitude -y install logcheck logcheck-database
}