Sunday, May 7, 2017

Everything you need to know about the SSH


අද මම කියලා දෙන්න යන්නේ SSH (Secure Shell) ගැන සාමාන්‍යයෙන් හැම කෙනෙක්ම දැන ගත යුතු දේවල් කිහිපයක්


1. SSH server installation 

අපිට remote server එකකට ssh මගින් connect වීමට නම් අපි පළමුව remote server එකෙහි ssh server මෘදුකාංගය install කල යුතුය.

  • apt based systems (Debian/Ubuntu)
    • Internet connect වී Command line (Terminal) එක open කරගන්න.
    • පහත commands enter කරන්න
      • sudo su (root log වීමට - optional)
      • apt-get update (repositories update කිරීමට - Optional)
      • apt-get upgrade (repositories update කිරීමට - Optional)
      • apt-get install openssh-server
      • service ssh start
  • yum based systems(Redhat/Centos)

    • Internet connect වී Command line (Terminal) එක open කරගන්න.
    • පහත commands enter කරන්න
      • sudo su (root log වීමට - optional)
      • yum -y update (repositories update කිරීමට - Optional)
      • yum -y upgrade (repositories update කිරීමට - Optional)
      • yum -y install openssh-server
      • systemctl start sshd

2. SSH default port එක වෙනස් කිරම (Default port 22)
  • දැනට භාවිතා වන Port එක බැලීමට
    • grep Port /etc/ssh/sshd_config [Redhat/Centos]
    • grep Port /etc/ssh/ssh_config   [Debian/Ubuntu]

  • දැනට භාවිතා වන Port එක වෙනස් කිරීමට
    • nano /etc/ssh/sshd_config [Redhat/Centos]
    • nano /etc/ssh/ssh_config         [Debian/Ubuntu]

3. Root User මගින් SSH login වීම enable/disable කිරී
  • nano /etc/ssh/sshd_config [Redhat/Centos]
  • nano /etc/ssh/ssh_config         [Debian/Ubuntu]
  • PermitRootLogin Yes (enable)
  • PermitRootLogin No (disable)


4. Client host DNS මගින් Search කිරීම disable කිරීම (DNS searching disable කිරිම මගින් වේගවත් ලෙස Client මගින් connect වීම සිදුකල හැකිය)

  • nano /etc/ssh/sshd_config [Redhat/Centos]
  • nano /etc/ssh/ssh_config         [Debian/Ubuntu]
  • UseDNS No (disable)




5. SSH Passwordless Login


6. Specific Users/User Group සදහා access enable කිරිම (සැ.යු. මෙම user/user group යනු remote server එකේ define කර ඇති user/user group වේ)
  • nano /etc/ssh/sshd_config [Redhat/Centos]
  • nano /etc/ssh/ssh_config         [Debian/Ubuntu]
  • Add below line(s)
  • AllowUsers kavinda kavinda2 kavinda3
  • AllowGroups wheel admin group1

7. Welcome/Warning message එකක් display කිරීම
  • Terminal එක කර ඔබට අවශ්‍ය Welcome/Warning banner එක ASCII letters වලින් හදාගන්න (nano /etc/banner)


  • nano /etc/ssh/sshd_config [Redhat/Centos]
  • nano /etc/ssh/ssh_config         [Debian/Ubuntu]
  • Change below parameter 
  • Banner PATH   (Remove leading # and replace with your path)




8. SSH  Protocol  version එක වෙනස් කිරීම (SSH Communication එක සදහා protocol 2ක් භාවිතා කරයි ඒවා protocol 1 හා protocol 2 ලෙස හදුන්වන අතර , protocol 1 යනු පැරණි version එක වන අතර protocol 2 enhanced version එක වේ )
  • nano /etc/ssh/sshd_config [Redhat/Centos]
  • nano /etc/ssh/ssh_config         [Debian/Ubuntu]
  • Add/Modify below entry as you wish
  • protocol 2,1 

9.  Trace unauthorized login attempts to SSH Server (මේ සදහා remote server එකෙහි log file එක භාවිතා කරයි)
  • Terminal එක කර  පහත command එක enter කරන්න
  • cat /var/log/secure | grep "Failed password for"



10. Copy files over SSH (මේ සදහා SCP command එක භාවිතා කරයි SCP - Secure copy)
  • Terminal එක කර  පහත command එක enter කරන්න
  • scp source_file  user@remoter_server:path
  • scp test.txt kavinda@192.168.1.100:/home/user/Desktop

11. Set Maximum login attempts (Remote server එකහි ආරක්ෂාව උදෙසා බොහෝ වෙලාවට login වෙන වරකදී enter කල හැකි වැරදි password enter කිරීම් limit කරනු ලැබේ [Avoid Brute Force Attack] )

  • nano /etc/ssh/sshd_config [Redhat/Centos]
  • nano /etc/ssh/ssh_config         [Debian/Ubuntu]
  • Add/Modify below entry as you wish
  • MaxAuthTries value

11. SSH Key-based authentication  

Remote server එකහි ආරක්ෂාව උදෙසා හා Password අන් අය වෙත යාම වලක්වීම්ට භාවිතා කරන සාර්ථක ක්‍රමයකි.මෙහි ඇති දුර්වලතාවයක් වනුයේ ඔබට සැමවිටම generate කරන ලද key එක ලග තබා ගැනීමට සිදුවීමය. 

මෙම ක්‍රමය ඉතාමත් ආරක්ෂිත ක්‍රමයක් වන අතර මෙම ක්‍රමය මගින් remote server එක hack/password change කිරම වැනිදේ වලක්වා ගත හැකිය

  • Terminal එක කර  පහත command එක enter කරන්න Create folder, change permission and navigate to new folder with the following commands)
  • mkdir .ssh/
  • chmod 700 .ssh/
  • cd .ssh/
  • touch authorized_keys
  • chmod 600 authorized_keys
  • ls -ltra (Show new files - Optional step)
  • ssh-keygen -t rsa  (Generate Public/Pravate keys using RSA algorithm)
  • cat id_rsa.pub >> authorized_keys (Append the public key to authorized_keys)
  • nano /etc/ssh/sshd_config [Redhat/Centos]
  • nano /etc/ssh/ssh_config         [Debian/Ubuntu]
  • Add/Modify below entries
  • --------------------------------------------------------------
  • RSAAuthentication Yes
  • PubkeyAuthentication Yes
  • --------------------------------------------------------------
  • ChallengeResponseAuthentication No
  • PasswordAuthentication No
  • UsePAM No
  • Generate කරගත් private key එක local (Client) PC එකේ save කරගැනීමට nano/vim editor මගින් open කරගෙන local PC එකේ ඇති Notepad වැනි editor එකක් මත paste කර  extension එකක් රහිතව save කරගන්න , සැයු මෙම key එක නොමැතිව ඔබට remote server එකට log විය නොහැකිය password based login disable කර ඇති නිසා,Physical remote server location  එකට ගොස්  or VNC/Teamviewer මගින් නැවත සැකසීම කල යුතුවේ . 
  • nano /root/.ssh/id_rsa  (To View generated Private key)
ඉහත key එක Secure CRT/Putty/Tera term වෙතට insert කර remote server එකට log වෙන්න





--------------------------------------------------------------------------------------------------------------------
Note : 

  • ඉහත කරන ලද හැම change එකක්ම අවසානයේ SSH service එක restart කර යුතුය.
    • service ssh restart                [Debian/Ubuntu] 
    • systemctl restart sshd          [Redhat/Centos]

  • ඉහත සදහන් බොහෝ Commands SSH configuration එකේ '#' මගින් comments කර ඇති අතර '#' remove කිරම මගින් එම commands enable/activate කල හැකිය




















SITTHAMA

Sinhala Support


Followers

Designed By Seo Blogger Templates