Skip to content

Instantly share code, notes, and snippets.

@tazjel
Created December 27, 2012 09:50
Show Gist options
  • Save tazjel/4386985 to your computer and use it in GitHub Desktop.
Save tazjel/4386985 to your computer and use it in GitHub Desktop.

Initial setup for ubuntu server

Update and Upgrade

sudo aptitude update && sudo aptitude safe-upgrade

User Setup

Create new user:

adduser <your username>

Add the user to the sudoers group:

usermod -a -G sudo <your username>

SSH Setup

Create key on local machine:

ssh-keygen -t rsa -C "<your username>@<your username>.org"

Copy key:

scp ~/.ssh/id_rsa.pub newuser@<server-ip>:~/

Apply key:

mkdir .ssh
mv id_rsa.pub .ssh/authorized_keys
chown -R <your username>:<your username> .ssh
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

Config sshd:

sudo vim /etc/ssh/sshd_config

Set "Port 869"

Set "PermitRootLogin no"

sudo service ssh restart

Connect with key on selected port:

ssh -p 869 newuser@<server-ip>

Secure the ssh:

sudo aptitude install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo vim /etc/fail2ban/jail.local

Set "enabled" to "true" in [ssh-ddos] section

Set "port" to "869" in [ssh] and [ssh-ddos] sections

sudo service fail2ban restart

Security

Iptables

sudo iptables -L
sudo vim /etc/iptables.firewall.rules

Add to file:


# file: /etc/iptables.firewall.rules

*filter

#  Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

#  Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

#  Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allow ports for testing
-A INPUT -p tcp --dport 8080:8090 -j ACCEPT

#  Allow ports for MOSH (mobile shell)
-A INPUT -p udp --dport 60000:61000 -j ACCEPT

#  Allow SSH connections
#  The -dport number should be the same port number you set in sshd_config
-A INPUT -p tcp -m state --state NEW --dport 869 -j ACCEPT

#  Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

#  Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

#  Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT

sudo iptables-restore < /etc/iptables.firewall.rules
sudo iptables -L
sudo vim /etc/network/if-pre-up.d/firewall

Add to file:

#!/bin/sh
/sbin/iptables-restore < /etc/iptables.firewall.rules

Set permission:

sudo chmod +x /etc/network/if-pre-up.d/firewall

Get an email anytime a user uses sudo

sudo aptitude install sendmail
sudo  vim /etc/sudoers.d/my_sudoers

Defaults mail_always

Defaults mailto="user@email"

sudo chmod 0440 /etc/sudoers.d/my_sudoers

FIXME: Time zone - related to server lozation? tzdata

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