openssh. Problem with connection to servers via ssh

One of these days I faced the following problem. I needed to organize the ability to work on two computers with servers via ssh. On one machine a public and private key was generated, a config file was configured, everything worked well. Next, I copied the keys to another machine, also set up config file and verified that the key was added ($ ssh-add -l), but when I was trying to connect to any server I was failing. Both machines were on Ubuntu OS (17.10 and 16.04 LTS).
Next, I tried to test connection:
$ ssh -v your-useful.server.com
Output was next:
...
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC:
 compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC:
 compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
On the last line, the check was froze and ended after a while. After a short searching based on the last line of output, it turned out that problem was associated with a bug in the openssh package. The bug was revealed in the fact that each connection was required to refine the cipher via the -c key.
$ ssh -c aes256-ctr your-useful.server.com
After that, everything began to work. To exclude redundancy, you need to add either in /etc/ssh/ssh_config or in ~/.ssh/config (I chose this option) the following line:
Ciphers aes128-ctr,aes192-ctr,aes256-ctr

Ruby. Null-aware operators

1. The safe navigation operator

There are some languages that support object-oriented programming that have support for the so-called the safe navigation operator (SNO). Also it's known as optional chaining operator, safe call operator or null-conditional operator. The SNO is a binary operator that returns null if its first argument is null;
otherwise it returns the second argument.

It's used to avoid sequential explicit null checks and assignments and replace them with method/property chaining. In programming languages where the navigation operator (e.g. ".") leads to an error if applied to a null object, the safe navigation operator stops the evaluation of a method/field chain and returns null as the value of the chain expression. It's currently supported in languages such as Apache Groovy, Ruby, Swift, C#, Kotlin, CoffeeScript and others. There is currently no common naming convention for this operator, but SNO is the most widely used term.

The main advantage of using this operator is that it solves a problem commonly known as pyramid of doom. Instead of writing multiple nested ifs, programmers can just use usual chaining, but put question mark symbols before dots (or other characters used for chaining).

Ruby supports the &. safe navigation operator (also known as the lonely operator) since version 2.3.0

Let's consider concrete example of using safe navigation operator on Ruby. My current version of Ruby is 2.5.1

Let's say that we have two classes: Owner and Account:

class Owner
  attr_accessor :first_name, :last_name, :age

  def initialize(first_name:, last_name:, age:)
    self.first_name = first_name
    self.last_name = last_name
    self.age = age
  end

  def summary
    "Owner: first name: #{first_name}, last name: #{last_name}, age: #{age}"
  end
end

module Roles
  GUEST = 1
  USER = 2
  ADMIN = 4
end

class Account
  include Roles

  attr_accessor :owner, :role

  def initialize(owner, role = GUEST)
    self.owner = owner
    self.role = role
  end
end

Create instances of these classes like that

owner = Owner.new(
  first_name: 'John',
  last_name: 'Smith',
  age: 30
)

account = Account.new(owner, Roles::USER)

Now we can get a full information about owner from account by the following way

puts account.owner.summary

But what if, while creating instances of our classes, something goes wrong? Then when you try to get the full information about owner from account you won't have guarantees that this operation will be successful. Moreover, we can get an error and our system may fall. Here is an example of such a situation

account = Account.new(nil)
puts account.owner.summary

As result we get next error:

undefined method `summary' for nil:NilClass (NoMethodError)

So, to avoid this, you can use safe navigation operator &.

puts account&.owner&.summary

And instead of an error, we get nil (it's a null in Ruby)

2. The null coalescing operator

The null coalescing operator (called the Logical Defined-Or operator in Perl) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages. While its behavior differs between implementations, the null coalescing operator generally returns the result of its first operand if it exists and isn't null.

In contrast to the ternary conditional if operator used as x ? x : y, but like the binary Elvis operator used as x ?: y, the null coalescing operator is a binary operator and thus evaluates its operands at most once, which is significant if the evaluation of x has side-effects.

In the ruby for these purposes, we can use the operator ||

some_variable = nil || 'default_value'

Git. Work with Bitbucket/GitHub repositories without username/password

Initially, you should have two generated keys: private - id_rsa and public - id_rsa.pub.

To do this, you need to run the command:

$ ssh-keygen -t rsa -b 4096 -f $HOME/.ssh/id_rsa


Next, you need to add your public key (its contents) via the web interface.
After that, you can work with your repositories without having to enter a password for every action you take.
But it's important to remember that you must work through ssh:// not through https://

If initially you cloned your repository like this

$ git clone https://git@REPOSITORY.git


then you need to change the remote URL to ssh://
To do this, run the command:

$ git remote set-url origin ssh://git@REPOSITORY.git


To look at current remote URL it's possible either that

$ git remote -v


or that

$ git config remote.origin.url

Git. Adding information about date and time to commit message

Sometimes it might be useful to add to commit information about current date and time.
So following commands show how you can do it:  

$ git add .
$ git commit -m "[feature-branch] Added utility files `date +'%Y-%m-%d %H:%M:%S'`"
$ git push origin feature-branch

Git clients for browsing git repository on Ubuntu

Nothing can replace the work with git in the console, but sometimes it's convenient to look at the repository from a bird's eye view. In this we can get help from different gui-clients.

There are a sufficient number of clients for different operating systems.
You can see them on this link - https://git-scm.com/downloads/guis.

There is a standard client on Ubuntu - gitk. But I didn't like it because it doesn't allow me to look at the entire repository. Only within one branch.

The following client - gitg - seemed to me quite good (look at the bellow picture).



You can test it. Install it on your unix system:

$ sudo apt-get install gitg

That's all. Go to the git repository, run next command and enjoy :)

$ gitg

Ubuntu. How to debug unix sockets like telnet

If you need to debug unix sockets like telnet service you can use netcat utility.

The syntax is:

$ nc -U /var/run/.../unixsocket.sock

Math. Combinatorics


The rule of sum.

If two actions A and B mutually exclude each other and action A can be performed in
k ways, and B - by n ways, then perform one of these actions (either A or B)
can be in k + m ways.

Example.

Let there be given two baskets, in one basket there are 8 red balls, in the other basket - 5 black
balls. How many ways can you pull out one ball? We can pull out either a red or a black ball.
By the rule of the sum we get that one ball can be pulled out in 8 + 5 = 13 ways.

The rule of product.

Let it be required to execute k actions sequentially. If the first action can be performed by N1
ways, the second action N2 ways, the third - N3 ways and so to the k-th action, which
can be performed in Nk ways, then all k actions can be performed in:

N = N1 * N2 * ... * Nk

ways.

Example.

Consider the baskets from the previous example. How many ways can you get two balls out?
First we can pull out either a red or a black ball. From the above example we know
that this can be done in 13 ways, so we can take out the second ball in 13 - 1 ways (one
the ball we pulled out at the first action), so we can take out 13 balls in 13 * 12 = 156 ways.

Combinations.

In combinations, the order of the elements is not important. That is, the following pairs of two symbols A and B in this case are identical: AB == BA.

Combinations without repetition.

Usually in this combination we can asks yourself: how many ways can I choose m elements from n elements?

 

Let's consider an example, let we have 4 books: A, B, C and D. We need to choose 2 books as a gift(!). How many ways can you do this? We need to choose from 4 items 2 items, and the order is not important. Therefore you need to find the number of combinations of 4 elements of 2.

       

As a result, we get 6 combinations: AB AC AD BC BD CD.
Note that here there are no repetitions (AA, BB, ...) and in fact AB is the same as BA.
The main thing is that in the set are two elements A and B.

Combination with repetition.

How many ways can be made up of n different types of combinations of m elements, if you do not take into account the order of the elements in the combination but elements of the same type can repeat?

 

Let's consider an example. In the confectionery shop were sold 4 varieties of cakes: A, B, C and D.
How many ways can we buy 2 cakes?

 

As a result, we get 10 combinations: AB AC AD BC BD CD AA BB CC DD. As we see, in combinations there are repetitions: AA BB CC DD.

Permutation.

In the layouts, unlike the combinations, the order of the elements is important. That is, the following pairs of two symbols A and B in this case are different: AB <> BA, because they have different orders of location.

Permutation without repetition. 

In this case, we ask the question: how many ways can I choose and place n different items in m places?

 

Let there be 4 symbols: A, B, C and D. How many ways you can combine two symbols from this set
avoid duplicating characters?

 

We got 12 ways:

AB AC AD BC BD CD
BA CA DA CB DB DC

Permutations with repetitions. 

Accordingly, how many ways can I choose and place n different objects in m places, not excluded among them the same

 

For example, let's use the condition of the problem from the previous example, but duplication of symbols is acceptable. That is, how many ways can the symbols A, B, C and D be combined in two characters?

 
We got 16 ways:

AB AC AD BC BD CD
BA CA DA CB DB DC
AA BB CC DD

Transpositions.

In transpositions, absolutely all elements of the set are considered. As well as in permutations
the order of the elements is important.

Transposition without repetition.

In this case, we ask the question: how many ways can I place n different subjects on n different places?

 

In fact, a transposition without repetitions is the permutation of n elements in n places.

 
Let there be given a string of 4 symbols - ABCD. How many ways can I rearrange the characters in this line?
 
In total 24 ways we can rearrange the characters in this line:

ABCD BACD CBAD DBCA
ABDC BADC CBDA DBAC
ADCB BDCA CDAB DACB
ADBC BDAC CDBA DABC
ACBD BCAD CABD DCBA
ACDB BCDA CADB DCAB

Transposition with repetition.

If there are identical elements among the n elements to be sorted, then we can ask:
How many ways can I rearrange n items that are sold in n different places, if there are among them
k of different types (k < n), that is, of the same types?

 
Example. 

How many different letters can be made from the letters of the word "mississippi"?
Here 1 letter "m", 4 letters "i", 4 letters "s" and 2 letters "p", only 11 letters.
Hence the number of permutations with repetitions is

 
Below is a diagram of the algorithm for choosing the appropriate way of working with a given set of elements.

Javascript. JSON-Schema validators (libraries)

If you need to validate your JSON-Schema I recommend next libraries:

- jjv (https://github.com/acornejo/jjv)

- djv (https://github.com/korzio/djv)

- ajv (https://github.com/epoberezkin/ajv)

jjv  is a simple library but hasn't a good performance. If you need to validate small json objects it might be good choice. However I recommend djv or ajv. As far as I know ajv is more popular than djv but all of them have good performance.

Ubuntu Desktop. Proxy ignore list

Recently, I faced with the following problem. We have a proxy server in the company.
Therefore, after setting up my test host on my local machine, I had to add it to the exception list.
Of course, I could configure browsers, but it wasn't entirely correct.
Therefore, it was better to add the host to the ignore list using the console.

If you want to view the list, run the command:

$ gsettings get org.gnome.system.proxy ignore-hosts
['localhost', '127.0.0.0/8', '::1']

To set your host to the ignore list, run the command:

$ gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.1', 'test.com', '*.test.com']"


As you can see in my case I added 'test.com' and '*.test.com'.

I hope that somebody this article will help to save time :)

Ubuntu Desktop 14.04. Install PostgreSQL

As you know PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.

Below is a short instruction on how to install PostgreSQL.

1. Let's install PostgreSQL:

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

2. Next, let's install graphical client pgAdmin for PostgreSQL:

sudo apt-get install pgAdmin3

After all installations you have done, add minimal changes to config files.

The working directory by default  is /etc/postgresql/{version}/main/
(the graphical installer installs in /opt/, then the working directory is
/opt/PostgreSQL-{version}/data). It contains the main configuration files.

1. Allow TCP/IP connections

In the configuration file postgresql.conf (the default is in
/etc/postgresql/{version}/main/), uncomment the line
#listen_addresses = 'localhost' by deleting the # character.

listen_addresses = 'localhost'

To connect to the server from other machines, the value 'localhost' should be replaced with the IP address of the machine, or 0.0.0.0, or simply put '*'.
All the necessary details can be found in the comments in the configuration file.

2. User settings

When you install the server, it creates a user postgres with a password postgres. These rights allow the server communicate with the operating system (as I know). But for work it's not enough. You must set a password for the same user in database. To do this, run the psql console client with the postgres user right:

sudo -u postgres psql

Then execute the query:

alter user postgres with encrypted password '_password_'

Where '_password_' is the password for connecting to the server under the user postgres.

3. Changing the authentication method:

In the configuration file pg_hba.conf (the default is in
/etc/postgresql/{version}/main/), change the authentication method for the postgres user to md5:

local   all         postgres                          md5

Restart the server:

sudo /etc/init.d/postgresql restart

pgAdmin configuration

1. Run pgAdmin
2. Create a new connection to the server: File-> Add server
3. Give the connection a name, specify it as the localhost host, as the postgres user with the password created when configuring the server
4. If everything is well configured, there should not be any problems.

Note: If you need a remote connection to the server, you need to make the following settings:

1. Add or edit the following line in your postgresql.conf:

listen_addresses = '*'

2. Add the following line as the first line of pg_hba.conf.
It allows access to all databases for all users with an encrypted password:

# TYPE DATABASE USER CIDR-ADDRESS  METHOD
host  all  all 0.0.0.0/0 md5

3. Restart the server:

sudo /etc/init.d/postgresql restart

psql console commands

Connect to database     \c  database_name
List of databases       \l
List of tables          \dt
List of columns         \d  table_name
Table description       \d+ table_name
List of users           \du
Exit from psql          \q

Server management commands

sudo service postgresql (start|stop|reload|restart|status)


Software versioning

The software life cycle might be very long. Changes in the program might be different - from correcting the error to full rewriting. Therefore we should reflect those changes in the software version. The following convention is recommended:

Major.Minor.Patch

Below you can see pictures for better understanding. More details are available on wiki-page.

Ubuntu. curl behind a proxy

1. Open default config file:

sudo nano ~/.curlrc

2. Add next setting

proxy = user:password@proxy-address:proxy-port

Ubuntu Desktop. add-apt-repository behind a proxy

1. Switch to super user (necessary):

sudo su

2. Then set two path variable:

export http_proxy="http://user:password@proxy-address:proxy-port/"
export https_proxy="http://user:password@proxy-address:proxy-port/"

3. After that you can add your repository:

add-apt-repository ppa:...

Tested on Ubuntu Desktop 16.04.3

Ubuntu Desktop. Solving the problem with sound

If you have trouble with sound like this. Sometimes it might disappear or appear by itself. Đ¢hen you can apply bellow recipe.

1. Open conf file:

nano /etc/pulse/daemon.conf

2. Find option realtime-scheduling, change option value from yes to no
and uncomment it:

..
; high-priority = yes
; nice-level = -11

realtime-scheduling = no
; realtime-priority = 5

; exit-idle-time = 20
; scache-idle-time = 20
... 

3. Then restart PulseAudio:

pulseaudio -k