How to SSH into Linux System with Private Key

Introduction

By default, Linux allow users to SSH into the system using only password without providing any private key. This is actually an insecure method of authentication as any users can SSH into the system as long as they have the username and password. Therefore we add an additional layer of security for the SSH authentication using owner’s private key.

First we need to generate an SSH-Key. The key format required for Windows and Linux are different, .ppk format are required by Windows while Linux can accept any kind of format as long as the key are in OpenSSH format.

For Windows User

First we have to generate a key pair that contain a public key and a private key using PuttyGen (Download PuttyGen here). After downloading the program, launch it. Follow the following steps to generate a key pair:

  1. For type of key to generate, choose RSA from the list.
  2. Click on Generate button under Actions to start the generation.
  3. Move your mouse pointer around in the blank area of the Key section, below the progress bar (to generate some randomness) until the progress bar is full;
  4. A private/ public key pair has now been generated;
  5. In the Key comment field, enter any comment you’d like, to help you identify this key pair, later (e.g. your e-mail address; home; office; etc.) — the key comment is particularly useful in the event you end up creating more than one key pair;
  6. Optional: Type a passphrase in the Key passphrase field & re-type the same passphrase in the Confirm passphrase field (if you would like to use your keys for automated processes, however, you should not create a passphrase);
  7. Click the Save public key button & choose whatever filename you’d like (some users create a folder in their computer named my_keys);
  8. Click the Save private key button & choose whatever filename you’d like (you can save it in the same location as the public key, but it should be a location that only you can access and that you will NOT lose! If you lose your keys and have disabled username/password logins, you will no longer be able log in!);
  9. Right-click in the text field labeled Public key for pasting into OpenSSH authorized_keys file and choose Select All;
  10. Right-click again in the same text field and choose Copy.
  11. SSH into target machine to upload the public key.
  12. Go to ~/.ssh/authorized_keys and copy over the content.
  13. Now you should be able to SSH into the target server by providing .ppk format private key.

NOTE: PuTTY and OpenSSH use different formats for public SSH keys. If the SSH Key you copied starts with “—- BEGIN SSH2 PUBLIC KEY …”, it is in the wrong format. Be sure to follow the instructions carefully. Your key should start with “ssh-rsa AAAA ….”

For Linux User

Linux user require no special program to generate SSH key pair. Follow the following steps below to generate a key pair:

  1. Type the following command into the terminal.
    ssh-keygen -t rsa -b 2048 -v
  2. You should see the following message after you enter the above command:
    Generating public/private rsa key pair.
    Enter file in which to save the key (~/.ssh/id_rsa): easierit
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in easierit.
    Your public key has been saved in easierit.pub.
    The key fingerprint is:
    bb:c6:9c:ee:6b:c0:67:58:b2:bb:4b:44:72:d3:cc:a5 localhost@localhost
    The key's randomart image is:
  3. You should now have two key files under ~/.ssh.
    id_rsa
    id_rsa.pub
  4. Next, we need to upload the public key to the server we will be SSH into using the following command.
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@12.34.56.78
  5. Now try logging into the machine, with ssh root@12.34.56.78, and check in: ~/.ssh/authorized_keys to make sure we haven’t added extra keys that you weren’t expecting, you may still want to use a password. Your authorized_keys file should look something like this.
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAX ...
  6. Now you can try to login with your private key from original Linux terminal using the following the command.
    sudo ssh -i ~/easierit.pem root@12.34.56.78

Enable Services on Startup in Linux

The chkconfig command can also be used to activate and deactivate services. The chkconfig --list command displays a list of system services and whether they are started (on) or stopped (off) in run levels 0-6.

abrt-ccpp          0:off  1:off  2:off  3:on   4:off  5:on   6:off
abrtd              0:off  1:off  2:off  3:on   4:off  5:on   6:off
acpid              0:off  1:off  2:on   3:on   4:on   5:on   6:off
atd                0:off  1:off  2:off  3:on   4:on   5:on   6:off
auditd             0:off  1:off  2:on   3:on   4:on   5:on   6:off
autofs             0:off  1:off  2:off  3:on   4:on   5:on   6:off
blk-availability   0:off  1:on   2:on   3:on   4:on   5:on   6:off
certmonger         0:off  1:off  2:off  3:on   4:on   5:on   6:off
cgconfig           0:off  1:off  2:off  3:off  4:off  5:off  6:off
cgred              0:off  1:off  2:off  3:off  4:off  5:off  6:off
cpuspeed           0:off  1:on   2:on   3:on   4:on   5:on   6:off

For example, if you wish to enable apache2 service in your system during startup, you can use the following command to activate it:

chkconfig httpd on

Similarly to turn off the service, use:

chkconfig httpd off

My thoughts on Laravel and Django

I am a self-taught web developer, having my first personal website built since I was around 14 years old back in year 2004. During that time, the Internet was just starting to be more helpful in gathering resources and information. Most of the website was built in HTML or PHP. So I started out my first webpage using PHP, without framework like Laravel.

Recently I began to switch from framework-less to using framework after I have not develop some web for some period of time. The first proper PHP framework that I had spent time learn is Laravel. Due to the nature and my previous experience on PHP, I was able to quickly pick up the framework within a month and start to build website in Laravel framework.

In my opinion, Laravel is way more easy to code PHP compared to without using any framework, aka. code from scratch. Laravel already provide a decent template and structure for me to start. It provides a proper way to manage URL routing which I couldn’t done it easily in previous method of coding where I have to make use of URL rewrite engine. Laravel also let me write my code in modular way with the MVC (Models, Views and Controllers) structure. I organized all my algorithms and logics in the Controllers while leaving the presentation layers in Views. The template inheritance saves me tons of time from duplicating same template over and over again, which in turn create more mess in the end. Database management is also an ease with the help of migrations. I can keep track of any changes I made to the database using different migration files. This also allow consistency across different server if I were have to migrate my website.

After awhile of coding with Laravel, I realized something I need is missing, the multithreading. Even though I have multiple CPU cores in my server, I cannot fully make use of these cores to complete my tasks faster. By nature, PHP is not a recommended language for multi-threaded processing. When I was dealing my research projects which required multi-threaded programming at the back end of the server, PHP cannot fulfill my needs. So I started to try out those modules that allow PHP to perform the tasks above. In the end, these modules generate more overhead than solving my problem.

I decided to try out django which is based on Python. That time, I have zero Python knowledge as I never learnt or used Python before. Fortunately, my knowledge in Java helped me learn Python easier due to similar concept and structure. After learning up django, what I would say is that, django is awesome! Multi-threaded programming is possible as everything can be done in Python! It’s more lightweight than Laravel because Laravel require tons of dependencies to be installed before started. Both django and Laravel have similar MVC structure, so switching from one to another doesn’t take much time.

There are some stuff where I feel is different in Laravel and django. For example:

  1. Editing setting during runtime is allowed in Laravel, but not in django. Trying to do so in django will give you errors.
  2. Variable processing in views is allowed but not recommended in Laravel. It is totally disabled in django unless using template tag.
  3. Laravel does not make use of regex while writing URL routing. You have to learn regex in order to properly write URL routing in django.
  4. django has a built in database administration panel. You can also manage your database in Laravel using PHPMyAdmin.

To me, both of the framework are decent in each language. If I need to write multi-threaded processing algorithms, I will choose django, else I might choose Laravel if I need to integrate with those old PHP system. It’s always better to work with sometime you already familiar with.

How to Install Windows 10 Cumulative Patch KB3189866

How to Install Windows 10 Cumulative Patch KB3189866
I’m seeing posts all over the web from folks trying to install the latest Windows 10 Anniversary Update (version 1607) cumulative update. KB3189866 is supposed to bring Win10 AU to build 14393.187, but reports say the installer gets stuck at 45 percent, 48 percent, 95 percent, and various other points along the way.The problem’s widespread, and Microsoft has known about it since shortly after the patch dropped. Since it’s a cumulative update, there’s no easy solution.

Microsoft will certainly figure out what’s blocking the patch, but in order to fix it the company will have to release a new cumulative update. Unless it can find a magic way to modify a released cumulative update, the only solution is a new cumulative update that absolves the sins of the original. That’s one of the big problems with these massive, one-way “as a service” patches. It’s also a totally predictable problem.
If you’re faced with this issue, be of good cheer: There’s an easy solution. Some suggest that you crank up Internet Explorer and download the patch from the Microsoft Update Catalog. There are links for the 32- and 64-bit versions of Win10, but they require Internet Explorer and the installation of an ActiveX control.
Fortunately, there’s a trick for getting to the Microsoft Update Catalog without using IE and without installing a 1990s-era ActiveX program. Make sure you’re running Windows 10 Anniversary Update, version 1607 (type winver in the Cortana Search box). You can then use any browser to click on this link.

If you have an RSS recognition add-on in Firefox or Chrome — for example “RSS Subscription extension (by Google)” for Chrome — you will immediately see links for downloading KB 3189866. They are:

  • Cumulative Update for Windows 10 Version 1607 for x64-based Systems (KB3189866) — this is the 64-bit version of the patch
  • Cumulative Update for Windows Server 2016 for x64-based Systems (KB3189866) — the version for Server 2016
  • Cumulative Update for Windows 10 Version 1607 (KB3189866) — which is the 32-bit version

If you don’t have an RSS recognition add-on, you see raw RSS text, which is pretty easy to decipher. Look for those three entries.

Click on the link to go into the Microsoft Update Catalog, click Download Now, click on the MSU file you want to download, choose to use the Standalone Update Installer, and click OK.

KB3189866 will install itself; you reboot and carry on.

Helper Service in Laravel 5.2

Helper Service in Laravel 5.2

First, create a new PHP file in app\Helpers directory, for example UserServiceProvider.php.

<?php

use Illuminate\Support\Facades\Session;

function is_logged_in()
{
    if (Session::get("user_id") != "") {
        return true;
    }
    return false;
}

Next, create new providers using php artisan make:provider HelperServiceProvider in the console. Then update the file content as followed:

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        require base_path() . '/app/Helpers/UserServiceProvider.php';
    }
}

Lastly, add the reference to the helper provider in config\app.php, as followed:

'providers'=> [
    
    ...
    App\Providers\HelperServiceProvider::class,
    ...

]

Now, you can call the helper function anywhere by just calling the method is_logged_in().

MySQL Clone Database

MySQL Clone Database

To clone a database named database_one into ‘database_two’ in MySQL, the following command can be used:

$root@server: mysqldump database_one -u username -p > database_one.sql
$root@server: mysql database_two -u username -p < database_one.sql

where username is the username of your MySQL database.