Skip to content

Instantly share code, notes, and snippets.

@johnantoni
Created April 18, 2012 13:32
Show Gist options
  • Select an option

  • Save johnantoni/2413580 to your computer and use it in GitHub Desktop.

Select an option

Save johnantoni/2413580 to your computer and use it in GitHub Desktop.
Linode Stack Script - Security by Xenofox
#!/bin/bash
#
# Security StackScript
# By Donald von Stufft <donald.stufft@gmail.com>
#
# <udf name=&quot;user_name&quot; label=&quot;Unprivileged User Account&quot; />
# <udf name=&quot;user_password&quot; label=&quot;Unprivileged User Password&quot; />
# <udf name=&quot;user_sshkey&quot; label=&quot;Public Key for User&quot; default=&quot;&quot; />
#
# <udf name=&quot;sshd_port&quot; label=&quot;SSH Port&quot; default=&quot;22&quot; />
# <udf name=&quot;sshd_protocol&quot; label=&quot;SSH Protocol&quot; oneOf=&quot;1,2,1 and 2&quot; default=&quot;2&quot; />
# <udf name=&quot;sshd_permitroot&quot; label=&quot;SSH Permit Root Login&quot; oneof=&quot;No,Yes&quot; default=&quot;No&quot; />
# <udf name=&quot;sshd_passwordauth&quot; label=&quot;SSH Password Authentication&quot; oneOf=&quot;No,Yes&quot; default=&quot;No&quot; />
# <udf name=&quot;sshd_group&quot; label=&quot;SSH Allowed Groups&quot; default=&quot;sshusers&quot; example=&quot;List of groups seperated by spaces&quot; />
#
# <udf name=&quot;sudo_usergroup&quot; label=&quot;Usergroup to use for Admin Accounts&quot; default=&quot;wheel&quot; />
# <udf name=&quot;sudo_passwordless&quot; label=&quot;Passwordless Sudo&quot; oneof=&quot;Require Password,Do Not Require Password&quot;, default=&quot;Require Password&quot; />
source <ssinclude StackScriptID=&quot;1&quot;>
# Update the System
system_update
# Install and Configure Sudo
aptitude -y install sudo
cp /etc/sudoers /etc/sudoers.tmp
chmod 0640 /etc/sudoers.tmp
test &quot;${SUDO_PASSWORDLESS}&quot; == &quot;Do Not Require Password&quot; && (echo &quot;%`echo ${SUDO_USERGROUP} | tr '[:upper:]' '[:lower:]'` ALL = NOPASSWD: ALL&quot; >> /etc/sudoers.tmp)
test &quot;${SUDO_PASSWORDLESS}&quot; == &quot;Require Password&quot; && (echo &quot;%`echo ${SUDO_USERGROUP} | tr '[:upper:]' '[:lower:]'` ALL = (ALL) ALL&quot; >> /etc/sudoers.tmp)
chmod 0440 /etc/sudoers.tmp
mv /etc/sudoers.tmp /etc/sudoers
# Configure SSHD
echo &quot;Port ${SSHD_PORT}&quot; > /etc/ssh/sshd_config.tmp
echo &quot;Protocol ${SSHD_PROTOCOL}&quot; >> /etc/ssh/sshd_config.tmp
sed -n 's/\(HostKey .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(UsePrivilegeSeparation .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(KeyRegenerationInterval .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(ServerKeyBits .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(SyslogFacility .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(LogLevel .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(LoginGraceTime .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
echo &quot;PermitRootLogin `echo ${SSHD_PERMITROOT} | tr '[:upper:]' '[:lower:]'`&quot; >> /etc/ssh/sshd_config.tmp
sed -n 's/\(StrictModes .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(RSAAuthentication .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(PubkeyAuthentication .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(IgnoreRhosts .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(RhostsRSAAuthentication .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(HostbasedAuthentication .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(PermitEmptyPasswords .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(ChallengeResponseAuthentication .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
echo &quot;PasswordAuthentication `echo ${SSHD_PASSWORDAUTH} | tr '[:upper:]' '[:lower:]'`&quot; >> /etc/ssh/sshd_config.tmp
sed -n 's/\(X11Forwarding .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(X11DisplayOffset .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(PrintMotd .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(PrintLastLog .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(TCPKeepAlive .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(MaxStartups .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(AcceptEnv .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(Subsystem .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
sed -n 's/\(UsePAM .*\)/\1/p' < /etc/ssh/sshd_config >> /etc/ssh/sshd_config.tmp
echo &quot;AllowGroups `echo ${SSHD_GROUP} | tr '[:upper:]' '[:lower:]'`&quot; >> /etc/ssh/sshd_config.tmp
chmod 0600 /etc/ssh/sshd_config.tmp
mv /etc/ssh/sshd_config.tmp /etc/ssh/sshd_config
touch /tmp/restart-ssh
# Create Groups
groupadd ${SSHD_GROUP}
groupadd ${SUDO_USERGROUP}
# Create User & Add SSH Key
USER_NAME_LOWER=`echo ${USER_NAME} | tr '[:upper:]' '[:lower:]'`
useradd -m -s /bin/bash -G ${SSHD_GROUP},${SUDO_USERGROUP} ${USER_NAME_LOWER}
echo &quot;${USER_NAME_LOWER}:${USER_PASSWORD}&quot; | chpasswd
USER_HOME=`sed -n &quot;s/${USER_NAME_LOWER}:x:[0-9]*:[0-9]*:[^:]*:\(.*\):.*/\1/p&quot; < /etc/passwd`
sudo -u ${USER_NAME_LOWER} mkdir ${USER_HOME}/.ssh
echo &quot;${USER_SSHKEY}&quot; >> $USER_HOME/.ssh/authorized_keys
chmod 0600 $USER_HOME/.ssh/authorized_keys
chown ${USER_NAME_LOWER}:${USER_NAME_LOWER} $USER_HOME/.ssh/authorized_keys
# Setup Hostname
get_rdns_primary_ip > /etc/hostname
/etc/init.d/hostname.sh start
# Restart Services
restartServices
@johnantoni

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment