Results 1 to 3 of 3
-
08-14-2007, 02:32 PM #1WHT Addict
- Join Date
- Feb 2004
- Posts
- 162
HOWTO: Debian 4.0 Base installation and configuration
Hello! I thought it might be nice to post some tutorials written for VPS's by staff at Spry. Normally we just keep all this content in our Wiki/Blog/Forums, but thought we'd see how you all like it. Let me know if you want to see more articles like this posted here! This is my first one, so let me know any feedback you have about the content.
Original Article at VPSLink
By: Paul Tomes
Scenario
You've chosen Debian 4.0 Etch as your VPS operating system and you want complete control over what is installed and you want control of the security of your server from the very start.
Aim
To take you from a brand new (or reinstalled) Debian 4.0 Etch VPS to a secure working environment in as short a time as possible. SSH will be locked down and a basic firewall constructed closing all ports not in use.
Steps
Firstly, log into your Control panel at https://cp.vpslink.com.
Select the relevant server and follow the links as shown below to install Debian 4.0 Etch:
Manage Server -> Manage OS -> Install OS -> Select Debian 4.0 "Etch" -> confirm change
Wait for the process to complete.
Of course if you do not have a VPSLink account, the method is the same for whatever host you are using.
If this is a VPS reinstall you will need to delete the entry for the IP address in your known_hosts file. This is located on your LOCAL workstation.
In this example, I used nano as my text editor:
Code:nano ~/.ssh/known_hosts
Log in
Once you have deleted the old entry for your IP address, log in to your VPS:
Code:ssh root@123.45.67.890
You can change this (HIGHLY recommended):
Manage Server -> Change Root Password -> enter new password
Add user
Add your main user. In this example, the username is 'onion':
Code:adduser onion
Sudo
At the moment we are logged in as the root user. This is only for a very short time and will never happen again. As such, we need to give the main user 'sudo' privileges which will enable them to administer the VPS without the security risk of allowing the root user to log in.
Code:aptitude install sudo
Now give the main user sudo privileges:
Code:visudo
Code:onion ALL=(ALL) ALL
Create a 'hidden' file in the new user's home directory:
Code:mkdir /home/onion/.ssh
Code:scp -2 /home/localuser/.ssh/id_rsa.pub root@123.45.67.890:/home/onion/.ssh/authorized_keys
Change permissions on the ssh directory so that it is not readable by anyone else (although this is only the public key and be no use to anyone else unless they have your private key which is located on your LOCAL workstation):
Code:chown -R onion:onion /home/onion/.ssh/ chmod go-w /home/onion/ chmod 700 /home/onion/.ssh chmod 600 /home/onion/.ssh/authorized_keys
Configuring SSH is the next step. Open /etc/ssh/sshd_config in your favorite text editor:
Code:nano /etc/ssh/sshd_config
Code:Port 30000 # change to a port of your choosing PermitRootLogin no PasswordAuthentication no # ONLY change if using authorized_keys as above X11Forwarding no UsePAM no UseDNS no AllowUsers onion
Next, reload SSH so the changes are affected:
Code:/etc/init.d/ssh reload
Don't log out yet - we need to test the new configuration!
Open a new connection from your LOCAL workstation and log in as the new user using the new port number:
Code:ssh -p 30000 onion@123.45.67.890
Code:exit
iptables
Now we are some way to have a secure base to work from.
Now we can create a very simple firewall using iptables. This will block all unused portsand log any unauthorized attempts to log into the VPS:
Code:sudo -i
Code:iptables-save > /etc/iptables.up.rules
Code:iptables -L
Code:Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
New iptables rules
Let's tighten that up a bit by creating a test iptables file:
Code:nano /etc/iptables.test.rules
Code:<pre> *filter # Allows 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 # Accepts all established inbound connections -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allows all outbound traffic # You could modify this to only allow certain traffic -A OUTPUT -j ACCEPT # Allows HTTP and HTTPS connections from anywhere (the normal ports for websites) -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # Allows SSH connections # THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE -A INPUT -p tcp -m state --state NEW --dport 30000 -j ACCEPT # Allow ping -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT # log iptables denied calls (access via 'dmesg' command) -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 </pre>
Activate these new rules:
Code:iptables-restore < /etc/iptables.test.rules
Code:iptables -L
Once you are happy, save the new rules to the master iptables file:
Code:iptables-save > /etc/iptables.up.rules
Don't log out!!!
You will need a working connection in case we messed up the firewall somehow - once locked out there is no way back in.
Open new terminal and try and log in now the basic firewall rules are there:
Code:ssh -p 30000 onion@123.45.67.890
reboot configuration
To make sure the iptables rules are started on a reboot we'll create a new file:
Code:nano /etc/network/if-pre-up.d/iptables
Code:#!/bin/bash /sbin/iptables-restore < /etc/iptables.up.rules
Code:chmod +x /etc/network/if-pre-up.d/iptables
Finally, type in:
Code:exit
If you want to test the firewall rules on a reboot then go ahead (this may be an good idea as you don't want to find out later that it didn't work due to a simple typing error)
Log into your Control panel at https://cp.vpslink.com.
Manage Server -> Reboot Server
Once done, log in again in the usual manner:
Code:ssh -p 30000 onion@123.45.67.890
Seems like a lot of work and it may seem that not a lot has happened.
However, once you are happy with what you are doing this only takes 2 or 3 minutes at the most and a great deal has happened.
SSH has been locked down very tightly allowing for a much more secure log in system and we have the start of a good firewall system with the iptables configuration. Sure it's basic right now but it does the job of blocking ports that are not being used.
-
09-11-2007, 03:02 PM #2Over there
- Join Date
- Oct 2004
- Location
- LA, CA
- Posts
- 1,069
When I try and restore those IPtables rules, I get this:
iptables-restore v1.3.6: no command specified
Error occurred at line: 23
-
09-11-2007, 06:53 PM #3Over there
- Join Date
- Oct 2004
- Location
- LA, CA
- Posts
- 1,069
Nevermind, fixed the error I think. I used the whole IPtables file except I replace the old filter with that new filter.
By the way, very nice guide. Thanks!Last edited by That Guy; 09-11-2007 at 06:59 PM.