غالبًا ما يكون منفذ SSH الافتراضي (22) هدفًا لهجمات القوة الغاشمة الآلية. سيعلّمك هذا الدليل كيفية تغيير منفذ SSH وضبط خيارات أمان إضافية لحماية خادمك.
📋 المتطلبات
- خادم VPS مع صلاحيات root أو sudo
- اتصال SSH نشط
- UFW مثبّت ومضبوط (راجع الدرس تثبيت UFW)
⚠️ مهم: أبقِ جلسة SSH مفتوحة
مهم: قبل البدء، أبقِ جلسة SSH نشطة. إذا حدث خطأ ما، فستظل قادرًا على الاتصال وإصلاح المشكلة.
افتح طرفيتَي SSH بالتوازي:
- الطرفية 1: جلسة العمل (لإجراء التعديلات)
- الطرفية 2: جلسة احتياطية (تحسبًا لأي طارئ)
🔧 تغيير منفذ SSH
الخطوة 1: اختيار منفذ جديد
اختر منفذًا بين 1024 و65535. منافذ شائعة يجب تجنبها:
- 22 (منفذ SSH الافتراضي، وهو مستهدف بكثافة)
- 80 و443 (HTTP/HTTPS)
- 3306 و5432 (قواعد البيانات)
أمثلة على منافذ آمنة: 2222 و2200 و1022 و5000
الخطوة 2: تعديل إعداد SSH
Bash# Edit SSH configuration file sudo nano /etc/ssh/sshd_config
الخطوة 3: تغيير المنفذ
ابحث عن السطر #Port 22 وعدّله:
# Before
#Port 22
# After (replace 2222 with your chosen port)
Port 2222
نصيحة: استخدم Ctrl + W في nano للبحث عن "Port 22"
الخطوة 4: الحفظ والخروج
في nano:
Ctrl + XللخروجYللتأكيدEnterللحفظ
الخطوة 5: التحقق من الصياغة
Bash# Verify that the configuration is valid sudo sshd -t
إذا لم يُرجع هذا الأمر أي أخطاء، فإعدادك صحيح.
الخطوة 6: السماح بالمنفذ الجديد في UFW
Bash# Allow the new SSH port (replace 2222 with your port) sudo ufw allow 2222/tcp # AlmaLinux / Rocky Linux (firewalld) # sudo firewall-cmd --permanent --add-port=2222/tcp && sudo firewall-cmd --reload # Reload UFW sudo ufw reload # Verify that the rule is active sudo ufw status | grep 2222
الخطوة 7: إعادة تشغيل خدمة SSH
Bash# Restart SSH with the new configuration sudo systemctl daemon-reload # AlmaLinux / Rocky Linux (SELinux) # sudo dnf install -y policycoreutils-python-utils # sudo semanage port -a -t ssh_port_t -p tcp 2222 # Ubuntu 22.10+ / 24.04 (ssh.socket) systemctl is-enabled --quiet ssh.socket && sudo systemctl restart ssh.socket # Debian / Ubuntu (ssh) — AlmaLinux / Rocky Linux (sshd) sudo systemctl restart ssh 2>/dev/null || sudo systemctl restart sshd # Verify that SSH is still working sudo systemctl status ssh 2>/dev/null || sudo systemctl status sshd
الخطوة 8: اختبار الاتصال الجديد
في طرفية جديدة، اختبر الاتصال بالمنفذ الجديد:
Bash# Replace 2222 with your port and user@your-ip with your credentials ssh -p 2222 user@your-ip
إذا نجح الاتصال، يمكنك إغلاق الجلسة القديمة على المنفذ 22.
Bash# Optional: Remove the old port 22 if you no longer use it sudo ufw delete allow 22/tcp sudo ufw reload
🔒 خيارات أمان إضافية
تعطيل تسجيل دخول root
مهم: تعطيل تسجيل دخول root عبر SSH ممارسة أمنية ممتازة. تأكد من إعداد مستخدم لديه sudo قبل إجراء هذا التعديل.
Bash# Edit SSH configuration sudo nano /etc/ssh/sshd_config # Find and modify this line PermitRootLogin no # Save and restart sudo sshd -t sudo systemctl restart ssh 2>/dev/null || sudo systemctl restart sshd
الحد من محاولات الاتصال
Bashsudo nano /etc/ssh/sshd_config # Add or modify these lines MaxAuthTries 3 # Maximum 3 connection attempts LoginGraceTime 30 # 30 second timeout to connect
تعطيل المصادقة بكلمة المرور (استخدم مفاتيح SSH فقط)
Bashsudo nano /etc/ssh/sshd_config # Modify this line PasswordAuthentication no PubkeyAuthentication yes
تحذير: لا تفعّل هذا الخيار إلا إذا ضبطت مفتاح SSH واختبرت أنه يعمل!
تعطيل أنفاق X11 (إذا لم تكن بحاجة إليها)
Bashsudo nano /etc/ssh/sshd_config # Modify this line X11Forwarding no
الإعداد الكامل الموصى به
إليك إعداد SSH آمنًا كاملًا لإضافته في /etc/ssh/sshd_config:
# Custom SSH port
Port 2222
# Disable root login
PermitRootLogin no
# Connection limits
MaxAuthTries 3
LoginGraceTime 30
# Disable password authentication (if you use SSH keys)
# PasswordAuthentication no
# PubkeyAuthentication yes
# Disable X11 forwarding
X11Forwarding no
# Disable TCP tunnels
AllowTcpForwarding no
# Inactivity timeout before disconnect
ClientAliveInterval 300
ClientAliveCountMax 2
بعد كل تعديل، لا تنسَ:
Bash# Verify syntax sudo sshd -t # Restart SSH sudo systemctl restart ssh 2>/dev/null || sudo systemctl restart sshd
🔄 إنشاء مستخدم بصلاحيات sudo (عند الحاجة)
إذا عطّلت تسجيل دخول root، فتأكد من وجود مستخدم لديه صلاحيات sudo:
Bash# Create a new user sudo adduser myuser # Add the user to the sudo group sudo usermod -aG sudo myuser # AlmaLinux / Rocky Linux # sudo usermod -aG wheel myuser # Test that the user can use sudo su - myuser sudo whoami # Should display "root"
✅ التحقق النهائي
التحقق من أن SSH يعمل
Bash# Check service status sudo systemctl status ssh 2>/dev/null || sudo systemctl status sshd # Verify that SSH is listening on the correct port sudo ss -tlnp | grep ssh # Verify configuration sudo sshd -T | grep port
التحقق من سجلات SSH
Bash# View connection attempts sudo journalctl -u ssh -u sshd -f # View successful connections sudo journalctl -u ssh -u sshd --since today | grep "Accepted" # View failed attempts sudo journalctl -u ssh -u sshd --since today | grep "Failed"
🛡️ حماية إضافية باستخدام fail2ban
fail2ban أداة تحظر تلقائيًا عناوين IP التي تحاول تنفيذ هجمات القوة الغاشمة.
تثبيت fail2ban
Bashsudo apt update sudo apt install fail2ban -y # AlmaLinux / Rocky Linux # sudo dnf install -y epel-release && sudo dnf install -y fail2ban
الإعداد الأساسي
Bash# Copy the configuration file sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local # Edit the configuration sudo nano /etc/fail2ban/jail.local
الإعداد الموصى به لـ SSH:
INI1[sshd] 2enabled = true 3backend = systemd 4port = 2222 # Your new SSH port 5maxretry = 3 # Number of attempts before ban 6bantime = 3600 # Ban time in seconds (1 hour) 7findtime = 600 # Time window to count attempts (10 minutes)
تفعيل fail2ban
Bash# Start fail2ban sudo systemctl start fail2ban # Enable on boot sudo systemctl enable fail2ban # Check status sudo systemctl status fail2ban # View banned IPs sudo fail2ban-client status sshd
🆘 استكشاف الأخطاء وإصلاحها
لم يعد بإمكاني الاتصال عبر SSH
إذا حُجب وصولك إلى الخادم:
-
استخدم جلستك الاحتياطية (الطرفية 2 التي أبقيتها مفتوحة)
-
إذا لم تكن لديك جلسة احتياطية:
- تواصل مع دعم Infrawire مع بيانات دخولك
- استخدم وحدة تحكم VNC/KVM من لوحة التحكم
- اتصل مباشرة واستعد الإعداد القديم:
Bash
sudo nano /etc/ssh/sshd_config # Put back Port 22 sudo systemctl restart ssh 2>/dev/null || sudo systemctl restart sshd
منفذ SSH لا يستجيب
Bash# Verify that SSH is listening on the port sudo ss -tlnp | grep ssh # Verify that the port is open in UFW sudo ufw status | grep 2222 # Check SSH logs sudo journalctl -u ssh -u sshd -n 50
الحاجة إلى الاتصال بالمنفذ القديم مؤقتًا
إذا احتجت إلى الاتصال بالمنفذ القديم (22) قبل إزالته:
Bash# Temporarily allow port 22 sudo ufw allow 22/tcp # Connect ssh -p 22 user@your-ip # Once connected, remove the rule sudo ufw delete allow 22/tcp
📚 موارد إضافية
تقدم Infrawire خادم VPS لكل توزيعة رئيسية: VPS Ubuntu، VPS Debian، VPS AlmaLinux، VPS Rocky Linux وVPS Kali Linux.
❓ الأسئلة الشائعة
س: ما المنفذ الذي يجب أن أختاره لـ SSH؟
ج: اختر منفذًا بين 1024 و65535. تجنب المنافذ الشائعة (80 و443 و3306، إلخ). منافذ مثل 2222 أو 2200 أو 1022 خيارات جيدة.
س: هل يمكنني استخدام المنفذ نفسه لعدة خدمات؟
ج: لا، يجب أن يكون لكل خدمة منفذها الفريد.
س: هل أحتاج فعلًا إلى تغيير منفذ SSH؟
ج: يُوصى بذلك بشدة. فهو يقلل هجمات القوة الغاشمة الآلية بشكل ملحوظ.
س: هل fail2ban ضروري إذا غيّرت المنفذ؟
ج: لا، لكنه طبقة أمان إضافية موصى بها.
س: كيف أتصل بالمنفذ الجديد من عميل SSH؟
ج: استخدم الخيار -p: ssh -p 2222 user@your-ip
أمان جيد! 🚀