@@ -65,16 +65,33 @@ su - myusername
6565sudo whoami # root
6666```
6767
68+ Configure time synchronization:
69+
70+ ``` sh
71+ sudo apt install ntp
72+ sudo timedatectl set-timezone Europe/Berlin # adjust accordingly
73+ sudo timedatectl set-ntp true
74+ timedatectl status
75+ ```
76+
77+ Enforce journald limits by editing ` /etc/systemd/journald.conf ` and set:
78+
79+ ``` txt
80+ SystemMaxUse=500M
81+ SystemMaxFileSize=100M
82+ MaxRetentionSec=30day
83+ ```
84+
6885#### SSH
6986
70- On your local machine generate SSH keys :
87+ Generate SSH Keys (on your local machine) :
7188
7289``` sh
73- ssh-keygen -t ed25519 -C " email@ddress .com"
90+ ssh-keygen -t ed25519 -C " email@address .com"
7491cat ~ /.ssh/id_ed25519.pub
7592```
7693
77- On the server (as your new user, not root):
94+ Add Your Public Key to the Server (as your new user, not root)
7895
7996``` sh
8097mkdir -p ~ /.ssh
@@ -83,83 +100,151 @@ nano ~/.ssh/authorized_keys # paste your public key here
83100chmod 600 ~ /.ssh/authorized_keys
84101```
85102
86- Test login:
103+ Test SSH login:
87104
88105``` sh
89106ssh myusername@vps-ip
90107```
91108
92- Edit SSH configuration : ` sudo nano /etc/ssh/sshd_config ` and add/edit these lines:
109+ Edit SSH settings : ` sudo nano /etc/ssh/sshd_config ` and ensure the following lines are set (uncomment or add if needed) :
93110
94111``` txt
95112PasswordAuthentication no
96113PubkeyAuthentication yes
97114```
98115
99- Check if ` /etc/ssh/sshd_config.d/50-cloud-init.conf ` exists with ` sudo nano /etc/ssh/sshd_config.d/50-cloud-init.conf ` and add/edit this line :
116+ Also check if ` /etc/ssh/sshd_config.d/50-cloud-init.conf ` exists and add:
100117
101118``` txt
102119PasswordAuthentication no
103120```
104121
105- Test and restart sshd :
122+ Test and restart the SSH daemon :
106123
107- ``` sh
124+ ``` sh
108125sudo sshd -t
109126sudo systemctl restart ssh
110127sudo systemctl status ssh
111128```
112129
113- Disable root login by editing ` sudo nano /etc/ssh/sshd_config ` and add/edit this line:
130+ Disable Root Login
131+
132+ Edit ` /etc/ssh/sshd_config ` again and add or edit this line:
114133
115134``` txt
116135PermitRootLogin no
117136```
118137
119- restart sshd :
138+ Restart SSH :
120139
121140``` sh
122141sudo systemctl restart ssh
123142```
124143
125- Test ssh login from a different terminal (result should be "Permission denied" ):
144+ Test in a new terminal (should fail ):
126145
127146``` sh
128147ssh root@vps-ip
129148```
130149
131150#### Firewall
132151
133- Using UFW (Uncomplicated Firewall):
152+ Initial Setup
134153
135- ``` sh
154+ ``` sh
136155sudo ufw status
137156sudo ufw default deny incoming
138157sudo ufw default allow outgoing
139- sudo ufw allow ssh # Allow SSH before enabling firewall
140- sudo ufw allow 80/tcp # Allow HTTP
141- sudo ufw allow 443/tcp # Allow HTTPS
158+ sudo ufw allow 22/tcp # Allow SSH (default)
159+ sudo ufw allow 80/tcp # Allow HTTP
160+ sudo ufw allow 443/tcp # Allow HTTPS
142161```
143162
144- Enable firewall and type 'y' when prompted :
163+ Enable UFW and verify :
145164
146165``` sh
147166sudo ufw enable
148167sudo ufw status verbose
149168```
150169
151- Change default SSH port :
170+ Change SSH Port :
152171
153172``` sh
154- sudo nano /etc/ssh/sshd_config
173+ sudo ufw allow 666/tcp
174+ ```
175+
176+ Edit SSH configuration ` /etc/ssh/sshd_config ` and find or add:
177+
178+ ``` txt
179+ Port 666
155180```
156181
182+ Test and restart SSH:
183+
184+ ``` sh
185+ sudo sshd -t
186+ sudo systemctl restart ssh
187+ ```
188+
189+ From a new terminal, test the new connection:
190+
191+ ``` sh
192+ ssh -p 666 myusername@vps-ip
193+ ```
194+
195+ Do not close your old SSH session until this test works.
196+
197+ Once confirmed, remove the old SSH port rule:
198+
157199``` sh
158- sudo ufw allow 666/tcp # changed Port 22 to Port 666
159200sudo ufw delete allow 22/tcp
160201sudo systemctl restart ssh
161202```
162203
204+ Verify the final rules:
205+
206+ ``` sh
207+ sudo ufw status numbered
208+ ```
209+
210+ Result should look like this:
211+
212+ | Port | Service | Purpose |
213+ | ------- | ------------ | --------------------- |
214+ | 666/tcp | SSH (custom) | Secure shell access |
215+ | 80/tcp | HTTP | Web traffic |
216+ | 443/tcp | HTTPS | Encrypted web traffic |
217+
218+ #### Security
219+
220+ Install and configure fail2ban:
221+
222+ ``` sh
223+ sudo apt install fail2ban
224+ sudo systemctl enable fail2ban
225+ sudo systemctl start fail2ban
226+ sudo systemctl status fail2ban
227+ ```
228+
229+ Add SSH-specific protection by editing ` /etc/fail2ban/jail.local ` and set:
230+
231+ ``` txt
232+ [sshd]
233+ enabled = true
234+ port = 666 # or your custom SSH port
235+ maxretry = 3
236+ bantime = 1h
237+ ```
238+
239+ #### Hostname
240+
241+ Help identify machines in SSH sessions and logs:
242+
243+ ``` sh
244+ sudo hostnamectl set-hostname my-vps
245+ echo " 127.0.1.1 my-vps" | sudo tee -a /etc/hosts
246+ ```
247+
163248#### Updates
164249
165250Activating unattended upgrades to ensure the server stays up-to-date:
@@ -194,6 +279,31 @@ sudo unattended-upgrades --dry-run
194279sudo systemctl status unattended-upgrades
195280```
196281
282+ #### Backups
283+
284+ Schedule automatic backups and maintenance tasks.
285+
286+ backup.sh script:
287+
288+ ``` sh
289+ tar -czf ~ /backups/backup_$( date +%F) .tar.gz /home/myusername
290+ ```
291+
292+ Open ` crontab -e ` and run backup daily at 4 AM
293+
294+ ``` txt
295+ 0 4 * * * /home/myusername/backup.sh
296+ ```
297+
298+ #### Audit
299+
300+ Automated system security audit with [ lynis] ( https://github.com/CISOfy/lynis/ ) :
301+
302+ ``` sh
303+ sudo apt install lynis
304+ sudo lynis audit system
305+ ```
306+
197307## Checks
198308
199309- [ ] SSH key authentication works
0 commit comments