This is an assignment h2 - Linux palvelimet ICT4TN021-7 in Haaga-Helia University of Applied Sciences

In this second assignment we were assigned in getting to know the terminal in Linux system. Object was to read logs, install SSH and get it up and running, installing your favourite softwares with terminal and lastly install three new CLI (Command Line Interface) softwares.

Syslog (Journalctl)

I’m using Arch Linux x86_64 as my OS for this assignment. First part of our assignment was to make two different entries in our system’s log. For this part I used systemd’s own logging system journalctl, which works the same way as syslog daemon. You can read more about systemd in here.

To read the log, use:

$ journalctl

Running that command alone outputs a lot of data, especially when you have had systemd running on your system for a long time. To filter output you need to append different options to the end of your command.

E.g. to show all messages from this boot, use:

$ journalctl -b

Note: You can also show messages from the previous boots with journalctl -b 1. That command shows messages from the previous boot and journalctl -b 2 shows messages from the second previous boot and so on.

To read messages in real time, use:

$ journalctl -f

Here’s an example of failed and successful

Failed log:

tammi 26 14:58:49 arch sudo[3403]: pam_unix(sudo:auth): authentication failure; logname= uid=1000 euid=0 tty=/dev/pts/0 ruser=topi rhost=  user=topi

Here you can see the main cause for this failure is sudo authentication failure, which happens when you enter wrong password for sudo. From this log you can see that log entry came from UID of 1000, which in my case represents my own user called topi. (To show UID used in your system run cat /etc/passwd) EUID stands for effective user id, which in this case represents root user. tty prints the file name of the terminal connected to standard input.

Successful log:

tammi 26 14:59:46 arch sudo[3403]:     topi : TTY=pts/0 ; PWD=/home/topi ; USER=root ; COMMAND=/usr/bin/pacman -Syu
tammi 26 14:59:46 arch sudo[3403]: pam_unix(sudo:session): session opened for user root by (uid=0)
tammi 26 14:59:47 arch sudo[3403]: pam_unix(sudo:session): session closed for user root

Here is succesful message from journalctl. Here you can see that I was running command sudo pacman -Syu, which updates all my systems packages with root privileges, as user topi. Line after that tells you what the working directory was when I was running the command. In this case it was /home/topi. Below that you can see that root session was opened successfully and closed when the command was completed.

Installing SSH

Installing OpenSSH on Arch Linux

$ sudo pacman -S openssh

For this part of the assignment I’ll use my own server for examples. I have my server already up and running, but I’ll tell how I set connect to it and how I set my key management between my local computer and remote server.

Update 22.2.2018: Removed Ubuntu commands from this part as unnecessary

Connecting to the Server with SSH

Note: Also for security reasons I’m not using my real username and ip-address to my server in these examples.

First time you log in server isn’t identified on your local machine, so you’ll be asked if you want to continue:

The authenticity of host 'ip-addr (ip-addr)' can't be established.
ECDSA key fingerprint is SHA256:ETqEVgksBX+yvLgRMnxitcRXiztWgr7lqcRM2unaSkI.
Are you sure you want to continue connecting (yes/no)? 

After you’ve answered to that you’ll be prompted for your users password:

[email protected]'s password:

And now you are in your server!

Allow SSH Connections

While we are logged to our server we also need to make sure that our firewall allows SSH connections so we can log back in (assuming you have enabled your firewall before this). This can be done with the command:

$ sudo ufw allow OpenSSH

This makes new rules for ufw to allow IPv4 and IPv6 traffic to port 22, which is the default port for SSH connections.

SSH Keys

You can add extra layer of security on you server with generating public and private SSH keys. You can then place your public key on your server and then unlocking it by connecting with the client that has the private

Generating RSA Key Pair

Here we are generating RSA key pair on your local computer. You can read more about RSA cryptosystem here.

$ ssh-keygen -t rsa

-t flag in this command specifies the type of key to be generated. In this case I used rsa, which then generated RSA key pair. On default without any flags ssh-keygen generates RSA key pair. But with this flag you can generate DSA, ECDSA or ED25519 key pair by giving value dsa, ecdsa or ed25519 in place of rsa.

After that you are asked of saving location for key pair:

Enter file in which to save the key (/home/topi/.ssh/id_rsa):

Then you are asked for passphrase:

Enter passphrase (empty for no passphrase):

After that ssh-keygen -t rsa outputs key’s fingerprint and randomart:

I had already generated mine key pair so this is an example output of ssh-keygen.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |

When that is done you can now copy you public key to your server via ssh-copy-id.

When you want to send your public key to your server use:

$ ssh-copy-id [email protected]

That command makes authorized_keys file with your public key in it in your server. Now when you try to login to your server it’ll ask you for your SSH keys passphrase:

Enter passphrase for key '/home/topi/.ssh/id_rsa': 

Update 24.2.2018: Added example login with SSH key pair.

Transferring Files via SSH

If you want transfer file via SSH between your local system and server, there are a few option for you. Most popular options for this are most likely scp and rsync.

Using scp:

$ scp ~/test.txt [email protected]:~/test

This transfers test.txt from your local machine to your server’s test folder.

This also works for whole folders and also the other way around:

$ scp -r [email protected]:~/test ~/test

*Note: ~/, which indicated home directory, is not necessary in scp since it assumes you are sending files to destination’s home directory.

Since rsync is new for me I’ll use it below at the part of the assignment where I test new CLI softwares.

Apt-Get of Your Dreams

Next part of our assignment was to build an apt-get command of our dreams. So one command that installs my favourite softwares. In my case this is actually relatively short command since I don’t use that many programs on a daily basis.

Emacs comic © xkcd

Install in Arch Linux

$ sudo pacman -S emacs git eclipse-jee firefox openssh
  • Emacs: Emacs is my editor of choice for almost everything.

  • Eclipse-JEE: Eclipse’s Java EE IDE. Java development, since unfortunately I have to this for many different school projects, is pretty much the only thing that I don’t do in Emacs. Ubuntu for some reason doesn’t even offer JEE IDEs and only offers an old version of Eclipse in their packet manager (it’s still possible to install via tar.gz file or Ubuntu’s PPAs). Thankfully in Arch, you can download the latest version of Eclipse straight from the package manager.

  • Git: Version control system

  • Firefox: Web browser

  • OpenSSH: Discussed above.

Update 22.2.2018: Removed Ubuntu commands from this part as unnecessary

Install CLI Softwares

1. Rsync


Install rsync in Arch Linux

$ sudo pacman -S rsync

Rsync is installed in Ubuntu by default

Rsync is a great tool for syncing folders and files between your remote machine and local machnine:

Example usage:

$ rsync -a ~/test [email protected]:~/test

This syncs test folder from your local machine with the test folder in your remote machine. Rsync transfers only the part of file that has changed between your local computer and remote machine, in the scenario where you need to update some folder relatively often.

2. R


Install R in Arch Linux

$ sudo pacman -S r

Install R in Debian/Ubuntu

How to install R on Ubuntu 16.04

R is a programming language and an environment for statistical computing and graphics. Lately I’ve been using R as my calculator so my knowledge of R is currently quite superficial. R is still something that I would love to learn more, since it seems to be relatively popular choice of programming language.

R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.


Example calculation:

> 5 + 5
[1] 10

3. Nethack


Install Nethack on Arch Linux

$ sudo pacman -S nethack

Nethack is a single-player rogue-like dungeon crawling game. Nethack is relatively old game and quite well known. I’m not a big gamer myself so this is not necessary my cup of tea, but the game itself is definitely quite interesting.

Salutations topi, the human Knight, welcome back to NetHack!

                      |[email protected]|

Topi the Gallant         St:16 Dx:9 Co:10 In:8 Wi:14 Ch:18  Lawful
Dlvl:1  $:0  HP:16(16) Pw:4(4) AC:3  Exp:1