Home » Tools » Install Ubuntu Server and Secure it

1) Update the system & reboot

Why: Applies the latest security patches and reloads the kernel so fixes take effect.

sudo apt update && sudo apt -y full-upgrade

sudo reboot

2) Create a named admin user

Why: Using a personal admin (instead of the default account) is safer and easier to audit.

sudo adduser mina

sudo usermod -aG sudo mina

  • adduser creates the account and sets a password.
  • Adding to sudo allows admin tasks when needed (prompting for your password).

3) SSH hardening (key-only + safer defaults)

3.1 Add your public key for the admin user

Why: Key-based login is much harder to brute-force than passwords.

sudo mkdir -p /home/mina/.ssh

# paste your public key into /home/mina/.ssh/authorized_keys

sudo chmod 700 /home/mina/.ssh

sudo chmod 600 /home/mina/.ssh/authorized_keys

sudo chown -R mina:mina /home/mina/.ssh

  • Correct permissions ensure only the owner can read the keys.

 

 

3.2 Secure the SSH server settings

Why: Blocks password and root logins and cleans up idle sessions.

sudo nano /etc/ssh/sshd_config

Set or ensure these lines:

PasswordAuthentication no

PermitRootLogin no

PubkeyAuthentication yes

ClientAliveInterval 300

ClientAliveCountMax 2

 

 

3.3 Apply the changes

sudo systemctl reload ssh

Tip: Keep console/VM access open and test a new SSH session with your key before closing your current one.

4) Timezone, automatic updates, firewall, and Fail2Ban

4.1 Set the correct timezone

Why: Accurate time is essential for logs and certificates.

sudo timedatectl set-timezone Australia/Melbourne

 

4.2 Install security helpers

Why: Enables auto security updates, a simple firewall, and brute-force protection.

sudo apt -y install ufw fail2ban unattended-upgrades

sudo dpkg-reconfigure –priority=low unattended-upgrades

  • In the prompt, enable unattended security updates.

 

4.3 Lock down the firewall (default-deny)

Why: Blocks all inbound traffic unless explicitly allowed.

sudo ufw default deny incoming

sudo ufw default allow outgoing

sudo ufw enable



4.4 (Optional) Allow SSH only from your admin device or subnet

Why: Reduces attack surface by limiting where SSH can come from.

# Broader (lab build only): allow entire internal subnet — use with caution

# sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp

 

# Safer: allow only your computer’s IP (replace with your real IP)

sudo ufw allow from 192.168.1.30 to any port 22 proto tcp

 

 

4.5 Check firewall status

sudo ufw status verbose

5) Install Docker & Docker Compose

Why: Run apps in isolated containers; modern, consistent deployments.

curl -fsSL https://get.docker.com | sudo sh

sudo usermod -aG docker $USER   # log out/in after this step to refresh group

docker –version

docker compose version || sudo apt -y install docker-compose-plugin

  • After adding your user to the docker group, log out and back in so it takes effect.

6) Ensure SSH starts now and on boot

Why: Guarantees remote access after reboots.

sudo systemctl enable –now ssh

7) (If needed) Re-create the admin user’s SSH folder

Why: Quick fix if the folder wasn’t created earlier.

sudo mkdir -p /home/mina/.ssh

sudo chmod 700 /home/mina/.ssh

sudo chown -R mina:mina /home/mina/.ssh

8) First remote login test

Why: Confirms the secure setup works with keys and your firewall rules.

# From your computer (replace with your server IP or DNS name)

ssh mina@<your_server_ip>

  • If it logs in without asking for a password, your key is working.
  • If it prompts for a password, re-check you pasted the correct .pub key and file permissions.

 

1) تحديث النظام وإعادة التشغيل

السبب: تثبيت آخر تحديثات الأمان وتحميل النواة الجديدة لتفعيل الإصلاحات.

sudo apt update && sudo apt -y full-upgrade

sudo reboot

2) إنشاء مستخدم إداري مُسمّى

السبب: استخدام حساب إداري خاص بك (بدل الحساب الافتراضي) أكثر أمانًا وأسهل في المراجعة.

sudo adduser mina

sudo usermod -aG sudo mina

  • adduser ينشئ الحساب ويضبط كلمة المرور.
  • إضافة المستخدم إلى مجموعة sudo تسمح بتنفيذ المهام الإدارية عند الحاجة فقط.

3) تشديد أمان SSH (مفاتيح فقط + إعدادات أكثر أمانًا)

3.1 إضافة المفتاح العام للمستخدم الإداري

السبب: الدخول بالمفاتيح أصعب بكثير على التخمين من كلمات المرور.

sudo mkdir -p /home/mina/.ssh

# الصق مفتاحك العام في الملف /home/mina/.ssh/authorized_keys

sudo chmod 700 /home/mina/.ssh

sudo chmod 600 /home/mina/.ssh/authorized_keys

sudo chown -R mina:mina /home/mina/.ssh

  • الأذونات الصحيحة تضمن أن المالك فقط يمكنه قراءة المفاتيح.

 

 

3.2 تأمين إعدادات خادم SSH

السبب: إيقاف تسجيل الدخول بكلمة مرور وبالمستخدم root، وإنهاء الجلسات الخاملة.

sudo nano /etc/ssh/sshd_config

ثبّت أو تأكّد من السطور التالية:

PasswordAuthentication no

PermitRootLogin no

PubkeyAuthentication yes

ClientAliveInterval 300

ClientAliveCountMax 2

 

 

3.3 تطبيق التغييرات

sudo systemctl reload ssh

ملاحظة أمان: اترك الوصول عبر الكونسول/واجهة الـVM مفتوحًا، واختبر جلسة SSH جديدة بمفتاحك قبل إغلاق جلستك الحالية.

4) إعداد المنطقة الزمنية والتحديثات التلقائية والجدار الناري وFail2Ban

4.1 ضبط المنطقة الزمنية

السبب: الوقت الدقيق مهم للسجلات والشهادات.

sudo timedatectl set-timezone Australia/Melbourne

 

 

4.2 تثبيت أدوات الأمان

السبب: تمكين تحديثات الأمان التلقائية، وجدار ناري بسيط، وحماية من محاولات التخمين.

sudo apt -y install ufw fail2ban unattended-upgrades

sudo dpkg-reconfigure –priority=low unattended-upgrades

  • عند ظهور النافذة، فعّل التحديثات الأمنية التلقائية.

 

 

4.3 قفل الجدار الناري (منع افتراضي للداخل)

السبب: حظر كل الاتصالات الواردة إلا ما تسمح به صراحة.

sudo ufw default deny incoming

sudo ufw default allow outgoing

sudo ufw enable

 

 

4.4 (اختياري) السماح بـSSH فقط من جهازك أو من الشبكة الداخلية

السبب: تقليل السطح المعرض للهجوم بحصر مصدر SSH.

# أوسع (للمعمل أثناء البناء): السماح لكل الشبكة الداخلية — بحذر

# sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp

 

# الأكثر أمانًا: السماح فقط بعنوان جهازك (استبدل بالعنوان الحقيقي)

sudo ufw allow from 192.168.1.123 to any port 22 proto tcp

 

 

4.5 التحقق من حالة الجدار الناري

sudo ufw status verbose

5) تثبيت Docker وDocker Compose

السبب: تشغيل التطبيقات داخل حاويات معزولة؛ طريقة حديثة ومتسقة للنشر.

curl -fsSL https://get.docker.com | sudo sh

sudo usermod -aG docker $USER   # سجّل خروج/دخول بعد هذه الخطوة لتفعيل المجموعة

docker –version

docker compose version || sudo apt -y install docker-compose-plugin

  • بعد إضافة المستخدم إلى مجموعة docker، قم بتسجيل الخروج ثم الدخول لتفعيل الصلاحيات.

6) ضمان تشغيل SSH الآن وعند الإقلاع

السبب: يضمن الوصول عن بُعد بعد إعادة التشغيل.

sudo systemctl enable –now ssh

7) (عند الحاجة) إعادة إنشاء مجلّد SSH للمستخدم الإداري

السبب: حل سريع إذا لم يُنشأ المجلّد سابقًا.

sudo mkdir -p /home/mina/.ssh

sudo chmod 700 /home/mina/.ssh

sudo chown -R mina:mina /home/mina/.ssh

8) اختبار أول اتصال عن بُعد

السبب: التأكد من أن الإعداد الآمن يعمل بالمفاتيح ومع قواعد الجدار الناري.

# من جهازك (استبدل بعنوان الخادم أو اسمه)

ssh mina@<your_server_ip>

  • إذا تم الدخول بدون طلب كلمة مرور، فمفتاحك يعمل.
  • إذا طلب كلمة مرور، تأكّد أنك لصقت ملف .pub الصحيح وأن الأذونات مضبوطة.

 

Scroll to Top