SSH (Secure Shell)#

مقدمه#

SSH (Secure Shell) یک پروتکل امن برای دسترسی از راه دور به دستگاه‌های شبکه است. SSH تمام ارتباطات را رمزگذاری می‌کند و در مقایسه با Telnet امنیت بیشتری دارد.

ویژگی‌های SSH در Soodar Router#

  • پشتیبانی از SSH server و client

  • احراز هویت با password یا public key

  • مدیریت کلیدهای عمومی (public keys)

  • تغییر پورت SSH

  • مدیریت known hosts

  • پشتیبانی از key fingerprint verification

مقایسه SSH و Telnet#

ویژگی

SSH

Telnet

رمزگذاری

✅ بله

❌ خیر

امنیت

✅ بالا

❌ پایین

احراز هویت

Password + Key

Password فقط

پورت پیش‌فرض

22

23

توصیه

✅ برای Production

❌ فقط Lab

توصیه امنیتی

همیشه از SSH به جای Telnet استفاده کنید. Telnet password ها را به صورت plain text ارسال می‌کند!


سناریو 1: اتصال SSH به روتر دیگر (SSH Client)#

هدف#

استفاده از Soodar Router به عنوان SSH client برای اتصال به دستگاه دیگر

پیکربندی#

اتصال ساده با username و password#

n1# ssh admin@192.168.1.100
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
RSA key fingerprint is SHA256:lJ2gRSCd8Wh0CrcPU8sOlZJdrbff2QrGaJ5zBcZ2S4I.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.100' (RSA) to the list of known hosts.
admin@192.168.1.100's password: 
n1#

اتصال با پورت مشخص#

n1# ssh admin@192.168.1.100 -p 2222
admin@192.168.1.100's password:
n1#

اتصال با verbose mode (برای troubleshooting)#

n1# ssh -v admin@192.168.1.100
OpenSSH_8.2p1, OpenSSL 1.1.1g
debug1: Connecting to 192.168.1.100 [192.168.1.100] port 22.
debug1: Connection established.
...
n1#

سناریو 2: پیکربندی SSH Server با Password#

هدف#

راه‌اندازی SSH server روی Soodar Router برای دسترسی از راه دور

پیش‌نیاز#

  • User account با password تعریف شده باشد

  • Interface مدیریتی IP داشته باشد

پیکربندی#

مرحله 1: ایجاد user با password#

n1(config)# username admin password Soodar@123
n1(config)# write
n1(config)# exit
n1#

مرحله 2: بررسی وضعیت SSH server#

SSH server به طور پیش‌فرض فعال است:

n1# show ip ssh
SSH Enabled - version 2.0
Authentication timeout: 120 secs; Authentication retries: 3
Server port: 22
n1#

مرحله 3: تست اتصال از کلاینت#

از دستگاه دیگر:

client$ ssh admin@192.168.1.1
admin@192.168.1.1's password: Soodar@123
n1#

نکته

SSH server به طور پیش‌فرض روی پورت 22 فعال است و نیازی به پیکربندی خاصی ندارد.


سناریو 3: احراز هویت با Public Key (بدون Password)#

هدف#

پیکربندی SSH server برای احراز هویت با کلید عمومی (Public Key Authentication)

مزایا#

  • امنیت بیشتر نسبت به password

  • بدون نیاز به وارد کردن password

  • امکان مدیریت متمرکز کلیدها

توپولوژی#

[Client] ----SSH----> [n1 Router]
        Public Key      SSH Server

پیکربندی#

مرحله 1: تولید کلید روی client#

روی دستگاه client (Linux/Mac):

client$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub

مرحله 2: مشاهده public key#

client$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC7Z... user@client-host

مرحله 3: اضافه کردن public key به روتر#

n1# configure terminal
n1(config)# ip ssh pubkey-chain
n1(conf-ssh-pubkey)# username admin
n1(conf-ssh-pubkey-user)# key ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC7Z...
n1(conf-ssh-pubkey-user)# exit
n1(conf-ssh-pubkey)# exit
n1(config)# write
n1(config)# exit
n1#

نکته مهم

کلید باید در یک خط و بدون فاصله وارد شود. اگر کلید خیلی طولانی است، می‌توانید از copy/paste استفاده کنید.

مرحله 4: بررسی کلیدهای اضافه شده#

n1# show ip ssh pubkey-chain
admin:
  1: kgxOYdwjBqPN90V2dLGOxwWSVrKQodgeZhjhaN0+NGc user@client-host (ssh-rsa)
n1#

تست#

اکنون از client می‌توانید بدون password وصل شوید:

client$ ssh admin@192.168.1.1
n1#
# بدون درخواست password وارد شدید! ✅

سناریو 4: مدیریت Public Keys#

اضافه کردن چندین کلید برای یک user#

n1(config)# ip ssh pubkey-chain
n1(conf-ssh-pubkey)# username admin
n1(conf-ssh-pubkey-user)# key ssh-rsa AAAAB3Nza... user1@laptop
n1(conf-ssh-pubkey-user)# key ssh-rsa AAAAB3Nza... user1@desktop
n1(conf-ssh-pubkey-user)# key ssh-rsa AAAAB3Nza... user1@phone
n1(conf-ssh-pubkey-user)# exit
n1(conf-ssh-pubkey)# exit
n1(config)# write

مشاهده تمام کلیدها#

n1# show ip ssh pubkey-chain
admin:
  1: kgxOYdwjBqPN90V2dLGOxwWSVrKQodgeZhjhaN0+NGc user1@laptop (ssh-rsa)
  2: AbCdEfGhIjKlMnOpQrStUvWxYz1234567890ABC user1@desktop (ssh-rsa)
  3: 98765432100ZyXwVuTsRqPoNmLkJiHgFeDcBa user1@phone (ssh-rsa)
n1#

حذف یک کلید خاص#

با استفاده از hash:

n1(config)# ip ssh pubkey-chain
n1(conf-ssh-pubkey)# username admin
n1(conf-ssh-pubkey-user)# no key kgxOYdwjBqPN90V2dLGOxwWSVrKQodgeZhjhaN0+NGc
n1(conf-ssh-pubkey-user)# exit
n1(conf-ssh-pubkey)# exit
n1(config)# write

یا با استفاده از شماره:

n1(config)# ip ssh pubkey-chain
n1(conf-ssh-pubkey)# username admin
n1(conf-ssh-pubkey-user)# no key 1
n1(conf-ssh-pubkey-user)# exit
n1(conf-ssh-pubkey)# exit
n1(config)# write

حذف تمام کلیدهای یک user#

n1(config)# ip ssh pubkey-chain
n1(conf-ssh-pubkey)# no username admin
n1(conf-ssh-pubkey)# exit
n1(config)# write

سناریو 5: تغییر پورت SSH (امنیت بیشتر)#

هدف#

تغییر پورت پیش‌فرض SSH (22) برای امنیت بیشتر

چرا پورت را تغییر دهیم؟#

  • کاهش حملات اتوماتیک (bots معمولاً پورت 22 را اسکن می‌کنند)

  • لایه امنیتی اضافی (Security by Obscurity)

  • جلوگیری از brute force attacks

پیکربندی#

مرحله 1: تغییر پورت SSH#

n1(config)# ip ssh port 2222
n1(config)# write
n1(config)# exit
n1#

محدوده پورت مجاز: 2000 تا 10000

مرحله 2: بررسی تغییرات#

n1# show ip ssh
SSH Enabled - version 2.0
Authentication timeout: 120 secs; Authentication retries: 3
Server port: 2222
n1#

تست اتصال با پورت جدید#

از client:

client$ ssh admin@192.168.1.1 -p 2222
admin@192.168.1.1's password:
n1#

هشدار

اگر پورت را تغییر دهید، حتماً آن را یادداشت کنید! در غیر این صورت ممکن است دسترسی خود را از دست بدهید.

بازگشت به پورت پیش‌فرض#

n1(config)# no ip ssh port
n1(config)# write

یا:

n1(config)# ip ssh port 22
n1(config)# write

سناریو 6: مدیریت Known Hosts (SSH Client)#

هدف#

مدیریت کلیدهای عمومی سرورهایی که روتر به آنها SSH می‌زند

چرا Known Hosts مهم است؟#

Known hosts از حملات Man-in-the-Middle (MITM) جلوگیری می‌کند. هربار که به سرور جدیدی SSH می‌زنید، fingerprint کلید سرور ذخیره می‌شود.

روش 1: اضافه شدن خودکار known host#

اولین بار که به سرور SSH می‌زنید:

n1# show ip ssh client known-host 192.168.30.39
# خالی - این سرور در لیست نیست

n1# ssh test@192.168.30.39
The authenticity of host '192.168.30.39 (192.168.30.39)' can't be established.
RSA key fingerprint is SHA256:lJ2gRSCd8Wh0CrcPU8sOlZJdrbff2QrGaJ5zBcZ2S4I.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.30.39' (RSA) to the list of known hosts.
test@192.168.30.39's password:

بررسی مجدد:

n1# show ip ssh client known-host 192.168.30.39
192.168.30.39 RSA SHA256:lJ2gRSCd8Wh0CrcPU8sOlZJdrbff2QrGaJ5zBcZ2S4I
n1#

روش 2: اضافه کردن دستی known host#

شما می‌توانید قبل از اتصال، کلید سرور را به صورت دستی اضافه کنید:

مرحله 1: دریافت کلید عمومی سرور#

روی سرور، کلید عمومی را مشاهده کنید:

server$ cat /etc/ssh/ssh_host_rsa_key.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCwOU2O2nNJGXIN5VT1Q0j7+H9kQQ9FnE0sl9aPQbOg...

مرحله 2: اضافه کردن به روتر#

n1# configure terminal
n1(config)# ip ssh client
n1(conf-ssh-client)# known-host 192.168.30.50 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCwOU2O2nNJGXIN5VT1Q0j7+H9kQQ9FnE0sl9aPQbOg/Sw1ryZyuUmApUFFABL7MDNZTKzWd3BfYsOBsXOsKOHiGTZCPLbS93tvHAYlkeIcYDR9JJEi4A67nN/zXSoT+Ew78iUADjWH6rQSy4dtg+ScHFAj3Z9P7TQpK8zWJDLgA28d+zyYSwNd/MkF+EPmAH7mPoKkg2EGCpr889pR5mcBiXPVq69yUNFUG7U0D2aqDaGbaXk9TcfqCrktVmjGVF8rY91TaLMJBngVaYYsnT+xdYp8i8nicxbJoYDvvde057soX6mcTLNXI0opUV9K5TPY7Idp6AWCAxhgJ11IN2z+HZGw56xKDVXL0VXNMngxxICqMV5CxhYHraGkyCha1KXnU2rPi8PbYJkJMIlsXZ+hW9oCZs9x6gzvHHdadi3Ox9JZ6KEqLI7OKf8KNd2alZrGUNjlDIlG/jZhWtYdB4W/oFPAWa5YFqDRfu+VJdVnrGqIzr8GWRlPOjAjwOsBcQk= HOST-KEY
n1(conf-ssh-client)# exit
n1(config)# write
n1(config)# exit
n1#

مرحله 3: بررسی#

n1# show ip ssh client known-host 192.168.30.50
192.168.30.50 RSA SHA256:bYisVirAvDxXqwbmYIn7IEj6Grdkf6BeTYCJ7LS11s0 HOST-KEY
n1#

مرحله 4: اتصال بدون warning#

n1# ssh test@192.168.30.50
test@192.168.30.50's password:
# بدون warning درباره authenticity ✅

مشاهده تمام known hosts#

n1# show ip ssh client known-host
192.168.30.39 RSA SHA256:lJ2gRSCd8Wh0CrcPU8sOlZJdrbff2QrGaJ5zBcZ2S4I
192.168.30.50 RSA SHA256:bYisVirAvDxXqwbmYIn7IEj6Grdkf6BeTYCJ7LS11s0 HOST-KEY
10.0.0.1 RSA SHA256:AbCdEfGhIjKlMnOpQrStUvWxYz1234567890ABC
n1#

حذف یک known host#

n1(config)# ip ssh client
n1(conf-ssh-client)# no known-host 192.168.30.39
n1(conf-ssh-client)# exit
n1(config)# write

حذف تمام known hosts#

n1(config)# no ip ssh client
n1(config)# write

چه زمانی باید known host را حذف کنیم؟

اگر کلید سرور تغییر کرد (مثلاً سرور reinstall شد)، باید known host قدیمی را حذف کنید تا با کلید جدید مشکلی پیش نیاید.


عیب‌یابی SSH#

مشکل 1: نمی‌توانم به روتر SSH بزنم#

بررسی‌ها:

  1. آیا SSH server فعال است؟

n1# show ip ssh
SSH Enabled - version 2.0
  1. آیا user و password صحیح است؟

n1# show users
Username  Privilege
admin     15
  1. آیا interface مدیریتی IP دارد؟

n1# show interface brief
Interface       IP-Address      Status       Protocol
ge0             192.168.1.1/24  up           up
  1. آیا firewall یا ACL مسدود نکرده؟

n1# show ip access-lists

مشکل 2: Public key authentication کار نمی‌کند#

بررسی‌ها:

  1. آیا کلید درست اضافه شده؟

n1# show ip ssh pubkey-chain
admin:
  1: kgxOYdwjBqPN90V2dLGOxwWSVrKQodgeZhjhaN0+NGc user@host (ssh-rsa)
  1. آیا username در هر دو جا یکسان است؟

# در پیکربندی روتر:
n1(config)# ip ssh pubkey-chain
n1(conf-ssh-pubkey)# username admin  ← باید با username اتصال یکسان باشد

# در client:
client$ ssh admin@192.168.1.1  ← همان username
  1. آیا private key روی client درست است؟

client$ ls -la ~/.ssh/
-rw------- 1 user user 3243 Oct 10 12:00 id_rsa      ← private key
-rw-r--r-- 1 user user  748 Oct 10 12:00 id_rsa.pub  ← public key

مشکل 3: Warning: "Remote host identification has changed"#

علت: کلید سرور تغییر کرده است (reinstall, etc.)

راه‌حل:

n1(config)# ip ssh client
n1(conf-ssh-client)# no known-host 192.168.30.50
n1(conf-ssh-client)# exit
n1(config)# write

سپس دوباره SSH بزنید تا کلید جدید ذخیره شود.

مشکل 4: "Connection refused"#

علت‌های احتمالی:

  • SSH server غیرفعال است

  • پورت SSH اشتباه است

  • Firewall بسته است

راه‌حل:

n1# show ip ssh
# بررسی پورت و وضعیت SSH

# تست با telnet اگر فعال باشد
client$ telnet 192.168.1.1 22

بهترین شیوه‌های امنیتی SSH#

1. استفاده از Strong Passwords#

اگر از password استفاده می‌کنید:

✅ Soodar@Router#2024!
✅ My$ecure!Pass123
❌ admin
❌ 123456

2. استفاده از پورت غیراستاندارد#

n1(config)# ip ssh port 2222
n1(config)# write

3. محدود کردن دسترسی با ACL#

n1(config)# access-list 10 permit 10.0.0.0 0.0.0.255
n1(config)# access-list 10 deny any
n1(config)# line vty 0 4
n1(config-line)# access-class 10 in
n1(config-line)# exit
n1(config)# write

4. استفاده از VRF برای Management#

n1(config)# vrf management
n1(config-vrf)# exit
n1(config)# interface ge0
n1(config-if)# ip vrf forwarding management
n1(config-if)# ip address 10.0.0.1/24
n1(config-if)# exit
n1(config)# write

5. فعال‌سازی Logging#

n1(config)# logging buffered informational
n1(config)# write

مشاهده login attempts:

n1# show logging | include SSH

خلاصه دستورات SSH#

SSH Server (روتر به عنوان سرور)#

دستور

توضیح

show ip ssh

نمایش وضعیت SSH server

ip ssh port <port>

تغییر پورت SSH

ip ssh pubkey-chain

مدیریت public keys

username <name>

انتخاب user برای اضافه کردن key

key <public-key>

اضافه کردن public key

show ip ssh pubkey-chain

نمایش تمام public keys

no key <hash>

حذف یک public key

SSH Client (روتر به عنوان کلاینت)#

دستور

توضیح

ssh <user>@<host>

اتصال به سرور SSH

ssh <user>@<host> -p <port>

اتصال با پورت مشخص

ssh -v <user>@<host>

اتصال با verbose mode

ip ssh client

ورود به حالت پیکربندی SSH client

known-host <ip> <key>

اضافه کردن known host

show ip ssh client known-host

نمایش تمام known hosts

show ip ssh client known-host <ip>

نمایش known host خاص

no known-host <ip>

حذف یک known host

مثال پیکربندی کامل#

# تنظیمات SSH Server
n1(config)# username admin password Soodar@123
n1(config)# ip ssh port 2222
n1(config)# ip ssh pubkey-chain
n1(conf-ssh-pubkey)# username admin
n1(conf-ssh-pubkey-user)# key ssh-rsa AAAAB3NzaC1yc2E...
n1(conf-ssh-pubkey-user)# exit
n1(conf-ssh-pubkey)# exit
n1(config)# write

# تنظیمات SSH Client
n1(config)# ip ssh client
n1(conf-ssh-client)# known-host 192.168.1.100 ssh-rsa AAAAB3Nz...
n1(conf-ssh-client)# exit
n1(config)# write