Sunday, May 29, 2016

Access AWS Linux instance over SSH


How to access your AWS Linux instance over SSH?

There are two options:

1. Access with your .pem key file, which you obtained from AWS when creating an instance

2. Access with password



Before you will be able to ssh into your instance via option 2., you will have to set up the account and password via option 1.


Option 1. steps:

1. Log into your AWS web portal and find your Linux instance in the instances list

2. Right click on the instance, and click on the 'connect' option

3. You will find your Public DNS address (e.g. ec2-your-instance.us-your-region.compute.amazonaws.com)

4. Find your .pem key file path (e.g. /user/doc/my-key-file.pem), and do ssh in your client by

ssh -i /user/doc/my-key-file.pem ec2-user@ec2-your-instance.us-your-region.compute.amazonaws.com

Note: ec2-user is the default user name for your Linux instance. If it happens to be different, refer to this for details of how to obtain it. If it asks you about RSA key fingerprint, just type yes.

5. If You have successfully logged into the instance over SSH after previous 4 steps, you will now be able to set up password access over ssh to your instance, so that you don't always need your .pem key file.


Option 2. set up password access:

1. ssh -i /user/doc/my-key-file.pem ec2-user@ec2-your-instance.us-your-region.compute.amazonaws.com

2. $ cd /

3. $ sudo useradd -s /bin/bash -m -d /home/USERNAME -g root USERNAME

4. $ sudo passwd USERNAME
Enter new pwd:
Retype new pwd:

5. add the user as a sudoer (optional):

$ sudo visudo
and add the following line to the file:
USERNAME  ALL=(ALL:ALL) ALL

6. enable password access

$ sudo vi /etc/ssh/sshd_config
and change PasswordAuthentication from 'no' to 'yes'

7. restart ssh service by

$ sudo /etc/init.d/ssh restart

or

$ sudo /etc/init.d/sshd restart

9. log out and log in using

$ ssh USERNAME@ec2-your-instance.us-your-region.compute.amazonaws.com
Enter pwd:









Thursday, May 26, 2016

Trie


What is a Trie data structure?

If you know what a Tree data structure is, you can think of a Trie as an ordered tree, such that it is similar to a Binary Search Tree but it it not necessary to have both key and value given a node of the tree. The node has a value if and only if it has a valid key.

Let's look at an example.

Trie is especially powerful and commonly used when dealing with letters and words.


The above is a trie example from Wikipedia that uses words as key and integer as value.

It takes string as an input and outputs an integer value if there is a match. So trie_example.get("tea") will return an integer 3, and trie_example.get("te") will return null since there is no value associated with that node. "tea" is considered a valid key while "te" is not, since we define the key is valid if it is a valid English word.

This kind of trie can be very powerful in time complexity when dealing with words, but it also uses a lot of space especially when there are lengthy words. A space-optimized trie called Radix tree can be used to solve this problem. Use word matching as an example, radix tree will not only store one letter in each node but as many as its children they share.