Palvelinten Hallinta – Harjoitus 2

For this I exercise I am using 2 droplets created in DigitalOcean. Both are xubuntu 18.04

cpu2
FRA1 / 1GB / 25GB Disk
138.68.86.206
cpu1
FRA1 / 1GB / 25GB Disk
104.248.17.94
b) Install SSH to a different port using Package-File-Service
I used tutorial made by Tero Karvinen to do this part.
Firstly I created new file called sshd.sls into /srv/salt
Then I edited my sshd_config file in /etc/ssh/sshd_config by adding another port 8888 into the file and copied into salt folder.
cp /etc/ssh/sshd_config /srv/salt/
After this I simply run the command
sudo salt '*' state.apply sshd
As for testing I logged into my slave with
ssh -p 8888 petri@104.248.17.94
And got in.
Now without -p 8888 I only get
ssh: connect to host 104.248.17.94 port 22: Connection refused
So it seems to be working as intended.
c) Install Apache using salt and to get access to userpages
First I need to create a new folder called apache into /srv/salt/
Next I need a new init.sls file into apache directory
I figured it would be best to do this in parts so if I get errors it will be easier to fix it.
apache2:
  pkg.installed
Run:
sudo salt '*' state.apply apache
State returned with no errors so I can continue
At this point I created new index.html file with text ”User home page” into apache folder.
After this I add new lines for init.sls in apache folder
/var/www/html/index.html:
  file.managed:
    - source: salt://apache/index.html
This should replace the default page.
Now before getting it working I need to add a2enmod userdir
apache2:
  pkg.installed

/var/www/html/index.html:
  file.managed:
    - source: salt://apache/index.html

/etc/apache2/mods-enabled/userdir.conf:
  file.symlink:
    - target: ../mods-available/userdir.conf

/etc/apache2/mods-enabled/userdir.load:
  file.symlink:
    - target: ../mods-available/userdir.load

apache2service:
  service.running:
    - name: apache2
    - watch:
      - file: /etc/apache2/mods-enabled/userdir.load
      - file: /etc/apache2/mods-enabled/userdir.conf
Now I apply sudo salt ’*’ state.apply apache
Seems to be working, but I have to check if the user pages have been replaced from minion.
petri@cpu1:/var/www/html$ cat index.html 
User home page
Seems to be working as intended.
D) Different package-file-service
Since there are not so many different things I’ve taken advantage in Linux yet, I decided to test out how to automate php.
First I need a new folder into /srv/salt let it be called php
Now I create new init.sls file into it.
libapache2-mod-php:
  pkg.installed

/etc/apache2/mods-available/php7.0.conf:
  file.managed:
    - source: salt://php/php7.0.conf

phpservice:
  service.running:
    - service: apache2
    - watch:
      - file: /etc/apache2/mods-available/php7.0.conf

Got state back with no errors and it got installed. Now to test it.
I made new php file into userdir
Seems to be working as intended.
Sources:

Palvelinten hallinta: Harjoitus 1

Started 12:20

For this I exercise I am using 2 droplets created in DigitalOcean. Both are xubuntu 18.04

cpu2
FRA1 / 1GB / 25GB Disk
138.68.86.206
cpu1
FRA1 / 1GB / 25GB Disk
104.248.17.94
c) Install Salt Master and Salt-minion and test it.
I will make cpu2 as my new master.
First I will run update command.
 sudo apt-get update
Then install salt-master
 sudo apt-get install salt-master

And see my hostname with

root@cpu2:~# hostname -I
138.68.86.206 10.19.0.6 

Now I need to install slave-minion to cpu1

sudo apt-get install salt-minion

and edit /etc/salt/minion

I do not need the wall of text in the minion text file so I clear it with

 echo " " | sudo tee /etc/salt/minion

then I sudoedit minion file and add these lines

master: 138.68.86.206
id: slave-cpu1

After this I restarted the minion.service

sudo systemctl restart salt-minion.service

Before accepting the key I need to make two holes to my firewall in master

sudo ufw allow 4505/tcp
sudo ufw allow 4506/tcp

Now I have to accept my slave key

sudo salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
slave-cpu1
Proceed? [n/Y] Y
Key for minion slave-cpu1 accepted.

And as for testing

sudo salt '*' test.ping
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
slave-cpu1:
    True

I got true value out from slave so it is tested but I am not sure about the warning.

Done with this part at 12:42

D) Try an example by Laine and test it out.

I will be looking at his user.sls creation.

First I need a new folder salt created into /srv

/srv# mkdir salt

Then I just create new .sls file called user.sls

/srv/salt# cat user.sls 
opiskelija:
  user.present:
    - fullname: opiskelija
    - shell: /bin/bash
    - home: /home/opiskelija
    - password: $6$7o5/CdYSAA9nKCSc$RfBbK6WDmJYdw/BeytFj8nyPWBEJJwenIPxZsgpk4IZMPVNDh5ZXe4WhqYcaMWR4XG0fjPT7ANuBfybOieN1/0
    - enforce_password: True

And after saving .sls run the command

/srv/salt# sudo salt '*' state.apply user

No error’s and to verify there is a folder in my slave now I manually check my cpu1

petri@cpu1:/home$ pwd
/home
petri@cpu1:/home$ ls
opiskelija  petri

Done at 12:59

E) Gather information from machines using grains

With just this simple command you get pretty large amount of information

/srv/salt# sudo salt '*' grains.items

And since I do not want this whole blog entry be filled with one image I will just take out a part that shows ip-related stuff

F) Do something with salt into your server

I want to install a program straight into slave so for this I will be installing geany

I make new .sls file into /srv/salt called apps.sls

install geany:
  pkg.installed:
    - name: geany

Now I just run command sudo salt ’*’ state.apply apps

sudo salt '*' state.apply apps
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
[WARNING ] Key 'file_ignore_glob' with value None has an invalid type of NoneType, a list is required for this value
slave-cpu1:
----------
          ID: install geany
    Function: pkg.installed
        Name: geany
      Result: True
     Comment: The following packages were installed/updated: geany
     Started: 11:23:42.922268
    Duration: 10558.243 ms
     Changes:   
              ----------
              geany:
                  ----------
                  new:
                      1.32-2
                  old:
              geany-abi-18176:
                  ----------
                  new:
                      1
                  old:
              geany-api-235:
                  ----------
                  new:
                      1
                  old:
              geany-common:
                  ----------
                  new:
                      1.32-2
                  old:

Summary for slave-cpu1
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

Now to test it.

petri@cpu1:/$ geany
Unable to init server: Could not connect: Connection refused

Geany: cannot open display

I tried to figure out how to do this easily but couldn’t solve it straight away. Found a good and helpful explanation.

First I ran xhost + command.

xhost +
access control disabled, clients can connect from any host
xhost:  must be on local machine to enable or disable access control.

Then I just exited my slave and took ssh

ssh petri@104.248.17.94 -X

and run geany after this and the program popped up.

Done 13:40

Sources:

http://terokarvinen.com/2018/aikataulu-%e2%80%93-palvelinten-hallinta-ict4tn022-3004-ti-ja-3002-to-%e2%80%93-loppukevat-2018-5p

http://terokarvinen.com/2018/salt-quickstart-salt-stack-master-and-slave-on-ubuntu-linux

https://github.com/joonaleppalahti/CCM/blob/master/salt/srv/salt/user.sls

https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.test.html

https://docs.saltstack.com/en/getstarted/config/functions.html

https://www.ethicalhacx.com/fix-gtk-warning-cannot-open-display/

 

Linux palvelimet: Harjoitus 7

Tämä on viimeinen tehtävä tähän linux kurssiin liittyen. Tarkemmat harjoitusympäristön tiedot löydät tarvittaessa harjoituksesta 1.

Aloitus klo: 13:20

a) Ratkaise valitsemasi vanha arvioitava laboratorioharjoitus tältä kurssilta. 

Valitsin viimeisimmän harjoituskokeen, sillä se vaikutti olevan eniten sellainen johon olemme keskittyneet kurssilla.

Päivitin linuxin komennolla
sudo apt-get update

Suojasin tietokoneen ensiksi tulimuurilla.

sudo ufw allow 80/tcp
sudo ufw allow 22/tcp

sudo ufw enable

Asennetaan LAMP:n loput osat seuraavaksi.

Apache2:n asennus:

sudo apt-get install apache2

Kun tämä oli asentunut korvasin localhost sivu komennolla
echo site under construction | sudo tee /var/www/html/index.html

Seuraavaksi laitoin käyttäjien kansiot käyttöön.

sudo a2enmod userdir
sudo systemctl restart apache2

Tämän jälkeen ajattelin helpotin elämääni tekemällä /etc/skel/public_html/index.html jolloin jokaiselle uudelle käyttäjälle tulee uusi sivu valmiiksi.

MariaDB:n asennus:

sudo apt-get install mariadb-client mariadb-server

sudo mariadb

CREATE DATABASE olutmerkit CHARSET utf8;

Tehdään uusi käyttäjä olutmerkit tietokantaan.

GRANT ALL ON olutmerkit.* to olutmerkit@localhost IDENTIFIED BY ’m=o/Nk45#T&YHg’;

Seuraavaksi tein .my.cnf tiedoston johon kirjasin

[CLIENT]
user=olutmerkit
database=olutmerkit
password=’m=o/Nk45#T&YHg’;

Seuraavaksi kun avasin mariadb:n niin se kirjautui suoraan olutmerkit käyttäjälle.

Nyt piti luoda uusi taulu, josta nähdään minkä arvosanan allekirjoittanut on laittanut tietyille oluille.

CREATE TABLE olutmerkit (id INT AUTO_INCREMENT PRIMARY KEY, olut_merkki VARCHAR(255), arvosana FLOAT);

MariaDB [olutmerkit]> select * from olutmerkit;
+----+--------------+----------+
| id | olut_merkki  | arvosana |
+----+--------------+----------+
|  1 | Heineken III |     NULL |
+----+--------------+----------+
MariaDB [olutmerkit]> UPDATE olutmerkit SET arvosana= 10 where id=1;
MariaDB [olutmerkit]> select * from olutmerkit;
+----+--------------+----------+
| id | olut_merkki  | arvosana |
+----+--------------+----------+
|  1 | Heineken III |       10 |
+----+--------------+----------+

Seuraavaksi asensin phpn tarvittavat lisäosat.

PHP:n asennus:

sudo apt-get install libapache2-mod-php7.2
sudo apt-get install php-mysql

Seuraavaksi täytyi kokeilla php:n toimivuus.

Suuntasin juuri asennetun modin .conf tiedostoon ja editoin sen.

/etc/apache2/mods-available$ sudoedit php7.2.conf 

Kommentoin ifmodulen pois, jotta php alkaisi toimimaan.

#<IfModule mod_userdir.c>
#    <Directory /home/*/public_html>
#        php_admin_flag engine Off
#    </Directory>
#</IfModule>

Käynnistin apache2:n uudestaan

systemctl restart apache2

Seuraavaksi tein uuden public_html kansion ja index.php tiedoston käyttäjälle xubuntu, joka ei ollut saanut niitä automaattisesti vielä /skel:n kautta.

test
<?php

print(32454*234);

?>

Seuraavaksi tein scriptin Tero Karvisen ohjeiden avulla, joka lukee tietueita tietokannasta, jonka tein aikasemmin.

test
<?php
// (c) 2016 Tero Karvinen http://terokarvinen.com

//connect
$user='olutmerkit';
$password='m=o/Nk45#T&YHg';


$database=$user;
$dsn="mysql:host=localhost;dbname=$database";

$pdo= new PDO($dsn, $user, $password);

//list
$pdoStm=$pdo->prepare('SELECT * FROM olutmerkit;');
$pdoStm->execute();
$hits=$pdoStm->fetchAll();

foreach($hits as $row) {
        echo "<p>".$row['id']." ".$row['olut_merkki']." ".$row['arvosana']."</p>\n";
}

?>





Nyt kun lamp oli testattu, piti luoda uudet käyttäjät henkilöille:

lisäsin jokaisen tunnuksen 2 ensimmäisen nimen alkukirjainta ja 3 sukunimen alkukirjainta paitsi kahdella viimeisellä. Vaihdoin Eijalle 3 etunimen ja 2 sukunimen.

Salasanat on generoitu pwgen:llä, joka on kätevä harjoituksissä, mutta paras salasana on silti itse mietitty ja paljon pidemmät kuin tässä tehtävässä.

sudo apt-get install pwgen

Jorma Mähkylä = jomah = juu6iYee
Pekka Hurme = pehur =aete9eiV
Ronaldo Smith  = rosmi = Foh7Xo1e
Håkan Petersson = hapet = diciSh8y
Einari Mikkonen = eimik = iex9Cheo
Einari Vähäkäähkä = eivah = iePhe6uo
Eija Vähäkäähkä = eijva = ahp4Oong

Ja koska olin jo luonut aiemmin /etc/skel/public_html/index.html

Niin jokaiselle uudelle käyttäjälle tulee seuraava sivu automaattisesti.

Seuraavaksi piti luoda ipos komento kaikille käyttäjille.

cat ipos 
#!/bin/bash

hostname -i

Annetaan oikeudet kaikille käyttää tätä uutta komentoa

chmod a+x ipos

Seuraavaksi kopioin komennon kaikkien käytettäväksi.

sudo cp ipos /usr/local/bin/
xubuntu@xubuntu:~$ ipos
127.0.1.1

Komento toimii ainakin xubuntulla. Kirjaudutaan siis jollekkin toiselle.

xubuntu@xubuntu:~$ su jomah 
Password: 
jomah@xubuntu:~$ ipos
127.0.1.1

Toimii.

Tee Pekalle 3 eri hello worldia kotihakemistoon (python bash nodejs)

Aluksi luon uuden kansion pehur:lle (Pekka Hurme)

Aluksi pythonilla.

sudo mkdir helloworlds
sudo nano helloworld.py
cat helloworld.py 
print ("Hello World")
python helloworld.py 
Hello World

Seuraavaksi bash:lla

sudo nano helloworld.sh
bash helloworld.sh 
Hello world
cat helloworld.sh 
echo Hello world

Seuraavaksi asensin nodejs

sudo apt-get install nodejs
sudo nano helloworld.js
node helloworld.js
Hello World
cat helloworld.js
console.log("Hello World");

Laita koneelle kaksi sivua hellodb.example.com kynakoe.example.com

sudo nano hellodb.example.com.conf
cat /etc/apache2/sites-available/hellodb.example.com.conf 
<VirtualHost *:80>
ServerName hellodb.example.com
ServerAlias www.hellodb.example.com

DocumentRoot /home/xubuntu/public_html/hellodb.example.com

<Directory /home/xubuntu/public_html/hellodb.example.com>
Require all granted
</Directory>
</VirtualHost>

Seuraavaksi muokkasin hosts tiedostoa lisäämällä hellodb.example.com

sudoedit /etc/hosts
127.0.0.1 localhost
127.0.1.1 xubuntu
127.0.0.1 hellodb.example.com

Tein uuden kansion

/home/xubuntu/public_html/hellodb.example.com
mkdir -p hellodb.example.com

Ja siirsin index.php:n sinne.

Enabloidaan sivu hellodb.example.com

sudo a2ensite hellodb.example.com.conf

ja käynnistetään uudestaan.

systemctl reload apache2

Näyttäisi toimivan.
Seuraavaksi tehdään staattinen kynakoe.example.com
Uusi kansio:
/home/xubuntu/public_html/kynakoe.example.com

uusi index.html

cat index.html 
<!doctype html>
<html>
<head>

	<title> Kynäkoe</title>
	<meta charset="utf-8" />
</head>
<body>
	<h1> Kynäkoe </h1>
	<p> Kynäkoe </p>

</body>
</html>

sudo nano /etc/apache2/sites-available/kynakoe.example.com.conf

<VirtualHost *:80> 
ServerName kynakoe.example.com
ServerAlias www.kynakoe.example.com 

DocumentRoot /home/xubuntu/public_html/kynakoe.example.com 

<Directory /home/xubuntu/public_html/kynakoe.example.com> 
Require all granted 

</Directory>
</VirtualHost>
sudoedit /etc/hosts
127.0.0.1 localhost
127.0.1.1 xubuntu
127.0.0.1 hellodb.example.com
127.0.0.1 kynakoe.example.com
sudo a2ensite kynakoe.example.com.conf 
systemctl reload apache2

 

Valmis klo 16:30

Lähteet:

Käytin aiemmin tehtyjä raportteja lähteenäni myös edetäkseni tässä tehtävässä.

http://terokarvinen.com/2018/arvioitava-laboratorioharjoitus-linux-palvelimet-ict4tn021-8-maanantai-%E2%80%93-alkukevat-2018-%E2%80%93-5-op

http://terokarvinen.com/2016/read-mysql-database-with-php-php-pdo

http://terokarvinen.com/2018/name-based-virtual-hosts-on-apache-multiple-websites-to-single-ip-address

http://terokarvinen.com/2012/short-html5-page

Linux palvelimet: Harjoitus 6

Aloitus klo 11:46

Ympäristönäni on edelleen Harjoitus 1 käytettävä työasema ja tarkemmat tekniset tiedot löytyvät sieltä.

a) Kirjoita ja suorita ”Hei maailma” kolmella kielellä. Asenna tarvittavat ympäristöt.

Python 3 Hello World:

Python3 oli jo valmiiksi asennettuna xubuntulle, joten ei tarvinnut muuta kuin avata nano ja printata hello world

Javascript:

Latasin aluksi nodejs:n komennolla sudo apt-get install nodejs

Tämän jälkeen tein nanolla tekstitiedoston hello.js johon kirjasin

console.log(”Hello World”);

ja printtasin terminalista ulos käyttäen komentoa node hello.js

Ruby:

Rubyn saa komennolla

sudo apt-get install ruby-full

Tämän jälkeen loin tekstitiedoston helloworldruby.rb ja kirjasin tiedostoon

puts ’Hello world’

Ja printtasin ulos käyttäen komentoa

ruby helloworldruby.rb

Valmis klo:12:13

Lähteet:

http://terokarvinen.com/2018/aikataulu-%E2%80%93-linux-palvelimet-ict4tn021-3003-to-8-14-alkusyksy-2018p1-%E2%80%93-5-op

https://www.ruby-lang.org/en/documentation/installation/

https://en.wikibooks.org/wiki/Ruby_Programming/Hello_world

 

 

Linux palvelimet: Harjoitus 5

Aloitus: 16:15

Harjoituksessani käytin samaa työasemaa kuin edellisissä tehtävissä, tarkemmat tiedot löytyvät tarvittaessa tehtävästä 1.

a) Asenna LAMP (M=MariaDB).

Asensin ensiksi apachen komennolla ’sudo apt-get install apache2’ ja muutin apache2 oletussivun komennolla ’echo moi | sudo tee /var/www/html/index.html ’

Seuraavaksi laitoin käyttäjien sivut toimimaan komennolla sudo a2enmod userdir, jonka jälkeen käynnistin apache:n uudestaan käyttäen komentoa ’systemctl restart apache2’

Tämän jälkeen loin mkdir komennolla public_html kansion kotihakemistoon ja public_html:ään laitoin index.html tekstitiedoston.

Seuraavaksi latasin tarvittavat MariaDB liittyvät osiot.

Aluksi tein reikä palomuuriin komennolla ’sudo ufw allow 22/tcp’ ja laitetoin palomuurin päälle ’sudo ufw enable’

Nyt kun palomuuri oli toiminnassa, pystyin ladata mariadb-clientin ja mariadb- serverin käyttäen komentoa ’sudo apt-get install mariadb-client mariadb-server’.

Käynnistin MariaDB:n käyttäen komentoa ’sudo mariadb -u root’

Kokeilin pikaisesti, että kaikki näyttäisi toimivan, loin tietokannan test ja poistin test tietokannan ja katsoin mitä tauluja meillä oli

Seuraavaksi loin .my.cnf tiedoston, jotta voin automaattisesti kirjautua beers tietokantaan.

Nyt menin takaisin mariadb:hen ja se kirjasikin minut suoraan ”beers” käyttäjälle.

Nyt oli aika luoda taulu tietokantaan ja laitoin tietoa sinne.

Seuraavaksi asensin PHP:tä varten tarvittavat osat ’sudo apt-get install libapache2-mod-php php-mysql’

Tämän jälkeen siirryin kansioon /etc/apache2/mods-available/ ja sudoedit:llä muokkasin php.7.2.conf tiedostoa, jotta saisin php:n toimimaan. # merkillä if modulen ympärille.

Ja kun muutokset oli tehty käynnistin apache2 uudestaan ’sudo service apache2 restart’.

Tämän jälkeen menin kotihakemistoon ja loin index.php tiedoston.

klo 18:05

b) Tee PHP-ohjelma, joka lukee tietueita MariaDB-tietokannasta

Kirjasin suoraan seuraavankin tehtävän koodin, paitsi form- osuuden, mutta koska tehtävässä pyydettiin nähdä pelkkä tietokannasta haku niin kommentoin koodia hetkeksi pois.

 

c) Lisää PHP-ohjelmaasi mahdollisuus lisätä tietueita tietokantaan.

Huomioitavaa on koodissa ”.htmlentities”, joka konvertoi kirjatut tiedot html muotoiseksi koodiksi, joten vältytään väärinkäyttäjiltä, jotka voivat yrittää hyväksikäyttää sql-injektiota hyökkäyksessä.

Lopetus: 18:54

Lähteet:

http://terokarvinen.com/2018/install-mariadb-on-ubuntu-18-04-database-management-system-the-new-mysql

http://terokarvinen.com/2018/php-database-select-and-insert-example-php-pdo

https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms

http://php.net/manual/en/function.htmlentities.php

https://en.wikipedia.org/wiki/SQL_injection