#LL @ME

Bitbucket Installation

I have a need to create code repository locally. I don't want to use code repo in the cloud. Bitbucket is the winner!

#1 - Install Ubuntu 16.10
Download from ubuntu.com, get the latest ISO file, boot and install.
During the installation wizard, make sure PostgreSQL is selected and installed.

#2 - Configure PostgreSQL
Login to ubuntu as the standard user

> sudo -u postgres psql postgres

\password mynewpassword
\q

>

#3 - Create PostgreSQL Database and Role

> sudo -u postgres
CREATE ROLE bitbucketuser WITH LOGIN PASSWORD 'mypassword' VALID UNTIL 'infinity';

CREATE DATABASE bitbucket WITH ENCODING='UTF8' OWNER=bitbucketuser CONNECTION LIMIT=-1;

\q

>

#4 - Install Bitbucket
Download the bitbucket installer from atlassian.com
Change the file permission to execute +x
Run it

#5 - Configure Bitbucket
During the configuration wizard, when asked for database, specify localhost, bitbucket as the database, bitbuckeruser and the user and 'mypassword' as the password


Ubuntu File Finders

To find the Disk Usage:

#> sudo du -sx /* 2> /dev/null | sort -n

To deep dive

#> sudo du -sx /var/* 2> /dev/null | sort -n

To find files bigger than something

#> sudo find / -size +10M -ls

UNIX Screen - Split Screen

You can do it in screen the terminal multiplexer.
  • To split vertically: ctrla then |.
  • To split horizontally: ctrla then S (uppercase one).
  • To un-split: ctrla then Q (uppercase one).
  • To switch from one to the other: ctrla then tab
Note: After splitting, you need to go into the new region and start a new session via ctrla then c before you can use that area.
EDIT, basic screen usage:
  • New terminal: ctrla then c.
  • Next terminal: ctrla then space.
  • Previous terminal: ctrla then backspace.
  • N'th terminal ctrla then [n](works for n∈{0,1…9})
  • Switch between terminals using list: ctrla then " (useful when more than 10 terminals)
  • Send ctrla to the underlying terminal ctrla then a.

Fail2ban BAN-Forever!

OK, enough is enough. As per my previous blog about setting up fail2ban, there was this one persistent idiot who kept trying to break in my network. Due to fail2ban default setting, which un-bans previously banned IP, he could keep trying and trying and trying...

So, I came across this blog and set my systems as per following:

Modify fail2ban config:

vi /etc/fail2ban/fail2ban.conf
Make sure

log level = 3
logtarget = /var/log/fail2ban.log
Modify Logrotate config:

vi /etc/logrotate.d/fail2ban
Make sure

notifempty
monthly
rotate 13
missingok
postrotate
fail2ban-client set logtarget /var/log/fail2ban.log >/dev/null
endscript
# If fail2ban runs as non-root it still needs to have write access
# to logfiles.
# create 640 fail2ban adm
create 640 root adm
Add Repeat Offender rule:

vi /etc/fail2ban/jail.local
Add the following to the bottom of the line. This sets any IP that has been failing 10 times for a period of 365 days will be banned forever

#
# Repeated Offender to be banned forever
# if has been baned 10 times for 1 year
#
[repeatoffender]

enabled  = true
filter   = repeatoffender
action   = repeatoffender[name=repeatoffender]
           sendmail-whois[name=Repeat-Offender, dest=me@domain.me, sender=fail2ban@domain.me]
logpath  = /var/log/fail2ban*
maxretry = 10
findtime = 31536000
bantime  = -1
Create fail2ban Filter:

vi /etc/fail2ban/filter.d/repeatoffender.conf
Add the following to the file:

# Fail2ban configuration file
# this filter monitors the fail2ban log file and permanently bans the IP
#
# only works with iptables

[Definition]
_jailname = repeatoffender
failregex = fail2ban.actions:\s+WARNING\s+\[(?:.*)\]\s+Ban\s+
ignoreregex = fail2ban.actions:\s+WARNING\s+\[%(_jailname)s\]+Ban\s+
Create fail2ban Action:

vi /etc/fail2ban/action.d/repeatoffender.conf
Add the following to the file:

# Fail2Ban configuration File
#
#

[INCLUDES]

before = iptables-blocktype.conf

[Definition]

# Option: actionstart
# Notes.: command executed once at the start of fail2ban
# Values: CMD

actionstart = iptables -N fail2ban-
              iptables -A fail2ban- -j RETURN
              iptables -I  -p  -j fail2ban-
              # Establish chain and blocks for saved IPs
              iptables -N fail2ban-ip-blocklist
              iptables -A fail2ban-ip-blocklist -j RETURN
              iptables -I  -p  -j fail2ban-ip-blocklist 
              cat /etc/fail2ban/ip.blocklist. |grep -v ^\s*#|awk '{print $1}' | while read IP; do iptables -I fail2ban-ip-blocklist 1 -s $IP -j REJECT --reject-with icmp-port-unreachable; done

# Option:  actionstop
# Notes.:  command executed once at the end of Fail2Ban
# Values:  CMD
#
actionstop = iptables -D  -p  -j fail2ban-
             iptables -F fail2ban-
             iptables -X fail2ban-
             # Remove chain and blocks for saved IPs to prevent duplicates on service restart
             iptables -D  -p  -j fail2ban-ip-blocklist
             iptables -F fail2ban-ip-blocklist
             iptables -X fail2ban-ip-blocklist

# Option:  actioncheck
# Notes.:  command executed once before each actionban command
# Values:  CMD
#
actioncheck = iptables -n -L  | grep -q 'fail2ban-[ \t]'

# Option:  actionban
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    See jail.conf(5) man page
# Values:  CMD
#
actionban = VERIFY="*"
            ADD="        # fail2ban/$( date '+%%Y-%%m-%%d %%T' ): Perma-Banned"
            FILE=/etc/fail2ban/ip.blocklist.
            grep -q "$VERIFY" "$FILE" || iptables -I fail2ban-  1 -s  -j DROP
            grep -q "$VERIFY" "$FILE" || echo "$ADD" >> "$FILE"

# Option:  actionunban
# Notes.:  command executed when unbanning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    See jail.conf(5) man page
# Values:  CMD
#
actionunban = # Do nothing becasuse their IP is in the blocklist file

# To manually unban from the ip blocklist file run this command:
# Be warned that if the ip is in log rotated files it must be whitelisted
#
# sed -i '/^/d' /etc/fail2ban/ip.blocklist.repeatoffender
#

[Init]

# Default name of the chain
#
name = default

# Option:  protocol
# Notes.:  internally used by config reader for interpolations.
# Values:  [ tcp | udp | icmp | all ] Default: tcp
#
protocol = tcp

# Option:  chain
# Notes    specifies the iptables chain to which the fail2ban rules should be
#          added
# Values:  STRING  Default: INPUT
chain = INPUT
that's it. to check the iptables, to show which IP is blocked there, just run:

iptables -L -n



Securing SSH Server with fail2ban and Email Notification

I use fail2ban to secure my SSH server, using the following guide

https://help.ubuntu.com/community/Fail2ban

this helps me to ban the IP address and notify me by email of the failed attempt
and I use the following script to notify me by email of the successful login

edit or create /etc/sshd/sshrc

DATE=`date "+%d.%m.%Y--%Hh%Mm"`
IP=`echo $SSH_CONNECTION | awk '{print $1}'`
REVERSE=`dig -x $IP +short`
echo "Subject: SSH Login Successfully" > /tmp/mail.content
echo "$DATE, user $USER just logged in from $IP ($REVERSE)" >> /tmp/mail.content 
sendmail -f "MyBox <fromemail@domain.tld>" -t "Lau, Laurence <me@domain.tld>" -s smtprelay.domain.tld < /tmp/mail.content &


FreeNAS 8 and Time Machine

I have just finished setting up the FreeNAS 8 on Hyper-V to backup my MAC using Time Machine.

Tricky bits settings up FreeNAS 8 on Hyper-V:

  • Remove the default NIC when creating VM
  • Add a new NIC, must be legacy NIC
  • FreeNAS 8 does not recognize SCSI disks, only IDE
  • After installed, change the IP to static
  • do: ifconfig to find out the adapter name
  • do: ifconfig down
  • do: ifconfig up

Once the FreeNAS is up and running, go to its web console:
  • Create a group called: backup-group
  • Create a user called: backup-user, with primary group ID: backup-group, enter the password
  • Go to Services, enable AFP
  • Go to AFP Settings, make sure Local Access is ticked
  • Go to Storage, and Create ZFS Data Set, called backup-apple
  • Once it is created, edit its permissions, make sure owner-user is changed to backup-user and owner-group is changed to backup-group. Tick the option to have the Group the write access
  • Go to Share, add Apple Share. Name it backup, path: /mnt/data/backup-apple. Make sure the Disk Discovery is ticked, Disk Discovery Mode is set to Time Machine
From my MAC, start Finder:
  • Click Go, and select Connect to Server
  • Enter Server Address: afp://freenas-ip
  • It will ask you for the username: backup-user, password: password
  • Start Console, type:defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1
  • Start Time Machine and Select Disk, select the backup

Thanks for reading :)

ESXi 4.1 NIC Driver Update

To update the ESXi 4.1 host with the latest NIC driver

  • Download the ISO file from VMware
  • Extract the ISO file and find the offline-bundle ZIP file
  • SCP this ZIP file to the ESXi host (e.g. /tmp/driver.zip)
  • Run the following command

esxupdate –bundle=/tmp/driver.zip update

reboot

Linux History Bash

To check the login history:
# last

To clear out the login history:
# > /var/log/wtmp

To check the last command run:
# history

To clear out the command history:
# history -c

Trinity Rescue CD

Have you ever have to restore your local administrator password because you forget the password of it?

The best boot CD so far:
http://trinityhome.org

Gmail and Hotmail Fetcher

I use fetchmail to fetch emails from any POP3 account. Since Gmail support POP3 (you need to enable it), I can fetch it use the following command:

poll pop.gmail.com proto pop3 and options no dns
user
'myemail@gmail.com' there with password 'mypassword' is myforwardemail@localemail.com options ssl

Hotmail is a little bit trickier, because it is a HTTP based email.
To Fetch Hotmail, I use GetLive:

Before you set GetLive, make sure you set your 'reading pane' to off in your Hotmail setting
Get GetLive from the Internet (just Google it)
Create a config file (getlive.conf):

UserName=myusername
Password=mypassword
Domain=hotmail.com
Downloaded=/var/log/getlive.log
processor=sendmail -i
myforwardemail@localemail.com
Delete=Yes
Folder=Inbox
mode=200810


and create a cron job to execute:

GetLive.pl --config-file getlive.conf

Change DST 2007 for Redhat Linux

In March 2006 the BC goverment decided to follow the US and change the daylight savings time starts and ends. So PD will start on March 11 2007.

To update your Redhat Linux DST, do the following

Check your local time zone
less /etc/sysconfig/clock

once you have found your zone, download the zonefile (e.g. mine is America/Detroit)
wget http://andrew.triumf.ca/northamerica.rules.txt

Check the old zone file
zdump -v /usr/share/zoneinfo/America/Detroit grep 2007

Compile the rules with zic
zic northamerica.rules.txt

Link the localtime file
ln -sf /usr/share/zonefile/America/Detroit /etc/localtime

Check the new localtime
zdump -v /etc/localtime grep 2007

You should see like this:
/etc/localtime Sun Mar 11 06:59:59 2007 UTC = Sun Mar 11 01:59:59 2007 EST isdst=0 gmtoff=-18000
/etc/localtime Sun Mar 11 07:00:00 2007 UTC = Sun Mar 11 03:00:00 2007 EDT isdst=1 gmtoff=-14400
/etc/localtime Sun Nov 4 05:59:59 2007 UTC = Sun Nov 4 01:59:59 2007 EDT isdst=1 gmtoff=-14400
/etc/localtime Sun Nov 4 06:00:00 2007 UTC = Sun Nov 4 01:00:00 2007 EST isdst=0 gmtoff=-18000

Nice Partition Utility

Ever wonder how to partition your HDD or re-size with zero cost? :)
Try this:

Gnome Partition Editor

It is a LIVE CD which you can boot and manage your partition. It supports NTFS!