Showing posts with label SSH. Show all posts
Showing posts with label SSH. Show all posts

2020-08-27

SSH with multiple private keys

Add privat keys
 $ ssh-add ~/.ssh/id_rsa_SERVER
Verify keys
 $ ssh-add -l
Test keys
 $ ssh -v URL_TO_HOST
Connect to server
 $ ssh HOSTNAME_OR_ALIAS

2019-06-26

Allow SSH access based on Country using GeoIP

  1. Install packages
    $ sudo apt install geoip-bin geoip-database
    
  2. Create script
    $ sudo vi /usr/local/bin/sshfilter.sh
    
    
    #!/bin/bash
    
    ALLOWED_COUNTRIES="KR US GB NZ"
    ALLOWED_IP="192.168 127.0.0"
    
    [[ "`echo $1 | grep ':'`" != "" ]] && SUFFIX="6"
    if [[ $ALLOWED_IP =~ ${1:0:7} ]]; then
     logger -p authpriv.notice "SSH ALLOWED $1 LOCALDOMAIN"
     exit 0
    fi
    
    GEORESULT=`/usr/bin/geoiplookup${SUFFIX} "$1"`
    COUNTRY=`echo $GEORESULT | cut -d : -f 2 | xargs | head -1`
    COUNTRYCODE=`echo $COUNTRY | cut -d , -f 1 | xargs`
    [[ $ALLOWED_COUNTRIES =~ $COUNTRYCODE ]] && RESPONSE="ALLOWED" || RESPONSE="DENIED"
    logger -p authpriv.notice "SSH $RESPONSE $1 $COUNTRY"
    [[ $RESPONSE == "ALLOWED" ]] && exit 0 || exit 1
    
  3. edit /etc/hosts.allow and /etc/hosts.deny
    # /etc/hosts.allow
    sshd: ALL: /usr/local/bin/sshfilter.sh %a
    
    # /etc/hosts.deny
    sshd: ALL
    

SSH lockout 60min after 5 brute force attempts in 10min

  1. install fail2ban
    $ sudo apt update
    $ sudo apt install fail2ban
    
  2. edit configuration
    $ sudo vi /etc/fail2ban/jail.conf
    
    [sshd]
    enabled  = true
    port     = ssh
    maxretry = 5    # 5 attempts not permitted
    findtime = 600  # within 10 minutes
    bantime  = 3600 # ban 1 hour
    logpath  = %(sshd_log)s
    backend  = %(sshd_backend)s
    
  3. unban an ip address
    $ sudo fail2ban-client set sshd unbanip IP_ADDRESS
    

2013-06-29

Manage multiple SSH private keys

There are 2 ways to manage multiple SSH private keys.

Step 0. Create a file named ~/.ssh/config

Step 1-1st, SSH can be configured with both host names and their identity files.

Host server1.remote.host
IdentityFile ~/.ssh/server1.remote.host.privatekey
Host server2.remote.host
IdentityFile ~/.ssh/server2.remote.host.privatekey

Step 1-2nd, SSH can be configured with a per-user configuration file.

$ vi config
Type the followings...

IdentityFie ~/.ssh/id_dsa_%h_%r
IdentityFie ~/.ssh/id_rsa_%h_%r
IdentityFie ~/.ssh/id_dsa
IdentityFie ~/.ssh/id_rsa

Save and quit.

$ssh username@remote.host

=> SSH client will try to read the private key at one of the following files
~/.ssh/id_dsa_remote.host_username
~/.ssh/id_rsa_remote.host_username
~/.ssh/id_dsa
~/.ssh/id_rsa

2012-08-08

How to install GIT Server on Windows

As you know, GIT is one of source code management system. And it's ability includes a distributed revision control.
If you don't need any central integrated server, use only msysgit.
If you want to control GIT by GUI, use msysgit and TortoiseGIT that is front-end for msysgit.

The following instruction is how to install GIT repository server on Windows.

  • Summary
    • Setup
      • Install Cygwin
      • Setup SSH daemon
      • Add user for GIT service
      • (optional) Install Gitolite for managing accounts
    • Use GIT
      • Create Repository
      • Push project on developer's system
      • Download and install TortoiseGIT and MsysGit
  1. Install Cygwin
    • Download Cygwin installer on cygwin.com
    • Run setup.exe
    • Add [git] in [Devel] and [openssh] in [Net] and [python] in [Python]
  2. Setup SSH
    • Add Environment Variable
      • Select [Properties] on [Computer]
      • Click [Advanced system settings] on Control Panel > System and Security > System]
      • Click [Environment Variables...]
      • Click [New...] on System variables pannel
      • Type [CYGWIN] for [Variable name] and [ntsec] for [Variable value]
      • Click [OK]
    • Run [Cygwin Bash Shell] as Administrator
      • $ ssh-host-config  <= setup sshd
      • Answer [yes] for most questions
      • Answer [no] for question about using a different username for running the service
      • $ cygrunsrv -S sshd  <= run sshd
  3. Add user
    * There are no users in Cygwin that do not exist in Windows first. Create the Windows user first then use mkpasswd to add that user to /etc/passwd.
    • On Control panel
      • Control Panel > User Accounts and Family Safety > Add or remove user accounts
      • Create a new account
    • On command prompt
      • $ net user git password /add /yes
      • $ net localgroup <an_local_group> git /add
      • $ mkpassd -l -u git >> /etc/passwd  <= on Cygwin shell
      • Update git's home directory to /cygdrive/d/git-repository in the file /etc/passwd <= d is hard disk drive
  4. Add public key
    • $ ssh-keygen
    • $ cd .ssh
    • $ cp id_rsa.pub /tmp
    • runas /user:git c:/cygwin/cygwin.bat <= on windows command prompt
    • $ cp /tmp/id_rsa.pub ~/.ssh/authorized_keys
  5. Use Git
    • Create repository
      • login as git
      • $ mkdir PROJECT.git
      • $ cd PROJECT.git
      • $ git --bare init
    • Push project
      * in the project folder
      • git init
      • git add .
      • git commit -m 'initial commit'
      • git remote add origin git@[SERVER_NAME or IP]:PROJECT.git
      • git push origin master
    • Pull project
      • git clone git@[SERVER]:PROJECT.git