Instructions to set up a basic LAMP+SSH server environment
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
35 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. Linux servers - Exercise 2
  2. ==============
  3. *Disclaimer:*
  4. --------------
  5. This exercise is a part of [Linux Server Administration (ICT4TN021, spring 2018) // Linux-palvelimet (ICT4TN021, kevät 2018)](http://www.haaga-helia.fi/fi/opinto-opas/opintojaksokuvaukset/ICT4TN021) school course organized as a part of Information Technology studies in Haaga-Helia university of Applied Sciences, Helsinki, Finland. Course lecturer [Tero Karvinen](http://terokarvinen.com/) has defined the original assignment descriptions in Finnish presented in this document in English. Answers and translations have been written by Pekka Helenius (me, ~ Fincer).
  6. *Table of contents:*
  7. --------------
  8. - [a) **Log events** Create two different log events: One successful event and one failed or forbidden event. Analyze the log lines in detail.](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#a-create-two-different-log-events-one-successful-event-and-one-failed-or-forbidden-event-analyze-the-log-lines-in-detail)
  9. - [Successful event example - Apache server](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#successful-event-example---apache-server)
  10. - [Failed event example - Apache server](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#failed-event-example---apache-server)
  11. - [Other error situations - libraries, headers & typos](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---libraries-headers--typos)
  12. - [Other error situations - unsupported Linux OS versions & broken libraries](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---unsupported-linux-os-versions--broken-libraries)
  13. - [Other error situations - a tricky error without clear identification](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---a-tricky-error-without-clear-identification)
  14. - [Other error situations - hardware issues](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---hardware-issues)
  15. - [Other error situations - random or unexpected errors](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---random-or-unexpected-errors)
  16. - [Other error situations - a tricky error tracked down](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---a-tricky-error-tracked-down)
  17. - [Other error situations - A simple test case (command which succeeds as root but fails otherwise)](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---a-simple-test-case-command-which-succeeds-as-root-but-fails-otherwise)
  18. - [b) **SSH daemon & commands** Optional task, not teached yet: Install SSH daemon. Try some of the following commands on your own SSH server: ssh-copy-id, sshfs, scp or git. (The easiest command might be scp: ‘scp foo.txt tero@example.com:’)](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#b-optional-task-not-teached-yet-install-ssh-daemon-try-some-of-the-following-commands-on-your-own-ssh-server-ssh-copy-id-sshfs-scp-or-git-the-easiest-command-might-be-scp-scp-footxt-teroexamplecom)
  19. - [c) **One-line installer** Create an apt-get command of your dreams: one single command or one-liner which installs your favorite applications.](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#c-create-an-apt-get-command-of-your-dreams-one-single-command-or-one-liner-which-installs-your-favorite-applications)
  20. - [d) **CLI programs** Install three new command line programs using your command line package manager. Try each of these programs in their target environment and purpose.](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#d-install-three-new-command-line-programs-using-your-command-line-package-manager-try-each-of-these-programs-in-their-target-environment-and-purpose)
  21. --------------
  22. **a)** Create two different log events: One successful event and one failed or forbidden event. Analyze the log lines in detail.
  23. --------------
  24. **Answer:**
  25. #### Successful event example - Apache server
  26. ```
  27. phelenius@my-machine:~$ cat /var/log/apache2/access.log
  28. 127.0.0.1 - - [24/Jan/2018:19:14:47 +0200] "GET / HTTP/1.1" 200 3525 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1 (KHTML, like Gecko) Version/11.0 Safari/605.1 Ubuntu/16.04 (3.18.11-0ubuntu1) Epiphany/3.18.11"
  29. 127.0.0.1 - - [24/Jan/2018:19:14:47 +0200] "GET /icons/ubuntu-logo.png HTTP/1.1" 200 3623 "http://localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1 (KHTML, like Gecko) Version/11.0 Safari/605.1 Ubuntu/16.04 (3.18.11-0ubuntu1) Epiphany/3.18.11"
  30. ```
  31. **Analysis:** In this event, user's client program (Web browser) has tried to access site **[localhost](https://en.wikipedia.org/wiki/Localhost)** for which Apache web server (HTTP daemon) has responded with OK code. The log snap reveals
  32. - client's IP address, in this case it is the same computer (`localhost` / `127.0.0.1`)
  33. - user ID (just a line in this case)
  34. - logging time
  35. - used HTTP method ([GET](https://www.w3schools.com/tags/ref_httpmethods.asp))
  36. - retrieved contents, using server defined root directory as root path (`/` and `/icons/ubuntu-logo.png`)
  37. - code 200 ([HTTP_OK](https://ci.apache.org/projects/httpd/trunk/doxygen/group__HTTP__Status.html#ga02e6d59009dee759528ec81fc9a8eeff))
  38. - target size 3623 (reported to the client)
  39. - [HTTP Referer](https://en.wikipedia.org/wiki/HTTP_referer) (client)
  40. - [User agent header](https://en.wikipedia.org/wiki/User_agent) reported by the client. According to the agent string, client browser has been gecko-based [Epiphany web browser](https://en.wikipedia.org/wiki/Epiphany_(GNOME)), using x86_64 processor architecture. This string can be manipulated in the client end. For instance, a desktop client web browser can pretend to be a mobile browser.
  41. Default syntax for Apache log files follow the layout which is described [here](https://httpd.apache.org/docs/2.4/logs.html) under section 'Common Log Format'.
  42. #### Failed event example - Apache server
  43. ```
  44. phelenius@my-machine:~$ cat /var/log/apache2/access.log
  45. 127.0.0.1 - - [24/Jan/2018:22:30:50 +0200] "GET /this-page-dont-exist HTTP/1.1" 404 510 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1 (KHTML, like Gecko) Version/11.0 Safari/605.1 Ubuntu/16.04 (3.18.11-0ubuntu1) Epiphany/3.18.11"
  46. 127.0.0.1 - - [24/Jan/2018:22:30:50 +0200] "GET /favicon.ico HTTP/1.1" 404 500 "http://localhost/this-page-dont-exist" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1 (KHTML, like Gecko) Version/11.0 Safari/605.1 Ubuntu/16.04 (3.18.11-0ubuntu1) Epiphany/3.18.11"
  47. ```
  48. **Analysis:** In this event, user's client program (Web browser) has tried to access site `http://localhost/this-page-dont-exist` for which Apache web server (HTTP daemon) has responded with error code. The log snap reveals the following:
  49. - client's IP address, in this case it is the same computer (`localhost` / `127.0.0.1`)
  50. - user ID (just a line in this case)
  51. - logging time
  52. - used HTTP method ([GET](https://www.w3schools.com/tags/ref_httpmethods.asp))
  53. - retrieved contents, using server defined root directory as root path (`/this-page-dont-exist` and `/favicon.ico`)
  54. - error code 404 ([HTTP_NOT_FOUND](https://ci.apache.org/projects/httpd/trunk/doxygen/group__HTTP__Status.html#gabd505b5244bd18ae61c581484b4bc5a0))
  55. - target sizes 500 & 510 (reported to the client)
  56. - [HTTP Referer](https://en.wikipedia.org/wiki/HTTP_referer) (client)
  57. - [User agent header](https://en.wikipedia.org/wiki/User_agent) reported by the client. According to the agent string, client browser has been gecko-based [Epiphany web browser](https://en.wikipedia.org/wiki/Epiphany_(GNOME)), using x86_64 processor architecture. This string can be manipulated in the client end. For instance, a desktop client web browser can pretend to be a mobile browser.
  58. Error logs of an Apache server can be found (by default) at `/var/log/apache2/error.log`. HTTP status codes with brief description are defined on [Apache homepage - Common HTTP Status Codes](https://wiki.apache.org/httpd/CommonHTTPStatusCodes).
  59. #### Other error situations - libraries, headers & typos
  60. The most common errors I have encountered on Linux desktop usage as a system administrator are various [Permission denied events](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Step_by_Step_Guide/s1-navigating-ownership.html) mostly as a consequence of a human error (forgetting sudo or something similar) or various typos in commands. I have compiled a lot of programs from source code, making various library errors practically unavoidable in long-term. The syntax of these errors is: [Cannot open shared object file: No such file or directory](https://stackoverflow.com/questions/480764/linux-error-while-loading-shared-libraries-cannot-open-shared-object-file-no-s/480786) where an executable can't find a requested `.so` library file or version from library path. Both of these errors can be avoided by typing commands precisely and avoiding compiling of software (and relying on official repositories and system-wide package upgrades, considering that installed packages are maintained actively).
  61. When compiling a program, common problems include
  62. - changes in [The GNU C++ Library API](https://gcc.gnu.org/onlinedocs/libstdc++/api.html) which may break compilation process of a C/C++ program on Linux.
  63. - missing header files (`.h` suffix), located at the same folder with original `.c` or `.cpp` files or at `/usr/include` folder or subfolders.
  64. #### Other error situations - unsupported Linux OS versions & broken libraries
  65. In long-term, using Linux distributions such as Ubuntu (or any variant that relies on Ubuntu package repositories, such as Linux Mint) become unusable with newer software. The newer versions require more recent library versions which older Ubuntu releases can't possibly provide anymore because official repositories have been shutted down. In this state, either older software must be used (not recommended) on older operating system or the system must be upgraded to more recent version where the newer packages are still available. In some extent, compiling programs from source on an older operating system version is possible and thus life cycle of an old operating system installation can be extended for some programs. This approach, however, leads easily to security risks and bugs which have not been fixed, and especially the security part is essential on Linux server installations.
  66. I have personal experience on using older Ubuntu 10.04 LTS after the official support dropped down. This operating system was running on desktop use, not in server environment. Thus, I accepted some security risks that may have been present in this approach. In software level, keeping the old Ubuntu installation led to major conflicts with libraries required by modern programs. I had older versions of some of these libraries installed but the newer versions introduced some major changes in function calls and methods so that compiling modern programs against these libraries was practically almost impossible (without major code patches). The most common library which broke first was [glibc](https://www.gnu.org/software/libc/). Ironically, some modern programs I couldn't use ran via [Wine](https://www.winehq.org). That's right: I couldn't use Linux versions but Windows versions ran flawlessly, on my Ubuntu 10.04 LTS.
  67. Since then, I have moved from Ubuntu to Arch Linux long time ago. Major reason for this is the rolling release model which Arch Linux uses. Compiling software from source is more flexible and, in theory, I don't have to reinstall my operating system never again due to "unsupported operating system version" issues.
  68. Based on my personal experience, rolling release model is not trouble free and can easily cause program/library conflicts but resolving these conflicts is flexible for an advanced Linux user. As an Arch Linux user, I must be aware of various conflicts which different updates and packages can cause, and I need to know it before I do anything crucial on my operating system. This means that updating software must never be done blindly and I need to be able to solve various error situations that might be present in the system. Usually these errors are poorly or misleadingly documented or not documented at all. Tracking down the root cause of some errors can be very tricky because of multiple components used by various programs.
  69. In general, some troublesome situation on Arch Linux consist of transitions from older API versions to a newer one. This has happened me once: from Qt4 to Qt5. The issue here is this: some packages are updated more frequently than the others, leading to mixing of two APIs in a system and therefore resulting to broken user experience.
  70. #### Other error situations - a tricky error without clear identification
  71. For instance, I recently bought a new laptop. I use Firefox on KDE 5 environment. My laptop has Optimus graphics. I have updated all Xorg packages, Mesa drivers, Nvidia drivers and Plasma workspace packages, not forgetting the web browser itself. In some cases, the browser just crashes, and not only the browser but the whole desktop and may lead to laptop freeze so that only hard-reset is a viable solution. In this case, I must try to figure out whether it is Optimus, Mesa, Nvidia, Plasma workspace (or any library in it) or browser component (add-on, compile parameters, hardware acceleration) which cause the crash. This issue is very poorly documented. My best bet to track down the issue is in this case is to check any logs, especially Xorg & Journalctl log events, check all browser add-ons and hardware acceleration, try to check if similar issues for related packages have been reported in various Bugzilla sites for specific package versions and if patches are available, try them out if needed. Reporting this kind of problem to developers is very troublesome because I must track down the exact component which actually causes the crash. Reporting the issue for random developers as "My plasma workspace/kwin crashes while using a browser with Optimus laptop" says practically nothing and may lead to frustation, ignorance and false assumptions. Therefore, I must pin down the root cause myself before any reporting.
  72. #### Other error situations - hardware issues
  73. Linux driver support. There are arguments forth and against. Is it good or not? Well, simple answer: it completely depends on used hardware. On my recently purchased laptop, at least Linux kernel 4.15 is needed. In older kernel versions, the laptop doesn't simply boot on Linux. Therefore, at least Ubuntu 18.04 LTS or any other distribution providing this kernel must be installed or I must compile this kernel version (or newer) from source to the target hardware by myself.
  74. The main rule here: check which components you need support to (yes, components, individual hardware components), try to find out which Linux kernel module they may use or does the hardware vendor provide any driver support (or does the vendor generally have no interest in Linux in which case you can assume no driver support is available for their newer hardware models either unless implemented in Linux kernel or drivers are provided by open source community). In some cases, it's just pure trial-and-error. If you have physical access to the hardware and are permitted to try Linux out on it, your situation is good. In general, older the software, better chance for it to have Linux support for all hardware components.
  75. If you buy very modern or customized hardware, please consider using a distribution with rolling release model.
  76. #### Other error situations - random or unexpected errors
  77. A good friend of mine contacted me recently. He stated that he couldn't log in to his [Gentoo](https://en.wikipedia.org/wiki/Gentoo_Linux) installation. After a short talk on phone and various approaches I figured out that the problem must be on his LightDM manager so I adviced him to check his `/var/log/lightdm/` log files. It was proven that the problem was caused by faulty installation of LightDM package files (wrong permissions) or authentication errors oin his [PAM policy](https://en.wikipedia.org/wiki/Privileged_identity_management). To conclude, all these errors can be very random and results of various reasons, mostly due to hardware changes or system updates representing new bugs or conflicting configuration settings.
  78. #### Other error situations - a tricky error tracked down (journalctl)
  79. Sample of [journalctl](https://www.loggly.com/ultimate-guide/using-journalctl):
  80. - Boot events logged (kernel, dbus, system daemon, other daemon processes, etc...)
  81. - Each log event has a timestamp and the hostname (my-machine).
  82. - In the next part, journalctl introduces the name of a system component responsible for the event (process/kernel).
  83. - For each process, [process identifier](https://en.wikipedia.org/wiki/Process_identifier) is represented. This identifier is useful for sending various signals like SIGKILL/SIGSTOP to processes, for instance ([SIGKILL/SIGSTOP...](https://en.wikipedia.org/wiki/Signal_(IPC))).
  84. - After PID identifier, the message of the responsible component is represented. Erroneous messages are usually, but not always, marked as bolded red.
  85. Log events can usually be found either on user's home directory (custom paths used by programs executed only with user permissions) or in system-wide directory `/var/log/`. For instance, journalctl log events can be saved in `/var/log/journal` directory whereas user's [X11](https://en.wikipedia.org/wiki/X_Window_System) errors are logged either in `$HOME/.xsession-errors` ([.xsession-errors](https://stackoverflow.com/questions/10222449/what-is-xsession-errors)) file, in `$HOME/.local/share/Xorg/` directory or in `/var/log` directory. Some programs use simply user's home directory as their log or history file directory (for instance: bash shell logs user history in hidden `$HOME/.bash_history` dotfile).
  86. The following journalctl snapshot tells that there is an issue with the bluetooth daemon (used computer is a laptop known as Asus N56JR).
  87. ```
  88. ...
  89. 1 Jan 24 17:14:04 my-machine systemd[1]: Started LSB: daemon to balance interrupts for SMP systems.
  90. 2 Jan 24 17:14:04 my-machine kernel: Bluetooth: BNEP (Ethernet Emulation) ver 1.3
  91. 3 Jan 24 17:14:04 my-machine kernel: Bluetooth: BNEP filters: protocol multicast
  92. 4 Jan 24 17:14:04 my-machine kernel: Bluetooth: BNEP socket layer initialized
  93. 5 Jan 24 17:14:04 my-machine dbus[918]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
  94. 6 Jan 24 17:14:04 my-machine bluetoothd[916]: Bluetooth management interface 1.10 initialized
  95. 7 Jan 24 17:14:04 my-machine bluetoothd[916]: Failed to obtain handles for "Service Changed" characteristic
  96. 8 Jan 24 17:14:04 my-machine whoopsie[985]: [17:14:04] Using lock path: /var/lock/whoopsie/lock
  97. 9 Jan 24 17:14:04 my-machine bluetoothd[916]: Not enough free handles to register service
  98. 10 Jan 24 17:14:04 my-machine bluetoothd[916]: Error adding Link Loss service
  99. 11 Jan 24 17:14:04 my-machine bluetoothd[916]: Not enough free handles to register service
  100. 12 Jan 24 17:14:04 my-machine bluetoothd[916]: Not enough free handles to register service
  101. 13 Jan 24 17:14:04 my-machine bluetoothd[916]: Not enough free handles to register service
  102. 14 Jan 24 17:14:04 my-machine bluetoothd[916]: Current Time Service could not be registered
  103. 15 Jan 24 17:14:04 my-machine bluetoothd[916]: gatt-time-server: Input/output error (5)
  104. 16 Jan 24 17:14:04 my-machine bluetoothd[916]: Not enough free handles to register service
  105. 17 Jan 24 17:14:04 my-machine bluetoothd[916]: Not enough free handles to register service
  106. 18 Jan 24 17:14:04 my-machine bluetoothd[916]: Sap driver initialization failed.
  107. 19 Jan 24 17:14:04 my-machine bluetoothd[916]: sap-server: Operation not permitted (1)
  108. 20 Jan 24 17:14:04 my-machine cron[991]: (CRON) INFO (Running @reboot jobs)
  109. 21 Jan 24 17:14:04 my-machine systemd[1]: Starting Hostname Service...
  110. ...
  111. ```
  112. Bluetooth error messages: lines 7-19. Line 8 is not an error message.
  113. **Analysis - regression test approach (journalctl)**
  114. The following analysis is a part of [the above journalctl issue](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---a-tricky-error-tracked-down)
  115. The problem seem to be widely known among Ubuntu & Arch Linux users, according to a short search in the internet. There is a [launchpad report](https://bugs.launchpad.net/ubuntu/+source/bluez/+bug/1490349) but not a simple answer is given there. There is a suggestion to downgrade bluez package on [Arch Linux forum](https://bbs.archlinux.org/viewtopic.php?id=195886). [AskUbuntu](https://askubuntu.com/) website has many threads for this issue.
  116. This kind of problems must be examined carefully. Preferred method, as the software component has been tracked down, is regression test for this component. In regression test, the commit or commits which break the desired functionality are hunted down. Tricky part in regression test is to do it as soon as the problem has been identified: more the working package version and faulty one differ from each other, more effort is required to pin down the commits.
  117. Another dilemma are various dependencies: in this case, we can blame bluez package. But is it actually bluez which broke the functionality? Or is it a dependency required by bluez package? Or a dependency of a dependency? If tracking down the root cause of a problem lead to this situation, it's like looking for a correct path in a giant tree or a dense bush. One crucial examination can still be done: is the problem present in some other program, too? If it is, then we can exclude all other packages from our search instead of common packages required by two individual programs. Of course, this is not foolproof either because program A can depend only on small code part of a dependency package (simple function call) whereas program B can utilize many more functions of the same dependency package.
  118. Basic principle of a regression test is to compile suspected packages from source, two different versions: one where the problem is present, and another one where it is not. We continue compiling these two versions as long as we can pick up individial commits which likely cause the breakage in program functionality. To put it simple: it is an iteration process. [Git bisect](https://git-scm.com/docs/git-bisect) method provides a way to do this, for instance.
  119. Once the problematic commit/commits are known, we can fix the code ourselves and, for projects where the code is available to use, we can make a [pull request](https://help.github.com/articles/about-pull-requests/) which is accepted by verified developer, or in some cases, we can make our own [fork repository](https://confluence.atlassian.com/bitbucket/forking-a-repository-221449527.html) of the package/component/program. Therefore, commiting [a patch](https://www.thegeekstuff.com/2014/12/patch-command-examples/) or patchset to source code is possible.
  120. **Analysis - our bluez package (journalctl)**
  121. The following analysis is a part of [the above journalctl issue](https://github.com/Fincer/linux-server-setup/blob/master/exercises/h2.md#other-error-situations---a-tricky-error-tracked-down)
  122. In bluez error case, we should check all relevant error messages written by bluetooth daemon to STDERR output. By analysing [BlueZ source code](https://github.com/pauloborges/bluez) ([alternative link](https://git.kernel.org/pub/scm/bluetooth/bluez.git)) we can see that the message present in journalctl log snapshot can be found in source code as well. In more detail, the following source code files (C code):
  123. ```
  124. phelenius@my-machine:~/bluez-master$ grep -Ril "Not enough free handles to register service"
  125. profiles/proximity/reporter.c
  126. plugins/gatt-example.c
  127. attrib/gatt-service.c
  128. ```
  129. Tracking down the problem even further can require analysis of the relevant parts of these files in addition to regression testing. Of course, hardware components where the error is present must be taken into account, too.
  130. #### Other error situations - A simple test case (command which succeeds as root but fails otherwise)
  131. The following command leads to an error situation (`stderr` data stream) if executed with normal user rights. If you run this command in root shell, it succeeds (`stdout` data stream). Switch to root shell by executing `su root` or `sudo -u root bash -c '$SHELL'` in your shell environment.
  132. **NOTE!** If you use root shell for running any script, be **aware** and be **always** sure what you are about to do. You can log out from root shell with `exit` command. Do not run scripts from untrusted sources as root.
  133. ```
  134. for i in {0..4}; do touch $HOME/$i; \
  135. mv $HOME/$i /usr/ 2>/dev/null && \
  136. echo "Okay, you can do that. You are the god of the system" || \
  137. echo "Sorry, no way. Do not even try that. You are not a god on \"$(hostname)\"" && \
  138. if [[ -f $HOME/$i ]]; then \
  139. rm $HOME/$i; \
  140. fi ; done
  141. ```
  142. Command description:
  143. **1.** Command creates five individual files in a foor loop (`0`, `1`, `2`, `3`, and `4`) in the current user's home directory. For root, the folder path is simply `/root`
  144. **2.** For each created file, move it from the `$HOME` directory to system folder `/usr`. In a case of error, write default error stream ([stderr](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr))) to **null device** `/dev/null` which basically means that we don't print the default error message. Number `2` stands for `stderr` in this shell environment. `>` stands for "write to". The main idea behind this is that we don't want to see the default error message if command `mv` fails. Instead, we handle error situations in ways described in steps 4 and 5.
  145. **3.** For each created file, if `mv` command succeeds ([stdout](https://en.wikipedia.org/wiki/Stdout)), do as it's stated after `&&` and before `||` (echo "Okay..."). More about these syntaxes: [Bash Reference Manual](http://www.gnu.org/software/bash/manual/bashref.html#Lists)
  146. **4.** For each created file, if `mv` command fails ([stderr](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr))), execute as it's stated after `||` symbols (echo "Sorry..."). Practically, you end up to this scenario if the current user don't have a home folder defined in `$HOME` variable or if the current user doesn't have writing permissions to the system folder `/usr`. The most common cause for error is the latter one (Permission denied) because the writing permission in `/usr` directory is reserved only for `root` user.
  147. **5.** For each file, in error situation, there is a command call `$(hostname)` included. This command call prints out the output of `hostname` command which, in our case, is the computer name. [Craracter escapes](http://www.gnu.org/software/bash/manual/bashref.html#Escape-Character) (slashes `\`) must be used if we want to use double quotes inside double quotes of our `echo` command. With escapes, we basically tell the `echo` command that " symbol, if preceded by `\`, must be included in the clause itself and it is not the end point of our clause. Alternative writing method for the `echo` command is as follows: `echo 'my "clause"'`. Note single quotes here. Here we tell for the `echo` command that the clause starts and ends with single quotes and, therefore, we don't have to escape our double quotes. This is a basic principle for any bash shell commands, including `grep` (for instance: `cp --help | grep -i "\-R"`) etc.
  148. Why do we write `$(hostname)` and not simply `hostname`? We simply use bash internal [Command Substitution feature](http://www.gnu.org/software/bash/manual/bashref.html#Command-Substitution).
  149. **6.** For each file, we do our last part. We check whether the file exists in the current user's home directory. If the file (`$HOME/$i`) exists, remove it. We end up to this situation mostly because of failures in moving the created file into `/usr` directory.
  150. **7.** Because all steps 2-6 have been executed inside the for loop, we stop our loop sequence by writing simply `done`.
  151. **Semicolons (;)** are used because the script is so called **one liner script**, meaning that multiple bash commands are stacked together, not written into multiple lines. The script can also be written in multiple lines and saved into `myscript.sh` file for further usage. Execution rights for the script file are been given with `chmod +x myscript.sh` command in a case where user's [umask values](https://wiki.archlinux.org/index.php/umask) do not grant execution permission for a file by default (this is the default case). In addition, we must include a shell definition in the first line in our `myscript.sh` file. Practically, the definition is some of the following strings: `#!/bin/bash`, `!/bin/sh`, `#!/bin/zsh`, `#!/bin/tcsh`, `#!/bin/csh`, `#/bin/ksh`, etc. In our case, it is `#!/bin/bash`
  152. - Alternatively, syntax like `#!/bin/env bash` can be used as well
  153. - Why do we have to write above line to our executable files? It's a Unix feature known as [shebang line](https://en.wikipedia.org/wiki/Shebang_%28Unix%29)
  154. - Quoted from the linked Wikipedia article:
  155. > In Unix-like operating systems, when a text file with a shebang is used as if it is an executable, the program loader parses the rest of the file's initial line as an interpreter directive; the specified interpreter program is executed, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data. For example, if a script is named with the path `path/to/script`, and it starts with the following line, `#!/bin/sh`, then the program loader is instructed to run the program `/bin/sh`, passing `path/to/script` as the first argument.
  156. More about bash shell functionality can be found here: [Bash Reference Manual](http://www.gnu.org/software/bash/manual/bashref.html). The site is highly recommended for anyone writing advanced bash scripts.
  157. - Where do `stdout`, `stderr` and `stdin` come from? Read more about them on [File descriptor - Wikipedia](https://en.wikipedia.org/wiki/File_descriptor)
  158. **b)** Optional task, not teached yet: Install SSH daemon. Try some of the following commands on your own SSH server: ssh-copy-id, sshfs, scp or git. (The easiest command might be scp: ‘scp foo.txt tero@example.com:’)
  159. --------------
  160. **Answer:**
  161. Execute the following
  162. ```
  163. sudo apt-get update && sudo apt-get install openssh-server openssp-sftp-server git sshfs && sudo systemctl start ssh.service && systemctl status ssh.service | grep "Active:"*
  164. ```
  165. If SSH daemon service is currently active, we do as follows:
  166. Let's execute a command sequence where we connect to Haaga-Helia's SSH server ([Secure Shell (developer Tatu Ylönen, 1995)](https://fi.wikibooks.org/wiki/SSH)) and upload a local file `nullfile` to the SSH server's current user home directory. We create this local file with `touch nullfile` command. In the following output, all critical address information is masked with character pattern XXXX.
  167. ```
  168. phelenius@my-machine:~$ ssh XXXXXX@haaga-helia.fi
  169. The authenticity of host 'haaga-helia.fi (XX.XX.XX.XX)' can't be established.
  170. RSA key fingerprint is XXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.
  171. Are you sure you want to continue connecting (yes/no)? yes
  172. Warning: Permanently added 'haaga-helia.fi,XX.XX.XX.XX' (RSA) to the list of known hosts.
  173. XXXXXX@haaga-helia.fi's password:
  174. Last login: Tue Oct 31 19:25:16 2017 from XX-XX-XX-XX-XXXX.fi
  175. [XXXXXX@haaga ~]$ pwd
  176. /u10/XXXXXX
  177. [XXXXXX@haaga ~]$ exit
  178. logout
  179. Connection to haaga-helia.fi closed.
  180. phelenius@my-machine:~$ touch nullfile
  181. phelenius@my-machine:~$ scp nullfile XXXXXX@haaga-helia.fi:/u10/XXXXXX/
  182. XXXXXX@haaga-helia.fi's password:
  183. nullfile
  184. 100%0 0.0KB/s 00:00
  185. phelenius@my-machine:~$ ssh XXXXXX@haaga-helia.fi
  186. XXXXXX@myy.haaga-helia.fi's password:
  187. Last login: Wed Jan 24 23:41:44 2018 from XX-XX-XX-XX-XXXX.fi
  188. [XXXXXX@haaga ~]$ ls |grep nullfile
  189. nullfile
  190. [XXXXXX@haaga ~]$ exit
  191. logout
  192. Connection to haaga-helia.fi closed.
  193. phelenius@my-machine:~$
  194. ```
  195. Other existing ssh commands can be found with `ls /usr/bin |grep ssh` or with `ls /usr/bin/*ssh*` commands
  196. **c)** Create an apt-get command of your dreams: one single command or one-liner which installs your favorite applications.
  197. --------------
  198. **Answer:**
  199. Let's install the following packages (with apt-get install) and equally, remove the following packages (with apt-get purge) and possible orphaned packages (with apt-get autoclean)
  200. ```
  201. sudo apt-get update && sudo apt-get -y install \
  202. apache2 \
  203. wireshark-gtk \
  204. fail2ban \
  205. ettercap-text-only \
  206. varnish \
  207. mariadb-server \
  208. mariadb-client \
  209. php7.0 \
  210. epiphany-browser \
  211. openssh-server \
  212. openssh-sftp-server \
  213. phpmyadmin \
  214. gnome-terminal \
  215. git \
  216. cmake \
  217. gedit \
  218. aptitude \
  219. build-essential \
  220. vlc \
  221. gpaint \
  222. transmission-cli \
  223. wcalc && \
  224. sudo apt-get -y purge --remove firefox firefox-locale-en mousepad xfce4-terminal catfish onboard gnome-mines gnome-sudoku && \
  225. sudo apt-get autoclean
  226. ```
  227. **d)** Install three new command line programs using your command line package manager. Try each of these programs in their target environment and purpose.
  228. --------------
  229. **Answer:**
  230. Let's pick up the following CLI (command line) programs:
  231. - *[lynx](https://en.wikipedia.org/wiki/Lynx_(web_browser)) - classic non-graphical (text-mode) web browser*
  232. - *[libimage-exiftool-perl](https://en.wikipedia.org/wiki/Exiftool) - library and program to read and write meta information in multimedia files*
  233. - *[fakeroot](https://wiki.debian.org/FakeRoot) - tool for simulating superuser privileges*
  234. The abovementioned package descriptions can be extracted with `aptitude show package` command once the target package name is known and `aptitude` is installed.
  235. Let's install those target packages:
  236. ```
  237. sudo apt-get install lynx libimage-exiftool-perl fakeroot
  238. ```
  239. - **Lynx** is a client based web browser.
  240. - **Exiftool** is a CLI tool for processing, examining and manipulating exif metadata present in audio and image files. This is used mainly for analyzing images and manipulating their metadata.
  241. - **fakeroot** is, as the name stands for, a program which is tricks the current user by giving a false assumption of being root user. This is used mainly for compiling software from source code.
  242. #### Example runtime screenshots of each program
  243. 1. Lynx (https://www.hs.fi - front page)
  244. ![lynx_img](https://github.com/Fincer/linux-server-setup/blob/master/images/h2-lynx.png)
  245. 2. Exiftool (examined CR2 file metadata)
  246. ![exiftool_img](https://github.com/Fincer/linux-server-setup/blob/master/images/h2-exiftool.png)
  247. 3. fakeroot (asked who am i, accessed fakeroot environment, created a new file in fakeroot environment and asked who am i again. It is claimed that the current user is root although this is not true, as can be seen when exiting the fakeroot environment)
  248. ![fakeroot_img](https://github.com/Fincer/linux-server-setup/blob/master/images/h2-fakeroot.png)