|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +if [ $EUID != 0 ]; then |
| 4 | + sudo "$0" "$@" |
| 5 | + exit $? |
| 6 | +fi |
| 7 | + |
| 8 | + |
| 9 | +temp_dir=.cac-setup |
| 10 | +os_ubuntu=$temp_dir/ubuntu |
| 11 | +os_centos=$temp_dir/centos |
| 12 | +packman_apt=$temp_dir/apt-get |
| 13 | +packman_yum=$temp_dir/yum |
| 14 | + |
| 15 | +create_temp_directory() { |
| 16 | + |
| 17 | + if [ ! -d $temp_dir ]; then |
| 18 | + |
| 19 | + mkdir $temp_dir |
| 20 | + |
| 21 | + if grep -i -q "ubuntu" /etc/os-release; then |
| 22 | + touch $os_ubuntu |
| 23 | + elif grep -i -q "centos" /etc/os-release; then |
| 24 | + touch $os_centos |
| 25 | + else |
| 26 | + echo "Unsupported OS" |
| 27 | + fi |
| 28 | + |
| 29 | + if [ -n "$(command -v apt-get)" ]; then |
| 30 | + touch $packman_apt |
| 31 | + elif [ -n "$(command -v yum)" ]; then |
| 32 | + touch $packman_yum |
| 33 | + else |
| 34 | + echo "Unsupported Package Manager" |
| 35 | + fi |
| 36 | + |
| 37 | + fi |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +clear_temp_directory() { |
| 42 | + rm -rf $temp_dir |
| 43 | +} |
| 44 | + |
| 45 | +prepare_system() { |
| 46 | + |
| 47 | + if [ ! -f $os_centos ]; then |
| 48 | + |
| 49 | + # http://utdream.org/post.cfm/yum-couldn-t-resolve-host-mirrorlist-centos-org-for-centos-6 |
| 50 | + echo "nameserver 8.8.8.8" >> /etc/resolv.conf |
| 51 | + echo "nameserver 8.8.4.4" >> /etc/resolv.conf |
| 52 | + echo "nameserver 127.0.0.1" >> /etc/resolv.conf |
| 53 | + |
| 54 | + fi |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +install_prerequisites() { |
| 59 | + |
| 60 | + if [ -f $packman_yum ]; then |
| 61 | + |
| 62 | + yum install yum-utils policycoreutils-python vim-common -y |
| 63 | + |
| 64 | + fi |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +upgrade_machine() { |
| 69 | + if [ -f $packman_apt ]; then |
| 70 | + apt-get update |
| 71 | + apt-get upgrade -y |
| 72 | + apt-get dist-upgrade -y |
| 73 | + apt-get autoremove -y |
| 74 | + elif [ -f $packman_yum ]; then |
| 75 | + yum update -y |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +update_machine_name() { |
| 80 | + echo "machine name [jeric]:" |
| 81 | + read machine_name |
| 82 | + |
| 83 | + if [[ -z "${machine_name// }" ]]; then |
| 84 | + machine_name=jeric |
| 85 | + fi |
| 86 | + |
| 87 | + if [ -f $os_ubuntu ]; then |
| 88 | + sed -i s/ubuntu/$machine_name/ /etc/hosts |
| 89 | + sed -i s/ubuntu/$machine_name/ /etc/hostname |
| 90 | + hostname $machine_name |
| 91 | + elif [ -f $os_centos ]; then |
| 92 | + hostnamectl set-hostname $machine_name |
| 93 | + fi |
| 94 | +} |
| 95 | + |
| 96 | +create_new_account() { |
| 97 | + |
| 98 | + echo "username:" |
| 99 | + read new_account |
| 100 | + echo "password:" |
| 101 | + read -s new_account_password |
| 102 | + |
| 103 | + # account update |
| 104 | + echo "change root password" |
| 105 | + echo "root:$new_account_password" | chpasswd |
| 106 | + echo "deleting default user" |
| 107 | + if [ -f $os_ubuntu ]; then |
| 108 | + deluser --remove-home user |
| 109 | + elif [ -f $os_centos ]; then |
| 110 | + userdel -r user |
| 111 | + fi |
| 112 | + |
| 113 | + |
| 114 | + echo "creating new user $new_account" |
| 115 | + if [ -f $os_ubuntu ]; then |
| 116 | + adduser --quiet --disabled-password --gecos "" $new_account |
| 117 | + elif [ -f $os_centos ]; then |
| 118 | + adduser $new_account |
| 119 | + fi |
| 120 | + |
| 121 | + echo "setting password for $new_account" |
| 122 | + echo "$new_account:$new_account_password" | chpasswd |
| 123 | + if [ -f $os_ubuntu ]; then |
| 124 | + adduser $new_account sudo |
| 125 | + elif [ -f $os_centos ]; then |
| 126 | + gpasswd -a $new_account wheel |
| 127 | + fi |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +cleanup_old_kernels__ubuntu() { |
| 132 | + |
| 133 | + mapfile -t kernels < <(dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r) | awk '{print $2}' | sed s/-generic//) |
| 134 | + |
| 135 | + for kernel in "${kernels[@]}" |
| 136 | + do |
| 137 | + echo "==========================================" |
| 138 | + echo "removing $kernel" |
| 139 | + echo "==========================================" |
| 140 | + sudo dpkg --purge $kernel-generic |
| 141 | + sudo dpkg --purge $kernel-header $kernel |
| 142 | + done |
| 143 | + |
| 144 | + |
| 145 | + echo |
| 146 | + echo "==========================================" |
| 147 | + echo "deleting old linux images from boot partition..." |
| 148 | + echo "==========================================" |
| 149 | + echo |
| 150 | + |
| 151 | + ls /boot | grep "\-generic" | grep -Fv $(uname -r) | awk '{print "/boot/" $1}' | xargs rm |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +cleanup_old_kernels__centos() { |
| 156 | + package-cleanup --oldkernels --count=1 -y |
| 157 | +} |
| 158 | + |
| 159 | +cleanup_old_kernels() { |
| 160 | + |
| 161 | + # clean up old kernels |
| 162 | + |
| 163 | + echo |
| 164 | + echo "==========================================" |
| 165 | + echo "cleaning up old kernels..." |
| 166 | + echo "==========================================" |
| 167 | + echo |
| 168 | + |
| 169 | + if [ -f $os_ubuntu ]; then |
| 170 | + cleanup_old_kernels__ubuntu |
| 171 | + elif [ -f $os_centos ]; then |
| 172 | + cleanup_old_kernels__centos |
| 173 | + fi |
| 174 | + |
| 175 | +} |
| 176 | + |
| 177 | +regenerate_ssh_server_keys() { |
| 178 | + mapfile -t ssh_key_types < <(ls -l /etc/ssh | grep .pub | awk '{print $9}' | sed -r 's/ssh_host_([a-zA-Z0-9]+)_key.pub/\1/') |
| 179 | + |
| 180 | + echo "new ssh server keys:" |
| 181 | + |
| 182 | + for ssh_key_type in "${ssh_key_types[@]}" |
| 183 | + do |
| 184 | + rm /etc/ssh/ssh_host_"$ssh_key_type"_key |
| 185 | + rm /etc/ssh/ssh_host_"$ssh_key_type"_key.pub |
| 186 | + |
| 187 | + ssh-keygen -q -N "" -t $ssh_key_type -f /etc/ssh/ssh_host_"$ssh_key_type"_key |
| 188 | + |
| 189 | + echo |
| 190 | + echo $ssh_key_type | awk '{print toupper($1)}' |
| 191 | + if [ -f $os_ubuntu ]; then |
| 192 | + ssh-keygen -E sha256 -lf /etc/ssh/ssh_host_"$ssh_key_type"_key |
| 193 | + ssh-keygen -E md5 -lf /etc/ssh/ssh_host_"$ssh_key_type"_key |
| 194 | + elif [ -f $os_centos ]; then |
| 195 | + awk '{print $2}' /etc/ssh/ssh_host_"$ssh_key_type"_key.pub | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64 |
| 196 | + ssh-keygen -lf /etc/ssh/ssh_host_"$ssh_key_type"_key |
| 197 | + fi |
| 198 | + |
| 199 | + done |
| 200 | +} |
| 201 | + |
| 202 | +configure_ssh() { |
| 203 | + |
| 204 | + echo |
| 205 | + echo "==========================================" |
| 206 | + echo "Configuring SSH..." |
| 207 | + echo "==========================================" |
| 208 | + echo |
| 209 | + |
| 210 | + echo "disabling root login..." |
| 211 | + sed -i "s/#PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config |
| 212 | + sed -i "s/#PermitRootLogin no/PermitRootLogin no/" /etc/ssh/sshd_config |
| 213 | + sed -i "s/PermitRootLogin yes/PermitRootLogin no/" /etc/ssh/sshd_config |
| 214 | + |
| 215 | + echo "new ssh port [22]:" |
| 216 | + read ssh_port |
| 217 | + |
| 218 | + if [[ -z "${ssh_port// }" ]]; then |
| 219 | + ssh_port=22 |
| 220 | + fi |
| 221 | + |
| 222 | + sed -i "s/#Port 22/Port 22/" /etc/ssh/sshd_config |
| 223 | + sed -i "s/Port 22/Port $ssh_port/" /etc/ssh/sshd_config |
| 224 | + |
| 225 | + if [ -f $os_centos ]; then |
| 226 | + echo "running semanage..." |
| 227 | + semanage port -a -t ssh_port_t -p tcp $ssh_port |
| 228 | + fi |
| 229 | + |
| 230 | + echo "creating new ssh server keys..." |
| 231 | + regenerate_ssh_server_keys |
| 232 | + |
| 233 | + |
| 234 | +} |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | +create_temp_directory |
| 239 | +prepare_system |
| 240 | + |
| 241 | +if [ ! -f $temp_dir/kernel_remove_ready ]; then |
| 242 | + |
| 243 | + install_prerequisites |
| 244 | + |
| 245 | + echo "==========================================" |
| 246 | + echo "basic information" |
| 247 | + echo "==========================================" |
| 248 | + echo |
| 249 | + |
| 250 | + update_machine_name |
| 251 | + |
| 252 | + create_new_account |
| 253 | + |
| 254 | + echo |
| 255 | + echo "==========================================" |
| 256 | + echo "upgrading to latest version before doing a release upgrade..." |
| 257 | + echo "==========================================" |
| 258 | + echo |
| 259 | + |
| 260 | + # upgrade everything before release upgrade |
| 261 | + upgrade_machine |
| 262 | + |
| 263 | + touch $temp_dir/kernel_remove_ready |
| 264 | + echo "press any key to reboot machine, rerun script after rebooting" |
| 265 | + read confirm_key |
| 266 | + reboot -h now |
| 267 | + exit |
| 268 | + |
| 269 | +fi |
| 270 | + |
| 271 | + |
| 272 | +if [ ! -f $temp_dir/release_upgrade_done ]; then |
| 273 | + |
| 274 | + cleanup_old_kernels |
| 275 | + |
| 276 | + if [ -f $os_ubuntu ]; then |
| 277 | + # manually select upgrade options (important) |
| 278 | + |
| 279 | + echo |
| 280 | + echo "==========================================" |
| 281 | + echo "begin release upgrade..." |
| 282 | + echo "==========================================" |
| 283 | + echo |
| 284 | + |
| 285 | + do-release-upgrade |
| 286 | + |
| 287 | + touch $temp_dir/release_upgrade_done |
| 288 | + echo "press any key to reboot machine, rerun script after rebooting" |
| 289 | + read confirm_key |
| 290 | + reboot -h now |
| 291 | + exit |
| 292 | + fi |
| 293 | + |
| 294 | + touch $temp_dir/release_upgrade_done |
| 295 | + |
| 296 | +fi |
| 297 | + |
| 298 | + |
| 299 | +if [ -f $os_ubuntu ]; then |
| 300 | + |
| 301 | + cleanup_old_kernels |
| 302 | + |
| 303 | + # check for newer updates |
| 304 | + |
| 305 | + echo |
| 306 | + echo "==========================================" |
| 307 | + echo "check for further updates..." |
| 308 | + echo "==========================================" |
| 309 | + echo |
| 310 | + |
| 311 | + upgrade_machine |
| 312 | + |
| 313 | + echo |
| 314 | + echo "==========================================" |
| 315 | + echo "cleaning up temporary files..." |
| 316 | + echo "==========================================" |
| 317 | + echo |
| 318 | + |
| 319 | +fi |
| 320 | + |
| 321 | + |
| 322 | +clear_temp_directory |
| 323 | + |
| 324 | +configure_ssh |
| 325 | + |
| 326 | +echo |
| 327 | +echo "==========================================" |
| 328 | +echo "System is Ready" |
| 329 | +echo "==========================================" |
| 330 | +echo |
| 331 | +echo "press any key to reboot machine" |
| 332 | +read confirm_key |
| 333 | +reboot -h now |
| 334 | + |
| 335 | + |
0 commit comments