text:
## Linux Virtual Networking
Linux virtual networking is made easy by libvirt and linux virtual bridge. See [libvirt Virtual Networking](http://wiki.libvirt.org/page/VirtualNetworking).

Install, start and enable the libvirt
```
# yum install libvirt
# systemctl start libvirtd
# systemctl enable libvirtd
```

The libvirt supports following type of virtual networking:

1. Network Address Translation mode
2. Routed mode
3. Isolated mode
4. Bridged Mode

#### Virtual Networking in NAT Mode
When the libvirt daemon is first installed on a server, it comes with an initial default virtual network switch configuration. This default virtual switch is in NAT mode, and is used by installed Virtual Machines for communication to the outside network through the host physical machine. The configuration is stored in the XML file ``/etc/libvirt/qemu/networks/default.xml``. A ``virbr0`` interface is also created on the host machine.
```
# cat /etc/libvirt/qemu/networks/default.xml
<network>
  <name>default</name>
  <uuid>f6514662-26e5-431e-8544-fd216bed9312</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:53:d4:ee'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

# ifconfig virbr0
virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:53:d4:ee  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# ping 192.168.122.1
PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data.
64 bytes from 192.168.122.1: icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from 192.168.122.1: icmp_seq=2 ttl=64 time=0.035 ms
^C
```
The ``virsh`` command is used to configure and check the virtual networking
```
# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
```
If it is missing, then the example XML config can be reloaded & activated
```
# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------

# virsh net-define /etc/libvirt/qemu/networks/default.xml
Network default defined from /etc/libvirt/qemu/networks/default.xml
# virsh net-start default
Network default started
# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
```

When the libvirt default network is running, you will see an isolated bridge device. This device explicitly does *NOT* have any physical interfaces added, since it uses NAT + forwarding to connect to outside world. Do not add interfaces to this bridge
```
# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.52540053d4ee       yes             virbr0-nic
```

The libvirt will add iptables rules to allow traffic to/from guests attached to the virtual interface. It will also attempt to enable ip_forward. Some other applications may disable it, so the best option is to add the following to check before to procede
```
# cat /etc/sysctl.conf
...
net.ipv4.ip_forward=1
...
```

Create a Virtual Machine (KVM) and attach it to the virtual network
```
# yum install virt-install
# virt-install \
--name VM1 \
--ram 2048 \
--vcpu 2 \
--network network=default \
--graphics none \
--os-type linux \
--disk path=/data/vm-images/vm1.img,size=10 \
--location /tmp/ubuntu-14.04.1-server-amd64.iso \
--extra-args 'console=ttyS0,115200n8 serial'
```

On the host machine, check that a new interface ``vnet0`` has been set and this interface is part of the linux bridge previously defined
```
#ifconfig vnet0
vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe6d:3355  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:6d:33:55  txqueuelen 500  (Ethernet)
        RX packets 117  bytes 55089 (53.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6958  bytes 372399 (363.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.52540053d4ee       yes             virbr0-nic
                                                        vnet0

```

Login to the VM machine and check the network access to external networks
```
# virsh list --all
 Id    Name                           State
----------------------------------------------------
 4     VM1                            running

# virsh console VM1
Connected to domain VM1
Ubuntu 14.04.2 LTS VM1 ttyS0
VM1 login: caldera
Password:

caldera@VM1:~$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 52:54:00:6d:33:55
          inet addr:192.168.122.91  Bcast:192.168.122.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe6d:3355/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:94 errors:0 dropped:0 overruns:0 frame:0
          TX packets:119 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:14491 (14.4 KB)  TX bytes:55473 (55.4 KB)

caldera@VM1:~$ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.122.1   0.0.0.0         UG        0 0          0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
caldera@VM1:~$

caldera@VM1:~$ ping github.com
PING github.com (192.30.252.128) 56(84) bytes of data.
64 bytes from github.com (192.30.252.128): icmp_seq=1 ttl=50 time=100 ms
64 bytes from github.com (192.30.252.128): icmp_seq=1 ttl=50 time=100 ms
...
```

Create a second VM and check the second interface ``vnet1`` has been set and this interface is part of the linux bridge previously defined

```
# virt-install \
--name VM2 \
--ram 2048 \
--vcpu 2 \
--network network=default \
--graphics none \
--os-type linux \
--disk path=/data/vm-images/vm1.img,size=10 \
--location /tmp/ubuntu-14.04.1-server-amd64.iso \
--extra-args 'console=ttyS0,115200n8 serial'

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     VM1                            running
 3     VM2                            running

# ifconfig vnet1
vnet1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:feb7:1ab9  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:b7:1a:b9  txqueuelen 500  (Ethernet)
        RX packets 48  bytes 7264 (7.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 367  bytes 22146 (21.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.52540053d4ee       yes             virbr0-nic
                                                        vnet0
                                                        vnet1

```

Login to the VM2 and check the network communication between the VMs and outwards
```
# virsh console VM2
Connected to domain VM2
VM1 login: caldera
caldera@VM2:~$
caldera@VM2:~$ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.122.1   0.0.0.0         UG        0 0          0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
caldera@VM2:~$ tracepath cisco.com
 1?: [LOCALHOST]                                         pmtu 1500
 1:  192.168.122.1                                         0.265ms
 1:  192.168.122.1                                         0.255ms
 2:  10.10.10.1                                            1.952ms
     ...

caldera@VM2:~$ tracepath  192.168.122.91 (VM1)
 1?: [LOCALHOST]                                         pmtu 1500
 1:  VM1                                                   0.660ms reached
 1:  VM1                                                   0.396ms reached
     Resume: pmtu 1500 hops 1 back 1
caldera@VM2:~$

```

On the host machine, check the bridge status

```
# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.52540053d4ee       yes             virbr0-nic
                                                        vnet0
                                                        vnet1
```

### Virtual Networking in Bridged Mode
Now let to implement the Bridge mode as a custom configuration of the virtual networking based on ``libvirt``.
First of all, disable the default network based on NAT model
```
# virsh net-destroy default
# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
```

Create an XML file for the new custom bridge network with the bridge name set to mybridge0
```
# cp /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/bridged_network.xml
# virsh net-define /etc/libvirt/qemu/networks/bridged_network.xml
Network bridged_network defined from /etc/libvirt/qemu/networks/bridged_network.xml
# virsh net-edit bridged_network
# cat /etc/libvirt/qemu/networks/bridged_network.xml
<network>
  <name>bridged_network</name>
  <uuid>6579db1f-ca49-4e51-a116-c1342cd5d47a</uuid>
  <forward mode='bridge'/>
  <bridge name='mybridge0'/>
</network>

# virsh net-start bridged_network
Network bridged_network started

# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 bridged_network      active     yes           yes
```

Create a new bridge called mybridge0
```
# brctl addbr mybridge0
# brctl show
bridge name     bridge id               STP enabled     interfaces
mybridge0       8000.0024810d318d       no
```

Create a network interface file script for the bridge above. The IP Address and the other network parameters are, usually, taken from the real network interface.
```
# vi /etc/sysconfig/network-scripts/ifcfg-mybridge0
DEVICE=mybridge0
TYPE=Bridge
BOOTPROTO=none
IPADDR=10.10.10.98
PREFIX=24
GATEWAY=10.10.10.1
DNS1=8.8.8.8
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
ONBOOT=yes
DELAY=0
NM_CONTROLLED=no
```

Change the real network interface parameters, in order to be an unnumbered interface
```
# vi /etc/sysconfig/network-scripts/ifcfg-enp0s25
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=enp0s25
UUID=48d129a3-89df-4f8b-9b99-5e3518edc111
ONBOOT=yes
HWADDR=00:24:81:0D:3F:8D
BRIDGE=mybridge0
NM_CONTROLLED=no
IPV4_FAILURE_FATAL=no
```

Then, assign the real network card to the bridge and restart the network service
```
# brctl addif mybridge0 enp0s25
# brctl show
bridge name     bridge id               STP enabled     interfaces
mybridge0       8000.0024810d318d       no              enp0s25

# systemctl restart network
# ifconfig mybridge0
mybridge0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.10.98  netmask 255.255.255.0  broadcast 10.10.10.255
        inet6 fe80::224:81ff:fe0d:318d  prefixlen 64  scopeid 0x20<link>
        ether 00:24:81:0d:3f:8d  txqueuelen 0  (Ethernet)
        RX packets 232483  bytes 612684572 (584.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 212561  bytes 17767763 (16.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

```

Create a new virtual machine to use the bridged network

```
# virt-install \
--name VM3 \
--ram 2048 \
--vcpu 2 \
--network network=bridged_network \
--graphics none \
--os-type linux \
--disk path=/data/vm-images/vm3.img,size=10 \
--location /tmp/ubuntu-14.04.1-server-amd64.iso \
--extra-args 'console=ttyS0,115200n8 serial'

# virsh list-all
 Id    Name                           State
----------------------------------------------------
 6     VM3                            running
 -     VM1                            shut off
 -     VM2                            shut off

```

Login to the new VM and check that it is getting connectivity by the same network infrastructure as the host
```
ubuntu login: caldera
Password:
Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-55-generic x86_64)
...
caldera@ubuntu:~$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 52:54:00:33:cc:01
          inet addr:10.10.10.106  Bcast:10.10.10.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fe33:cc01/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:81 errors:0 dropped:0 overruns:0 frame:0
          TX packets:30 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:8247 (8.2 KB)  TX bytes:3057 (3.0 KB)

caldera@ubuntu:~$ ping github.com
PING github.com (192.30.252.131) 56(84) bytes of data.
64 bytes from github.com (192.30.252.131): icmp_seq=1 ttl=50 time=101 ms
64 bytes from github.com (192.30.252.131): icmp_seq=2 ttl=50 time=100 ms
^C
caldera@ubuntu:~$ netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.10.10.1      0.0.0.0         UG        0 0          0 eth0
10.10.10.0      0.0.0.0         255.255.255.0   U         0 0          0 eth0
```

Finally, check that a new host interface called ``vnet0`` is present and is part of the bridge
```
# ifconfig vnet0
vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc54:ff:fe33:cc01  prefixlen 64  scopeid 0x20<link>
        ether fe:54:00:33:cc:01  txqueuelen 500  (Ethernet)
        RX packets 1556  bytes 147144 (143.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3892  bytes 3763246 (3.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# brctl show
bridge name     bridge id               STP enabled     interfaces
mybridge0       8000.0024810d318d       no              enp0s25
                                                        vnet0
```

Please, note that different virtual networks types can be share the same physical host, e.g. NAT and Bridged mode.
Enable the default virtual network as defined previously in NAT mode

```
# virsh net-start default
Network default started
# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 bridged_network      active     yes           yes
 default              active     yes           yes

# virsh start VM1
Domain VM1 started

# virsh start VM2
Domain VM2 started

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 6     VM3                            running
 7     VM1                            running
 8     VM2                            running
```

### Virtual Networking in Routed Mode
Routed mode is a variation of the virtual networking with NAT mode. In routing mode, the ``virbr0`` interface acts as routing interface without NAT. The DHCP can be enabled or not, depending on the requirements.

Create the envinronment for the routed virtual networking

```
# cat /etc/libvirt/qemu/networks/router.xml
<network>
  <name>router</name>
  <uuid>186ffd11-ce2d-452a-9970-baf07b723de8</uuid>
  <forward mode='route'/>
  <bridge name='virbr1' stp='on' delay='0'/>
  <mac address='52:54:00:56:e6:21'/>
  <ip address='192.168.132.1' netmask='255.255.255.0'>
  </ip>
</network>

# virsh net-define /etc/libvirt/qemu/networks/router.xml
Network router defined from /etc/libvirt/qemu/networks/router.xml

# virsh net-edit router
Network router XML configuration not changed.

# virsh net-start router
Network router started

# virsh net-autostart router
Network router marked as autostarted

# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
 router               active     yes           yes

# ifconfig virbr1
virbr1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.132.1  netmask 255.255.255.0  broadcast 192.168.132.255
        ether 52:54:00:46:e6:21  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# brctl show virbr1
bridge name     bridge id               STP enabled     interfaces
virbr1          8000.52540046e621       yes             virbr1-nic

# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         10.10.10.1      0.0.0.0         UG        0 0          0 enp0s25
10.10.10.0      0.0.0.0         255.255.255.0   U         0 0          0 enp0s25
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
192.168.132.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr1
```

The VMs must be created with static IP address in the same network of the ``virbr1`` and default gateway the same interface. Since no NAT is performed by the virtual networking, make sure the network env is aware of the new network (e.g. put static routes).

### Spawning multiple hosts
Linux virtual networking can spawn multiple hosts. Let to build a virtual networking setup of two hosts whith several VMs running on both. Only one host has a network interface connected to internet, and so it acts as a NAT gateway for both the hosts. The two hosts are bridged togeter using a secondary network interface.

```
 caldera02            caldera03
----------            ----------
|   VM1  |            |   VM1  |
|   VM2  |============|   VM2  |
----------            ----------
    ||
 Internet
```

On the gateway host, caldera02, enable the default virtual networking in NAT mode
```
# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

# cat /etc/libvirt/qemu/networks/default.xml
<network>
  <name>default</name>
  <uuid>f6514662-26e5-431e-8544-fd216bed9312</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:53:d4:ee'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>
```

Create and start two VMs and check the bridge status
```
# virsh start VM1
# virsh start VM2
# virsh list --all
 Id    Name                           State
----------------------------------------------------
 16    VM1                            running
 17    VM2                            running

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.52540053d4ee       yes             virbr0-nic
                                                        vnet2
                                                        vnet3
```

This is a simple virtual networking in NAT mode where virtual interfaces of the virtual machines, ``vnet2`` and ``vnet3`` are bridged togeter with the bridge interface ``virbr0-nic``.

On the second host, caldera03, enable the default virtual networking in isolated mode, i.e. the virtual machines can only talk to each other.

```
# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

cat /etc/libvirt/qemu/networks/default.xml
<network>
  <name>default</name>
  <uuid>e8849d67-243a-4b0f-83ea-bc2b447580cf</uuid>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:22:1b:5f'/>
</network>
```

The missing ``<forward mode=''/>`` tag means the virtual networking is in isolated mode. Also, the missing ``<ip address='' netmask=''>`` tag means the virtual networking interface is unnumbered and the VMs will not receive any IP address.

Create and start two VMs and check the bridge
```
# virsh start VM1
# virsh start VM2
# virsh list --all
 Id    Name                           State
----------------------------------------------------
 16    VM1                            running
 17    VM2                            running

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.c46e1f0099cd       yes             virbr0-nic
                                                        vnet0
                                                        vnet1

```

Now we have to bridge the two hosts with a dedicated network interface ``ens2``.
On the gateway host, caldera02, make ``ens2`` interface unnumbered and add it to the bridge
```
# cat /etc/sysconfig/network-scripts/ifcfg-ens2
TYPE=Ethernet
BOOTPROTO=none
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=ens2
UUID=452ee132-35a6-4c36-915b-91b85902f2b9
ONBOOT=yes
NM_CONTROLLED=no

#  brctl addif virbr0 ens2
#  brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.52540053d4ee       yes             ens2
                                                        virbr0-nic
                                                        vnet2
                                                        vnet3
```

Do the same on the second host, caldera03
```
# cat /etc/sysconfig/network-scripts/ifcfg-ens2
TYPE=Ethernet
BOOTPROTO=none
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=ens2
UUID=1de92d8b-1c83-4bf5-be22-171f65b1b66b
ONBOOT=yes
NM_CONTROLLED=no

#  brctl addif virbr0 ens2
# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.c46e1f0099cd       yes             ens2
                                                        vnet0
                                                        vnet1
```

On the host caldera03, login to VM and check the virtual machine is getting the IP address from the gateway host, caldera02. Then check the tracepath to the internet
```
# virsh console VM1
Connected to domain VM1
Escape character is ^]

caldera@ubuntu:~$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 52:54:00:36:5a:aa
          inet addr:192.168.122.101  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4975 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3031 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6741689 (6.7 MB)  TX bytes:368948 (368.9 KB)

caldera@ubuntu:~$ tracepath cisco.com
 1?: [LOCALHOST]                                         pmtu 1500
 1:  192.168.122.1                                         0.686ms
 1:  192.168.122.1                                         0.670ms
 2:  10.10.10.1                                            4.425ms

```


text:
### Backup the data
The ``rsync`` command is used to synchronize entire directory trees. Basically, it copies file as the ``cp`` command does. In addition, ``rsync`` checks if the file being copied already exists. If the file exists and there is no change in size or modification time, ``rsync`` will avoid an unnecessary copy and save time. Furthermore, because rsync copies only the parts of files that have actually changed, it can be very fast.

The ``rsync`` is very efficient when recursively copying one directory tree via network, because only the differences are transmitted. One often synchronizes the destination directory tree with the origin, using the  ``rsync -r`` option to recursively walk down the directory tree copying all files and directories below the one listed as the source.

```
# rsync -ravzh project_ABC /data/backups
sending incremental file list
project_ABC/
project_ABC/file1.txt
project_ABC/file2.txt
project_ABC/file3.txt
project_ABC/file4.txt

sent 636 bytes  received 92 bytes  1.46K bytes/sec
total size is 452  speedup is 0.62

```

### Compress the data
File data is often compressed to save disk space and reduce the time it takes to transmit files over networks. Linux uses a number of methods to perform this compression.

|Command|Usage|
|-------|-----------|
|gzip 	|The most frequently used Linux compression utility|
|bzip2  |Produces files significantly smaller than those produced by gzip|
|xz     |The most space efficient compression utility used in Linux. It is now used by kernel.org to store archives of the Linux kernel.|
|zip    |Is often required to examine and decompress archives from other operating systems|

These techniques vary in the efficiency of the compression (how much space is saved) and in how long they take to compress; generally the more efficient techniques take longer. Decompression time doesn't vary as much across different methods.

### Archiving data
The ``tar`` command allows you to create or extract files from an archive file, often called a tarball. At the same time you can optionally compress while creating the archive, and decompress while extracting its contents.

Here are some examples of the use of tar:

|Command|Usage|
|-------|-----------|
|tar xvf mydir.tar|Extract all the files in mydir.tar into the mydir directory|
|tar zcvf mydir.tar.gz mydir|Create the archive and compress with gzip|
|tar jcvf mydir.tar.bz2 mydir|Create the archive and compress with bz2|
|tar xvf mydir.tar.gz|Extract all the files in mydir.tar.gz into the mydir directory.|
|tar cvf  mydir.tar|show the content into the mydir directory|

### Copying disks
The ``dd`` command is very useful for making copies of raw disk space. For example, to back up the Master Boot Record (MBR) (the first 512 byte sector on the disk that contains a table describing the partitions on that disk), use:
```
# dd if=/dev/sda of=sda.mbr bs=512 count=1
```
To use dd to make a copy of one disk onto another, deleting everything that previously existed on the second disk, use:
```
# dd if=/dev/sda of=/dev/sdb
```
An exact copy of the first disk device is created on the second disk device.

The ``dd`` command is usefull to duplicate a bootable disk as a Compact Flash card, a Micro SD card or a bootable USB dongle. Insert the CF Card to be copied into the system and make a copy
```
# dd if=/dev/sdb of=./backup.img
```
Remove the CF Card, insert a new one and make a new copy
```
# dd if=./backup.img of=/dev/sdc
```

repo_name: MinhKMA/Linux-Tutorial

mime_type: text/plain


text:
## Network interfaces
Network interfaces are a connection channel between a device and a network. Physically, network interfaces can proceed through a network interface card (**NIC**) or can be more abstractly implemented as software. You can have multiple network interfaces operating at once. Specific interfaces can be brought up (activated) or brought down (de-activated) at any time. A list of currently active network interfaces is reported by the ``ifconfig`` utility. Network configuration files are essential to ensure that interfaces function correctly.

For **Debian** family configuration, the basic network configuration file is ``/etc/network/interfaces``. For **RedHat** family system configuration, the routing and host information is contained in ``/etc/sysconfig/network``. The network interface configuration script for the ``eth0`` interface is located at ``/etc/sysconfig/network-scripts/ifcfg-eth0``. For **SUSE** family system configuration, the routing and host information and network interface configuration scripts are contained in the ``/etc/sysconfig/network`` directory.

```
# cat /etc/sysconfig/network-scripts/ifcfg-enp0s25
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=no
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s25
UUID=d9315bd4-159b-4871-95f5-98f2fbcc5a06
ONBOOT=yes
HWADDR=00:24:81:0F:EC:DE
IPADDR=10.10.10.97
PREFIX=24
GATEWAY=10.10.10.1
DNS=8.8.8.8
```

The ``ip`` is a very powerful program that can do many things.
```
# ip addr show
# ip route show
```

### Routing table
The ``route`` command is used to view or change the IP routing table. You may want to change the IP routing table to add, delete or modify static routes to specific hosts or networks.

```
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.10.10.1      0.0.0.0         UG    0      0        0 enp0s25
10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 enp0s25
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 enp48s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 enp0s25
172.25.101.0    0.0.0.0         255.255.255.0   U     0      0        0 enp48s0
#
# route add 10.58.47.235 gw 172.25.101.1
route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.10.10.1      0.0.0.0         UG    0      0        0 enp0s25
10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 enp0s25
10.58.47.235    172.25.101.1    255.255.255.255 UGH   0      0        0 enp48s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 enp48s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 enp0s25
172.25.101.0    0.0.0.0         255.255.255.0   U     0      0        0 enp48s0
#
# route delete 10.58.47.235 gw 172.25.101.1
# route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.10.10.1 enp0s25
# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.10.10.1      0.0.0.0         UG    0      0        0 enp0s25
10.0.0.0        10.10.10.1      255.0.0.0       UG    0      0        0 enp0s25
10.10.10.0      0.0.0.0         255.255.255.0   U     0      0        0 enp0s25
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 enp48s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 enp0s25
172.25.101.0    0.0.0.0         255.255.255.0   U     0      0        0 enp48s0
# route delete -net 10.0.0.0 netmask 255.0.0.0 gw 10.10.10.1 enp0s25
```


text:
### Filesystem Structure
On many systems, including Linux, the **filesystem** is structured like a tree. The tree is usually portrayed as inverted, and starts at what is most often called the **root** directory, which marks the beginning of the hierarchical filesystem and is also denoted by **/**.

The Filesystem Hierarchy Standard (**FHS**) grew out of historical standards from early versions of UNIX. The FHS provides Linux developers and system administrators with a standard directory structure for the filesystem, which provides consistency between systems and distributions. Linux supports various filesystem types created for Linux, along with compatible filesystems from other operating systems. Many older, legacy filesystems are supported. Some examples of filesystem types that Linux supports are:

1. **ext3**, **ext4**, **btrfs**, **xfs** (native Linux filesystems)
2. **vfat**, **ntfs**, **hfs** (filesystems from other operating systems)

Each filesystem resides on a hard disk **partition**. Partitions help to organize the contents of disks according to the kind of data contained and how it is used. For example, important programs required to run the system are often kept on a separate partition than the one that contains files owned by regular users. In addition, temporary files created and destroyed during the normal operation of Linux are often located on a separate partition; in this way, using all available space on a particular partition may not fatally affect the normal operation of the system.

Before you can start using a filesystem, you need to mount it to the filesystem tree at a **mountpoint**. This is simply a directory (which may or may not be empty) where the filesystem is to be attached (mounted). Sometimes you may need to create the directory if it doesn't already exist. If you mount a filesystem on a non-empty directory, the former contents of that directory are covered-up and not accessible until the filesystem is unmounted. Thus mount points are usually empty directories.

The ``mount`` command is used to attach a filesystem somewhere within the filesystem tree. Arguments include the device node and mount point.
```
$ mount /dev/sda5 /mnt
```
This will attach the filesystem contained in the disk partition associated with the ``/dev/sda5`` device node, into the filesystem tree at the ``/mnt`` mount point. Note that unless the system is otherwise configured only the root user has permission to run mount. If you want it to be automatically available every time the system starts up, you need to edit the file ``/etc/fstab`` accordingly. The name is short for Filesystem Table. Looking at this file will show you the configuration of all pre-configured filesystems.

The ``umount`` command is used to detach the filesystem from the mount point.
```
$ umount /mnt
```

The command ``df -Th`` (it stands for disk-free) will display information about mounted filesystems including type and usage statistics about currently used and available space.

```
# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
/dev/mapper/os-root        xfs        50G  2.0G   48G   4% /
devtmpfs                   devtmpfs  1.8G     0  1.8G   0% /dev
tmpfs                      tmpfs     1.9G  4.0K  1.9G   1% /dev/shm
tmpfs                      tmpfs     1.9G  8.6M  1.8G   1% /run
tmpfs                      tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/swift01-zone01 xfs        49G   33M   49G   1% /srv/node/z1d1
/dev/mapper/swift02-zone02 xfs        49G   33M   49G   1% /srv/node/z2d1
/dev/sda1                  xfs       497M  167M  331M  34% /boot
/dev/mapper/os-data        xfs        20G  261M   20G   2% /data
```

### The home directories
In any UNIX system, each user has his own home directory, usually placed under ``/home``. The ``/root`` directory on modern Linux systems is no more than the root user's home directory. The ``/home`` directory is often mounted as a separate filesystem on its own partition, or even exported remotely on a network through NFS.

### The binary directories
The ``/bin`` directory contains executable binaries, essential commands used in single-user mode, and essential commands required by all system users, such as ``ps``, ``ls``, ``cp``. Commands that are not essential for the system in single-user mode are placed in the ``/usr/bin`` directory, while the ``/sbin`` directory is used for essential binaries related to system administration, such as ``ifconfig`` and ``shutdown``. There is also a ``/usr/sbin`` directory for less essential system administration programs. All the binary directories are under the root partition. Sometimes ``/usr`` is a separate filesystem that may not be available in single-user mode. This was why essential commands were separated from non-essential commands. However, in some of the most modern Linux systems this distinction is considered obsolete, and ``/usr/bin`` and ``/bin`` are actually just linked together as are ``/usr/sbin`` and ``/sbin``.

### The device directory
The ``/dev`` directory contains device nodes, a type of pseudo-file used by most hardware and software devices, except for network devices. This directory is empty on the disk partition when it is not mounted but it contains entries which are created by the ``udev`` system, which creates and manages device nodes on Linux, creating them dynamically when devices are found. The ``/dev`` directory contains items such as:
```
/dev/sda1
/dev/lp1
/dev/dvd1
```

### The variable directory
The ``/var`` directory contains files that are expected to change in size and content as the system is running (var stands for variable) such as the entries in the following directories:

* System log files: ``/var/log``
* Packages files: ``/var/lib``
* Print queues: ``/var/spool``
* Temp files: ``/var/tmp``
* FTP home directory: ``/var/ftp``
* Web Server directory: ``/var/www``

The ``/var`` directory may be put in its own partition so that growth of the files can be accommodated and the file sizes do not fatally affect the system.

### The system configuration directory
The ``/etc`` directory is the home for system configuration files. It contains no binary programs, although there are some executable scripts. For example, the file ``resolv.conf`` tells the system where to go on the network to obtain host name to IP address mappings (DNS). Files like ``passwd``, ``shadow`` and ``group`` for managing user accounts are found in the ``/etc`` directory. System run level scripts are found in subdirectories of ``/etc``. For example, ``/etc/rc2.d`` contains links to scripts for entering and leaving run level 2. Some Linux distributions extend the contents of ``/etc``. For example, **Red Hat** adds the ``/etc/sysconfig`` subdirectory that contains more configuration files.

### The boot directory
The ``/boot`` directory contains the few essential files needed to boot the system. For every alternative kernel installed on the system there are four files:

* ``vmlinuz`` is the compressed Linux kernel, required for booting
* ``initramfs`` is the initial ram filesystem, required for booting
* ``config is`` the kernel configuration file, only used for debugging
* ``System.map`` contains the kernel symbol table, only used for debugging

Each of these files has a kernel version appended to its name.

### The libraries directory
The ``/lib`` contains libraries (common code shared by applications and needed for them to run) for the essential programs in ``/bin`` and ``/sbin`` folders. Most of these are what are known as dynamically loaded libraries (also known as shared libraries or Shared Objects (SO)). On some Linux distributions there exists a ``/lib64`` directory containing 64-bit libraries, while ``/lib`` contains 32-bit versions. Kernel modules (kernel code, often device drivers, that can be loaded and unloaded without re-starting the system) are located in ``/lib/modules/``.

### Additional directories

|Directory|Usage|
|---------|-----|
| /opt | Optional application software packages |
| /sys | Virtual pseudo-filesystem giving information about the system and the hardware. Can be used to alter system parameters and for debugging purposes. |
| /srv | Site-specific data served up by the system. Seldom used. |
| /tmp | Temporary files; on some distributions these files are erased across a reboot |
| /media | It is typically located where removable media, such as CDs, DVDs and USB drives are mounted. Unless configuration prohibits it, Linux automatically mounts the removable media in this directory when they are detected. |
| /usr | Multi-user applications, utilities and data |
| /usr/include | Header files used to compile applications |
| /usr/lib | Libraries for binary programs |
| /usr/lib64 | 64bit Libraries for binary programs |
| /usr/share | Shared data used by applications, generally architecture-independent |
| /usr/src | Source code, usually for the Linux kernel |
| /usr/local | Data and programs specific to the local machine. |

### File System Table
for details on the file system table, i.e. the ``/etc/fstab`` file, please see [fstab (Italian)](https://wiki.archlinux.org/index.php/Fstab_%28Italiano%29#Dischi_esterni)


text:
### Linux release and system info
Linux System Administrators need to get info from the system. Here some useful commands.

Linux release and distribution
```
# cat /etc/*release
CentOS Linux release 7.0.1406 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CentOS Linux release 7.0.1406 (Core)
```
Kernel version
```
# uname -r
3.10.0-123.13.2.el7.x86_64
```
Memory Info
```
# head /proc/meminfo
MemTotal:        3776748 kB
MemFree:         2230496 kB
MemAvailable:    2782088 kB
Buffers:            1452 kB
Cached:           652196 kB
SwapCached:            0 kB
Active:          1069616 kB
Inactive:         193056 kB
Active(anon):     609504 kB
Inactive(anon):     8304 kB
```
File system
```
# df -h
Filesystem         Dimens. Usati Disp. Uso% Montato su
/dev/sda1          12G     6,2G  4,9G  56%      /
/dev/small-db02    5,9G    2,6G  3,0G  46%      /db02
/dev/small-db01    5,0G    3,6G  1,2G  77%      /db01
/dev/small-db05    7,8G    1,2G  6,2G  17%      /db05
/dev/small-db03    39G     5,4G   32G  15%      /db03
/dev/small-db04    30G     2,5G   26G   9%      /db04
```

Count the number of CPU
```
# cat /proc/cpuinfo | grep model | uniq -c
      2 model name      : Intel(R) Core(TM)2 Duo CPU     E8500  @ 3.16GHz
```

### The proc Filesystem
The ``/proc`` filesystem contains virtual files that exist only in memory. This filesystem contains files and directories that mimic kernel structures and configuration information. It doesn't contain real files but runtime system information (e.g. system memory, devices mounted, hardware configuration, etc). Some important files in ``/proc`` are:

```
/proc/cpuinfo
/proc/interrupts
/proc/meminfo
/proc/mounts
/proc/partitions
/proc/version
/proc/<process-id-#>
/proc/sys
```
The ``/proc`` filesystem is very useful because the information it reports is gathered only as needed and never needs storage on disk.

### Hostname
The hostname identifies the machine within the domain.
```
# cat /etc/hostname
```
Set a new host name
```
# hostname NEW_NAME
```
This will set the hostname of the system to NEW_NAME. This is active right away and will remain like that until the system will be rebooted. On **Debian** based systems, use the file ``/etc/hostname`` to read the hostname of the system at boot time and set it up using the init script ``/etc/init.d/hostname.sh``. The hostname saved in the file ``/etc/hostname`` will be preserved on system reboot and will be set using the same script we used.

On **RedHat** based systems, use the ``hostnamectl`` utility to get and set the hostname.

```
# hostnamectl status
   Static hostname: caldera01
         Icon name: computer-desktop
           Chassis: desktop
        Machine ID: <machineId>
           Boot ID: <bootId>
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-123.13.2.el7.x86_64
      Architecture: x86_64
```


text:
## Shared storage on the network with iSCSI
Many ways to share storage on a network exist. The iSCSI protocol defines a way to see a remote blocks device as a local disk. A remote device on the network is called iSCSI Target, a client which connects to iSCSI Target is called iSCSI Initiator.

### iSCSI Target Setup
Install admin tools first, configure target to persistantly start at boot time and then start it
```
# yum -y install targetcli
# systemctl enable target
# systemctl start target
```
To start using ``targetcli``, run it and to get a layout of the tree interface, run ls
```
# targetcli
targetcli shell version 2.1.fb37
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/> ls
o- / .............................................................................................................. [...]
  o- backstores ................................................................................................... [...]
  | o- block ....................................................................................... [Storage Objects: 0]
  | o- fileio ...................................................................................... [Storage Objects: 0]
  | o- pscsi ....................................................................................... [Storage Objects: 0]
  | o- ramdisk ..................................................................................... [Storage Objects: 0]
  o- iscsi ................................................................................................. [Targets: 0]
  o- loopback .............................................................................................. [Targets: 0]
/>
```
#### Create a Backstore
Backstores enable support for different methods of storing an object on the local machine. Creating a storage object defines the resources the backstore will use. The supported backstores are: block devices, files, pscsi and ramdisks. Block devices are in our case.
```
/> /backstores/block create name=block_storage dev=/dev/sdb1
Generating a wwn serial.
Created block storage object block_backend using /dev/sdb1.
```
#### Create an iSCSI Target
Create an iSCSI target using a specified name
```
/> iscsi/ create iqn.2015-05.com.noverit.caldara02:3260
Created target iqn.2015-05.com.noverit.caldara02:3260.
Created TPG 1.
```
#### Configure an iSCSI Portal
An iSCSI Portal is an object specifying the IP address and port where the iSCSI target listen to incoming connections
```
/> /iscsi/iqn.2015-05.com.noverit.caldara02:3260/tpg1/portals/ create
Using default IP port 3260
Binding to INADDR_ANY (0.0.0.0)
Created network portal 0.0.0.0:3260
```
By default, a portal is created when the iSCSI Target is created listening on all IP addresses (0.0.0.0) and the default iSCSI port 3260. Make sure that the 3260 is not used by another application, else specify a different port.

#### Configure Access List
Create an Access List for each initiator that will be connecting to the target. This enforces authentication when that initiator connects, allowing only LUNs to be exposed to each initiator. Usually each initator has exclusive access to a LUN. All initiators have unique identifying names IQN. The initiator's unique name IQN must be known to configure ACLs. For open-iscsi initiators, this can be found in the ``/etc/iscsi/initiatorname.iscsi`` file.
```
# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.1994-05.com.redhat:2268c31791
```
If required, use this IQN to enforce authentication by creating the ACLs.

#### Configure the LUNs
A Logical Unit Number (LUN) is a number used to identify a logical unit, which is a device addressed by the standard SCSI protocol or Storage Area Network protocols which encapsulate SCSI, such as Fibre Channel or iSCSI itself.
To configure LUNs, create LUNs of already created storage objects.
```
/> /iscsi/iqn.2015-05.com.noverit.caldara02:3260/tpg1/luns/ create /backstores/block/block_storage
Created LUN 0.
```
At the end of configuration, the iSCSI target envinronment should look like the following
```
/> ls
o- / ........................................................................................................... [...]
  o- backstores ................................................................................................ [...]
  | o- block .................................................................................... [Storage Objects: 2]
  | | o- ana-storage ...................................................... [/dev/sdb1 (20.0GiB) write-thru activated]
  | | o- oracle-storage .................................................. [/dev/sdb2 (120.0GiB) write-thru activated]
  | o- fileio ................................................................................... [Storage Objects: 0]
  | o- pscsi .................................................................................... [Storage Objects: 0]
  | o- ramdisk .................................................................................. [Storage Objects: 0]
  o- iscsi .............................................................................................. [Targets: 1]
  | o- iqn.2015-05.com.noverit.caldara02:3260 .............................................................. [TPGs: 1]
  |   o- tpg1 .................................................................................... [gen-acls, no-auth]
  |     o- acls ............................................................................................ [ACLs: 0]
  |     o- luns ............................................................................................ [LUNs: 2]
  |     | o- lun0 .................................................................... [block/ana-storage (/dev/sdb1)]
  |     | o- lun1 ................................................................. [block/oracle-storage (/dev/sdb2)]
  |     o- portals ...................................................................................... [Portals: 1]
  |       o- 10.10.10.98:3260 ................................................................................... [OK]
  o- loopback ........................................................................................... [Targets: 0]
/>
/> exit
Global pref auto_save_on_exit=true
Last 10 configs saved in /etc/target/backup.
Configuration saved to /etc/target/saveconfig.json
```
The ``/etc/target/saveconfig.json`` file contains the above configuration.

Restart the target service
```
# service target restart
Redirecting to /bin/systemctl restart  target.service
```
### iSCSI Initiator Setup
After configuring the iSCSI on the target machine, move to setup the iSCSI initiator machine.
Install admin tools first

```
# yum -y install iscsi-initiator-utils
```
The iSCSI initiator is composed by two services, iscsi and iscsid, start both and enable to start at system startup
```
# service iscsid start
# service iscsi start
# service iscsid status
# service iscsi status
# chkconfig iscsi on
# chkconfig iscsid on
# chkconfig --list | grep iscsi
iscsi           0:off   1:off   2:off   3:on    4:on    5:on    6:off
iscsid          0:off   1:off   2:on    3:on    4:on    5:on    6:off
```

To connect the target, first discover the published iSCSI resouces and then login
```
# iscsiadm --mode discovery --type sendtargets --portal caldara02:3260 --discover
10.10.10.98:3260,1 iqn.2015-05.com.noverit.caldara02:3260
# iscsiadm --mode node --targetname iqn.2015-05.com.noverit.caldara02:3260 --portal caldara02:3260 --login
Logging in to [iface: default, target: iqn.2015-05.com.noverit.caldara02:3260, portal: 10.10.10.98,3260] (multiple)
Login to [iface: default, target: iqn.2015-05.com.noverit.caldara02:3260, portal: 10.10.10.98,3260] successful.
#
```
Since no authentication has been set, no user and password are required.
Check the storage block devices.
```
[root@caldara01 ~]# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                         8:0    0 232.9G  0 disk
├─sda1                      8:1    0   500M  0 part /boot
├─sda2                      8:2    0  73.4G  0 part
│ ├─os-swap               253:0    0   3.9G  0 lvm  [SWAP]
│ ├─os-root               253:1    0    50G  0 lvm  /
│ └─os-data               253:2    0 178.5G  0 lvm  /data
└─sda3                      8:3    0   159G  0 part
  └─os-data               253:2    0 178.5G  0 lvm  /data
sdc                         8:32   0    20G  0 disk
sdd                         8:48   0   120G  0 disk
```
The two disks ``/dev/sdc`` and ``/dev/sdd`` are the remote iSCSI block devices exported by the target. They are seen as local block devices in the initiator machine. The disks can be used as standard local disks commands and configurations, including ``fdisk``, ``mkfs``, ``e2label``, etc.

```
# e2label /dev/sdc ANA
# e2label /dev/sdd ORACLE
# mkdir /ana
# mkdir /oracle
# mount -L ANA /ana
# mount -L ORACLE /oracle
# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/os-root   50G  2.8G   48G   6% /
devtmpfs             3.8G     0  3.8G   0% /dev
tmpfs                3.8G     0  3.8G   0% /dev/shm
tmpfs                3.8G  370M  3.4G  10% /run
tmpfs                3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/mapper/os-data  179G   22G  158G  12% /data
/dev/sda1            497M  228M  270M  46% /boot
/dev/sdc              20G   45M   19G   1% /ana
/dev/sdd             118G   60M  112G   1% /oracle
```
To disconnect the remote devices, umount and logout
```
# umount /ana
# umount /oracle
#
#  iscsiadm --mode node --targetname iqn.2015-05.com.noverit.caldara02:3260 --portal 10.10.10.98 --logout
Logging out of session [sid: 10, target: iqn.2015-05.com.noverit.caldara02:3260, portal: 10.10.10.98,3260]
Logout of [sid: 10, target: iqn.2015-05.com.noverit.caldara02:3260, portal: 10.10.10.98,3260] successful.
#
```

Stop and then disable the services at startup, if required
```
# service iscsid status
iscsid (pid  1184) is running...
# service iscsi status
No active sessions
# service iscsid stop
Stopping iscsid:                      [  OK  ]
# service iscsi stop
Stopping iscsi:                       [  OK  ]
# chkconfig iscsid off
# chkconfig iscsi off
# chkconfig --list | grep iscsi
iscsi           0:off   1:off   2:off   3:off   4:off   5:off   6:off
iscsid          0:off   1:off   2:off   3:off   4:off   5:off   6:off
```


text:
### File permissions
In Linux and other UNIX operating systems, every file is associated with a user who is the owner. Every file is also associated with a group which has an interest in the file and certain rights, or permissions: read, write, and execute.

|Command|Result|
|-------|-----------|
|chown|Used to change user ownership of a file or directory|
|chgrp|Used to change group ownership|
|chmod|Used to change the permissions on the file|

Files have three kinds of permissions: read (**r**), write (**w**), execute (**x**). These are generally represented as in the following order **rwx**. These permissions affect three groups of owners: user (**u**), group (**g**), and others (**o**). As a result, you have the following three groups of three permissions:

|rwx:|rwx:|rwx|
|----|----|---|
|u:|g:|o|

There are a number of different ways to use the ``chmod`` command. For instance, to give the owner execute permission:

```
$ ls -l test1
-rw-rw-r-- 1 joy caldera 1601 Mar 9 15:04 test1
$ chmod u+x test1
$ ls -l test1
-rwxrw-r-- 1 joy caldera 1601 Mar 9 15:04 test1
```

This kind of syntax can be difficult to type and remember, so one often uses a shorthand which lets you set all the permissions in one step. This is done with a simple algorithm, and a single digit suffices to specify all three permission bits for each entity. This digit is the sum of:

* 4 if read permission is desired.
* 2 if write permission is desired.
* 1 if execute permission is desired.

Thus 7 means read+write+execute, 6 means read+write, and 5 means read+execute.

When you apply this to the ``chmod`` command you have to give three digits for each degree of freedom, such as in
```
$ chmod 755 test1
$ ls -l test1
-rwxr-xr-x 1 joy caldera 1601 Mar 9 15:04 test1
```
The group ownership is changed by using the ``chgrp`` command
```
# ll /home/mina/myfile.txt
-rw-rw-r--. 1 mina caldera 679 Feb 19 16:51 /home/mina/myfile.txt
# chgrp root /home/mina/myfile.txt
# ll /home/mina/myfile.txt
-rw-rw-r--. 1 mina root 679 Feb 19 16:51 /home/mina/myfile.txt
```


text:
## Network Filesystem
Using **NFS** (the Network File System) is one of the methods used for sharing data across physical systems. Many system administrators mount remote users' home directories on a server in order to give them access to the same files and configuration files across multiple client systems. This allows the users to log in to different machines yet still have access to the same files and resources.

On a generic Linux distribution, the NFS server daemon is typically started with the command ``service nfs start``. The file ``/etc/exports`` contains the directories and permissions that a host is willing to share with other systems over NFS. An entry in this file may look like ``/shared *(rw)``. This entry allows the directory ``/shared`` to be mounted using NFS with read and write (rw) permissions and shared with other hosts in the same domain. After modifying the ``/etc/exports`` file, you can use the ``exportfs -av`` command to notify Linux about the directories you are allowing to be remotely mounted using NFS.

On the client machine, if it is desired to have the remote filesystem mounted automatically upon system boot, the ``/etc/fstab`` file is modified to accomplish this. For example, an entry in the client's ``/etc/fsta``b file might look like ``<servername>:/shared /mnt/nfs/shared nfs defaults 0 0``. You can also mount the remote filesystem without a reboot or as a one-time mount by directly using the ``mount`` command. If ``/etc/fstab`` is not modified, this remote mount will not be present the next time the system is restarted.

On RedHat based distributions (CentOS-7) NFS server
```
# yum install -y nfs-utils
# mkdir /var/shared
```
Add an entry into the ``/etc/exports`` file
```
# vi /etc/exports
# /var/shared 10.10.10.0/24(no_root_squash,no_all_squash,rw,sync)
```
Where:
* ``/var/shared`` is the shared folder
* ``10.10.10.0/24`` is IP address range of clients
* ``rw`` is the permission to shared folder
* ``sync`` synchronizes shared folder
* ``root_squash`` disable the root privilege
* ``no_root_squash`` enables the root privilege
* ``no_all_squash`` enables the user’s authority

The ``no_root_squash`` option leaves root users on NFS clients to write files as root user on the NFS server. Default is ``root_squash``.

```
# systemctl start rpcbind
# systemctl start nfs-server
# systemctl enable rpcbind
# systemctl enable nfs-server
# systemctl status rpcbind
# systemctl status nfs-server
```

On the client machine mount the shared folder to a local folder
```
# mkdir -p /mnt/nfs
# mount 10.10.10.97:/var/shared /mnt/nfs
# cd /mnt/nfs
# touch filename.txt
```
**Note**: this is only for explanation. Please, do not use it in production systems. Check the NFS resources related to your distribution.

To run a NFS server behind the firewall, you should make some changes on the NFS configuration file, e.g. ``/etc/sysconfig/nfs`` on Red Hat/CentOS family, and then enable the ports on the firewall configuration. The reason is that NFS requires the ``rpcbind`` service, which dynamically assigns ports for RPC services and can cause problems for configuring firewall rules. See: [http://initrd.org/wiki/NFS_Setup](http://initrd.org/wiki/NFS_Setup)


text:
### Linux Network Namespaces
Dedicated networking devices use Virtual Routing and Forwarding (VRF), meaning that more than one virtual router (Layer 3 forwarding instance) can be run on the same physical device. In the Linux virtual networking space, the network namespaces allow separate instances of network interfaces and routing tables to operate independent of each other.

#### Basic operations on namespaces
In Linux, yust be root for all operations which change the configuration of the network stack.

Creating a network namespace

    [root@centos-01 ~]# ip netns add Blue
    [root@centos-01 ~]# ip netns list
    Blue
    [root@centos-01 network-scripts]# ll /var/run/netns/
    total 0
    -r--r--r-- 1 root root 0 Feb 11 16:29 Blue

Each network namespace has its own loopback interface, its own routing table and its own iptables setup providing nat and filtering.

    [root@centos-01 ~]# ip netns exec Blue ip addr list
    1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

Make sure to bring up that interface before to operate with the network namespace

    [root@centos-01 ~]# ip netns exec Blue ip link set dev lo up
    [root@centos-01 ~]# ip netns exec Blue ifconfig
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Network namespaces offer in addition the capability to run processes within the network namespace. For example, run a bash session in the Blue namespace

    [root@centos-01 ~]# ip netns exec Blue bash
    [root@centos-01 ~]# ifconfig
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    [root@centos-01 ~]# netstat -nr
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    [root@centos-01 ~]# exit

Delete the namespace

    [root@centos-01 ~]# ip netns add Yellow
    [root@centos-01 ~]# ip netns list
    Yellow
    Blue
    [root@centos-01 ~]# ip netns delete Yellow
    [root@centos-01 ~]# ip netns list
    Blue

#### Add interfaces to network namespaces
To connect a network namespace to the outside world, attach a virtual interface to the “default” or “global” namespace where physical interfaces exist. To accomplish this, let's to create a couple of virtual interfaces, called ``vetha`` and ``vethb``

    [root@centos-01 ~]# ip link add vetha type veth peer name vethb

Attach ``vethb`` to the Blue namespace

    [root@centos-01 ~]# ip link set vethb netns Blue
    [root@centos-01 ~]# ip netns exec Blue ip link set dev vethb up
    [root@centos-01 ~]# ip netns exec Blue ifconfig
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
    vethb: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
            ether 7e:e4:29:bc:9c:67  txqueuelen 1000  (Ethernet)

Virtual network interface ``vetha`` remain attacched to the global namespace

    [root@centos-01 ~]# ip link set dev vetha up
    [root@centos-01 ~]# ifconfig
    ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.10.10.21  netmask 255.255.255.0  broadcast 10.10.10.255
            inet6 fe80::20c:29ff:fe1e:6bf1  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:1e:6b:f1  txqueuelen 1000  (Ethernet)
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
    vetha: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet6 fe80::e899:ceff:fef6:3010  prefixlen 64  scopeid 0x20<link>
            ether ea:99:ce:f6:30:10  txqueuelen 1000  (Ethernet)

Configure the virtual interface in global network namespace

    [root@centos-01 ~]# ip addr add 192.168.100.1/24 dev vetha
    [root@centos-01 ~]# route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         gateway         0.0.0.0         UG    100    0        0 ens32
    10.10.10.0      0.0.0.0         255.255.255.0   U     100    0        0 ens32
    192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 vetha
    [root@centos-01 ~]#

and in the Blue network namespace

    [root@centos-01 ~]# ip netns exec Blue ip addr add 192.168.100.2/24 dev vethb
    [root@centos-01 ~]# ip netns exec Blue route
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 vethb
    [root@centos-01 ~]#

Both the namespaces, Blue and global are now reachable each other via virtual network interfaces

    [root@centos-01 ~]# ping 192.168.100.2
    PING 192.168.100.2 (192.168.100.2) 56(84) bytes of data.
    64 bytes from 192.168.100.2: icmp_seq=1 ttl=64 time=0.041 ms
    64 bytes from 192.168.100.2: icmp_seq=2 ttl=64 time=0.029 ms
    ^C
    [root@centos-01 ~]# ip netns exec Blue ping 192.168.100.1
    PING 192.168.100.1 (192.168.100.1) 56(84) bytes of data.
    64 bytes from 192.168.100.1: icmp_seq=1 ttl=64 time=0.034 ms
    64 bytes from 192.168.100.1: icmp_seq=2 ttl=64 time=0.039 ms
    ^C

But they are completly separated routing entities

    [root@centos-01 ~]# ip netns exec Blue ping 10.10.10.1
    connect: Network is unreachable
    [root@centos-01 ~]#
    [root@centos-01 ~]# ping 10.10.10.1
    PING 10.10.10.1 (10.10.10.1) 56(84) bytes of data.
    64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=0.545 ms
    64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=0.369 ms
    ^C


text:
## Linux basic security
By default, Linux has several account types in order to isolate processes and workloads:

1. **root**
2. **system**
2. **normal**
3. **network**

For a safe environment, it is advised to grant the minimum privileges possible and necessary to accounts, and remove inactive accounts. The ``last`` command, which shows the last time each user logged into the system, can be used to help identify potentially inactive accounts which are candidates for system removal.
```
# last
adriano  pts/4        10.10.10.113     Thu Feb 19 16:50   still logged in
mina     pts/2        10.10.10.113     Thu Feb 19 16:39   still logged in
root     pts/1        10.10.10.113     Thu Feb 19 16:25 - 16:25  (00:00)
root     pts/0        10.10.10.113     Thu Feb 19 15:42   still logged in
adriano  pts/3        10.10.10.246     Wed Feb 18 17:53 - 18:44  (00:51)
root     pts/2        10.10.10.99      Wed Feb 18 17:14 - 18:44  (01:30)
adriano  pts/1        10.10.10.246     Wed Feb 18 16:57 - 19:19  (02:22)
root     pts/0        10.10.10.246     Wed Feb 18 16:25 - 19:19  (02:53)
root     pts/0        10.10.10.246     Tue Feb 17 13:29 - 19:29  (06:00)
reboot   system boot  3.10.0-123.20.1. Tue Feb 17 13:28 - 17:20 (2+03:51)
```

The **root** account is the most privileged account on a Linux/UNIX system. This account has the ability to carry out all facets of system administration, including adding accounts, changing user passwords, examining log files, installing software, etc.

A regular account user can perform some operations requiring special permissions; however, the system configuration must allow such abilities to be exercised. Running a network client or sharing a file over the network are operations that do not require a root account.

In Linux you can use either ``su`` or ``sudo`` commands to temporarily grant root access to a normal user; these methods are actually quite different. When using the ``su`` command:

* to elevate the privilege, you need to enter the root password. Giving the root password to a normal user should never, ever be done
* once a user elevates to the root account, the normal user can do anything that the root user can do for as long as the user wants, without being asked again for a password
* there are limited logging features

When using the ``sudo`` command:

* you need to enter the user’s password and not the root password
* what the user is allowed to do can be precisely controlled and limited; by default the user will either always have to keep giving their password to do further operations with ``sudo``, or can avoid doing so for a configurable time interval
* detailed logging features are available

### The sudo command
Granting privileges using the ``sudo`` command is less dangerous than ``su`` and it should be preferred. By default, ``sudo`` must be enabled on a per-user basis. However, some distributions (such as Ubuntu) enable it by default for at least one main user, or give this as an installation option. To execute just one command with root privilege type ``sudo <command>``. When the command is complete you will return to being a normal unprivileged user. The ``sudo`` configuration files are stored in the ``/etc/sudoers`` file and in the ``/etc/sudoers.d/`` directory. By default, that directory is empty.

The ``sudo`` command has the ability to keep track of unsuccessful attempts at gaining root access. An authentication failure message would appear in the ``/var/log/secure`` log file  when trying to execute sudo bash without successfully authenticating the user

```
# tail /var/log/secure
authentication failure; logname=op uid=0 euid=0 tty=/dev/pts/6 ruser=op rhost= user=op
conversation failed
auth could not identify password for [op]
op : 1 incorrect password attempt ;
TTY=pts/6 ; PWD=/var/log ; USER=root ; COMMAND=/bin/bash
```

Whenever the ``sudo`` command is invoked, a trigger will look at ``/etc/sudoers`` and the files in ``/etc/sudoers.d`` to determine if the user has the right to use ``sudo`` and what the scope of their privilege is. Unknown user requests and requests to do operations not allowed to the user even with ``sudo`` are reported. You can edit the sudoers file by using the ``visudo`` command, which ensures that only one person is editing the file at a time, has the proper permissions, and refuses to write out the file and exit if there is an error in the changes made.

The basic structure of an entry is:
> who where = (as_whom) what

To create a normal user account and give it sudo access, login as root user and edit the ``/etc/sudoers`` file with the ``visudo`` command. Find the lines in the file that grant ``sudo`` access to users in the group ``wheel`` when enabled.
```
## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL
```
Remove the comment character at the start of the second line. This enables the configuration option. Save your changes. Add the user you created to the ``wheel`` group.
```
# usermod -aG wheel adriano
# su adriano -
$ groups
adriano wheel
$ sudo whoami
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for adriano:
root
```
If sudo is configured correctly the last line value will be ``root``.

Some Linux distributions prefer you add a file in the directory ``/etc/sudoers.d`` with a name the same as the user. This file contains the individual user's sudo configuration, and one should leave the master configuration file untouched except for changes that affect all users.

### The process isolation
Linux is considered to be more secure than many other operating systems because processes are naturally isolated from each other. One process normally cannot access the resources of another process, even when that process is running with the same user privileges. Additional security mechanisms that have been recently introduced in order to make risks even smaller are:

1. **Control Groups**: allows system administrators to group processes and associate finite resources to each group (**cgroup**).
2. **Linux Containers**: makes it possible to run multiple isolated Linux systems containers on a single system.
3. **Virtualization**: hardware is emulated in such a way that not only processes can be isolated, but entire systems are run simultaneously as isolated and insulated guests (**virtual machines**) on one physical host.

### Password encryption
Protecting passwords has become a crucial element of security. Most Linux distributions rely on a modern password encryption algorithm called SHA-512 (Secure Hashing Algorithm 512 bits), developed by the U.S. National Security Agency (NSA) to encrypt passwords. The SHA-512 algorithm is widely used for security applications and protocols. These security applications and protocols include TLS, SSL, PHP, SSH, S/MIME and IPSec. SHA-512 is one of the most tested hashing algorithms.

### Password aging
The password aging is a method to ensure that users get prompts that remind them to create a new password after a specific period. This can ensure that passwords, if cracked, will only be usable for a limited amount of time. This feature is implemented using the ``chage`` command, which configures the password expiry information for a user.
```
# chage --list adriano
Last password change                                    : Feb 18, 2015
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7
```

### Public/Private Keys for Authentication
Using encrypted keys for authentication offers two main benefits. Firstly, it is convenient as you no longer need to enter a password if you use public/private keys. Secondly, once public/private key pair authentication has been set up on the server, you can disable password authentication completely meaning that without an authorized key you can't gain access.

Create a private key for client and a public key for server to do it
```
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

# cd /root/.ssh
# ll
total 8
-rw------- 1 root root    0 May 30 11:17 authorized_keys
-rw------- 1 root root 1675 May 30 11:17 id_rsa
-rw-r--r-- 1 root root  396 May 30 11:17 id_rsa.pub
-rw-r--r-- 1 root root    0 May 30 11:07 known_hosts
# chmod 700 ~/.ssh
# chmod 600 ~/.ssh/id_rsa
```

This will create two files in your hidden ssh directory called: ``id_rsa`` and ``id_rsa.pub`` The first is your private key and the other is your public key. Install the public key to the authorized keys list and then remove it from the server
```
# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
# rm -rf ~/.ssh/id_rsa.pub
```
Please, note that the same public key can be installed to many servers, just copy it on that server and install to the authorized keys list.

Copy the private key on the client that you will use to connect to the server and then remove it from the server
```
# scp ~/.ssh/id_rsa root@clientmachine:root/.ssh/
# rm -rf ~/.ssh/id_rsa
```

On Linux and Unix client, use the private key to login to the server
```
# ssh -i ~/.ssh/id_rsa root@servermachine
```

On Windows client, use the puttygen tool to make the key in a suitable format and use the Putty application to login to the server. Please, note that each user that want to login must have his own key pair.


text:
## Volume Manager, a real example
Let to see, step-by-step, a real example of Volume Manager on a CentOS 7 setup.
On the host caldera01 there is an additional disk ``/dev/sdb`` we want to use for store a mysql database.

```
[root@caldera01 ~]# df -Th
Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/mapper/os-root    xfs        50G  2.1G   48G   5% /
devtmpfs               devtmpfs  3.8G     0  3.8G   0% /dev
tmpfs                  tmpfs     3.8G     0  3.8G   0% /dev/shm
tmpfs                  tmpfs     3.8G  8.6M  3.8G   1% /run
tmpfs                  tmpfs     3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/mapper/os-data    xfs       175G  256M  175G   1% /data
/dev/sda1              xfs       497M  190M  308M  39% /boot

[root@caldera01 ~]# lsblk
NAME                         MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                            8:0    0 232.9G  0 disk
├─sda1                         8:1    0   500M  0 part /boot
└─sda2                         8:2    0 232.4G  0 part
  ├─os-swap                  253:0    0   7.8G  0 lvm  [SWAP]
  ├─os-root                  253:1    0    50G  0 lvm  /
  └─os-data                  253:2    0 174.6G  0 lvm  /data
sdb                            8:16   0 232.9G  0 disk
└─sdb1                         8:17   0 232.9G  0 part

[root@caldera01 ~]# fdisk /dev/sdb
Disk /dev/sdb: 250.1 GB, 250059350016 bytes, 488397168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0003b431

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   488397167   244197560   8e  Linux LVM
```

Create a LVM layout
```
[root@caldera01 ~]# pvcreate /dev/sdb1
[root@caldera01 ~]# vgcreate vgdb /dev/sdb1
[root@caldera01 ~]# lvcreate -l 100%FREE -n lvol1 vgdb
[root@caldera01 ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  os   lvm2 a--  232.39g    0
  /dev/sdb1  vgdb lvm2 a--  232.88g    0
[root@caldera01 ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  os     1   3   0 wz--n- 232.39g    0
  vgdb   1   1   0 wz--n- 232.88g    0
[root@caldera01 ~]# lvs
  LV    VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data  os   -wi-ao---- 174.63g
  root  os   -wi-ao----  50.00g
  swap  os   -wi-ao----   7.77g
  lvol1 vgdb -wi-ao---- 232.88g
```

Make an ``ext3`` file system on the logical volume and mount the partition under a ``/db`` directory
```
[root@caldera01 ~]# mkfs -t ext3 /dev/vgdb/lvol1
[root@caldera01 ~]# mkdir /db
[root@caldera01 ~]# mount /dev/sdb1 /db
```

Install a mysql database on the new filesystem
```
[root@caldera01 ~]# sudo rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
[root@caldera01 ~]# yum install -y mysql-server
[root@caldera01 ~]#  vi /etc/my.cnf
[mysqld]
...
datadir=/db/mysql
...
[root@caldera01 ~]# systemctl start mysqld
[root@caldera01 ~]# systemctl status mysqld
[root@caldera01 ~]# systemctl enable mysqld
```

From GitHub, install a sample database with an integrated test suite, used to test your applications and database servers
```
[root@caldera01 ~]# yum install -y git
[root@caldera01 ~]# git clone https://github.com/datacharmer/test_db.git
[root@caldera01 ~]# cd /db/test_db
```

Load the test database
```
[root@caldera01 ~]# mysql < employees.sql
```

If you want to install with two large partitioned tables, alternatively, run
```
[root@caldera01 ~]# mysql < employees_partitioned.sql
```

Run the test suite
```
[root@caldera01 ~]# mysql  -t < test_employees_md5.sql
+----------------------+
| INFO                 |
+----------------------+
| TESTING INSTALLATION |
+----------------------+
+--------------+------------------+----------------------------------+
| table_name   | expected_records | expected_crc                     |
+--------------+------------------+----------------------------------+
| employees    |           300024 | 4ec56ab5ba37218d187cf6ab09ce1aa1 |
| departments  |                9 | d1af5e170d2d1591d776d5638d71fc5f |
| dept_manager |               24 | 8720e2f0853ac9096b689c14664f847e |
| dept_emp     |           331603 | ccf6fe516f990bdaa49713fc478701b7 |
| titles       |           443308 | bfa016c472df68e70a03facafa1bc0a8 |
| salaries     |          2844047 | fd220654e95aea1b169624ffe3fca934 |
+--------------+------------------+----------------------------------+
+--------------+------------------+----------------------------------+
| table_name   | found_records    | found_crc                        |
+--------------+------------------+----------------------------------+
| employees    |           300024 | 4ec56ab5ba37218d187cf6ab09ce1aa1 |
| departments  |                9 | d1af5e170d2d1591d776d5638d71fc5f |
| dept_manager |               24 | 8720e2f0853ac9096b689c14664f847e |
| dept_emp     |           331603 | ccf6fe516f990bdaa49713fc478701b7 |
| titles       |           443308 | bfa016c472df68e70a03facafa1bc0a8 |
| salaries     |          2844047 | fd220654e95aea1b169624ffe3fca934 |
+--------------+------------------+----------------------------------+
+--------------+---------------+-----------+
| table_name   | records_match | crc_match |
+--------------+---------------+-----------+
| employees    | OK            | ok        |
| departments  | OK            | ok        |
| dept_manager | OK            | ok        |
| dept_emp     | OK            | ok        |
| titles       | OK            | ok        |
| salaries     | OK            | ok        |
+--------------+---------------+-----------+
```

We want to add 2 additional LUNs via iSCSI protocol to the LVM layout. The iSCSI makes the system able to see the external LUNs as additional disks, called ``/dev/sdc`` and ``/dev/sdd``
Each additional disk is of 232.9G

```
[root@caldera01 ~]# lsblk
NAME                         MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                            8:0    0 232.9G  0 disk
├─sda1                         8:1    0   500M  0 part /boot
└─sda2                         8:2    0 232.4G  0 part
  ├─os-swap                  253:0    0   7.8G  0 lvm  [SWAP]
  ├─os-root                  253:1    0    50G  0 lvm  /
  └─os-data                  253:2    0 174.6G  0 lvm  /data
sdb                            8:16   0 232.9G  0 disk
└─sdb1                         8:17   0 232.9G  0 part
  └─vgdb-lvol1               253:4    0 232.9G  0 lvm  /db
sdc                            8:32   0 232.9G  0 disk
sdd                            8:48   0 232.9G  0 disk
```

Now let's to extend the LV ``lvol1`` by using the 2 additional LUNs
```
[root@caldera01 ~]# pvcreate -f /dev/sdc
  Wiping iso9660 signature on /dev/sdc.
  Wiping dos signature on /dev/sdc.
  Physical volume "/dev/sdc" successfully created

[root@caldera01 ~]# pvcreate -f /dev/sdd
  Wiping dos signature on /dev/sdd.
  Physical volume "/dev/sdd" successfully created

[root@caldera01 ~]# pvscan
  PV /dev/sda2   VG os     lvm2 [232.39 GiB / 0    free]
  PV /dev/sdb1   VG vgdb   lvm2 [232.88 GiB / 0    free]
  PV /dev/sdc              lvm2 [232.89 GiB]
  PV /dev/sdd              lvm2 [232.89 GiB]

[root@caldera01 ~]# vgextend vgdb /dev/sdc /dev/sdd
  Volume group "vgdb" successfully extended

[root@caldera01 ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "os" using metadata type lvm2
  Found volume group "vgdb" using metadata type lvm2

[root@caldera01 ~]# vgdisplay vgdb
  --- Volume group ---
  VG Name               vgdb
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               698.64 GiB
  PE Size               4.00 MiB
  Total PE              178852
  Alloc PE / Size       59618 / 232.88 GiB
  Free  PE / Size       119234 / 465.76 GiB
  VG UUID               O557zn-CcSI-1Ec4-LryC-uqi8-B42R-pYTHKU

[root@caldera01 ~]# lvscan
  ACTIVE            '/dev/os/root' [50.00 GiB] inherit
  ACTIVE            '/dev/os/data' [174.63 GiB] inherit
  ACTIVE            '/dev/os/swap' [7.77 GiB] inherit
  ACTIVE            '/dev/vgdb/lvol1' [232.88 GiB] inherit

[root@caldera01 ~]# lvextend -L +400G /dev/vgdb/lvol1
  Size of logical volume vgdb/lvol1 changed from 232.88 GiB (59618 extents) to 632.88 GiB (162018 extents).
  Logical volume lvol1 successfully resized
[root@caldera01 ~]#

[root@caldera01 ~]# lvscan
  ACTIVE            '/dev/os/root' [50.00 GiB] inherit
  ACTIVE            '/dev/os/data' [174.63 GiB] inherit
  ACTIVE            '/dev/os/swap' [7.77 GiB] inherit
  ACTIVE            '/dev/vgdb/lvol1' [632.88 GiB] inherit
```

Now the LV successfully increased its size by 632G but the file system still is 232G
```
[root@caldera01 ~]# df -Th
Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/mapper/os-root    xfs        50G  2.1G   48G   5% /
devtmpfs               devtmpfs  3.8G     0  3.8G   0% /dev
tmpfs                  tmpfs     3.8G     0  3.8G   0% /dev/shm
tmpfs                  tmpfs     3.8G  8.6M  3.8G   1% /run
tmpfs                  tmpfs     3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/mapper/os-data    xfs       175G  256M  175G   1% /data
/dev/sda1              xfs       497M  190M  308M  39% /boot
/dev/mapper/vgdb-lvol1 ext3      230G  642M  217G   1% /db
```

Extend the file system without umount it
```
[root@caldera01 ~]# resize2fs -p /dev/vgdb/lvol1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vgdb/lvol1 is mounted on /db; on-line resizing required
old_desc_blocks = 15, new_desc_blocks = 40
The filesystem on /dev/vgdb/lvol1 is now 165906432 blocks long.
```

Let's check our data file
```
[root@caldera01 ~]# cd /db/test_db
[root@caldera01 ~]# mysql  -t < test_employees_md5.sql

+----------------------+
| INFO                 |
+----------------------+
| TESTING INSTALLATION |
+----------------------+
+--------------+------------------+----------------------------------+
| table_name   | expected_records | expected_crc                     |
+--------------+------------------+----------------------------------+
| employees    |           300024 | 4ec56ab5ba37218d187cf6ab09ce1aa1 |
| departments  |                9 | d1af5e170d2d1591d776d5638d71fc5f |
| dept_manager |               24 | 8720e2f0853ac9096b689c14664f847e |
| dept_emp     |           331603 | ccf6fe516f990bdaa49713fc478701b7 |
| titles       |           443308 | bfa016c472df68e70a03facafa1bc0a8 |
| salaries     |          2844047 | fd220654e95aea1b169624ffe3fca934 |
+--------------+------------------+----------------------------------+
+--------------+------------------+----------------------------------+
| table_name   | found_records    | found_crc                        |
+--------------+------------------+----------------------------------+
| employees    |           300024 | 4ec56ab5ba37218d187cf6ab09ce1aa1 |
| departments  |                9 | d1af5e170d2d1591d776d5638d71fc5f |
| dept_manager |               24 | 8720e2f0853ac9096b689c14664f847e |
| dept_emp     |           331603 | ccf6fe516f990bdaa49713fc478701b7 |
| titles       |           443308 | bfa016c472df68e70a03facafa1bc0a8 |
| salaries     |          2844047 | fd220654e95aea1b169624ffe3fca934 |
+--------------+------------------+----------------------------------+
+--------------+---------------+-----------+
| table_name   | records_match | crc_match |
+--------------+---------------+-----------+
| employees    | OK            | ok        |
| departments  | OK            | ok        |
| dept_manager | OK            | ok        |
| dept_emp     | OK            | ok        |
| titles       | OK            | ok        |
| salaries     | OK            | ok        |
+--------------+---------------+-----------+
```

Our data is safe! Now we want to resize the logical volume to use only 1 LUN instead of 2 LUNs as we made before. Firt of all, shutdown any service or application that is using the logical volume we want to resize. In our case, we have to stop the MySQL database running on the volume
```
[root@caldera01 test_db]# systemctl stop mysql
[root@caldera01 test_db]# systemctl status mysql
mysqld.service - MySQL Community Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: inactive (dead) since Thu 2015-10-22 12:13:28 CEST; 2min 12s ago
 Main PID: 15388 (code=exited, status=0/SUCCESS)
```

Make a backup of ALL yuor data present on the volume
```
[root@caldera01 test_db]# tar cvf data_backup.tar *
[root@caldera01 test_db]# gzip data_backup.tar
[root@caldera01 test_db]# mv data_backup.tar /data_backup_folder
```

Umount the LV
```
[root@caldera01 ~]# umount /db
```

Then check for the file-system error (Must pass in every 5 steps of file-system check if not there might be some issue with your file-system)

```
[root@caldera01 /]# e2fsck -ff /dev/vgdb/lvol1
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vgdb/lvol1: 403/41484288 files (10.9% non-contiguous), 2803302/165906432 blocks
```

Now let's to resize the file system. Since we want to remove the last LUN, we have to calculate the final size of the file system by summing its original size ``/dev/sdb1`` to the size of the first added LUN ``/dev/sdc``. We have ``/dev/sdb1`` Total PE 59618 + ``/dev/sdc`` Total PE 59617 = Total PE 119235. Total size, expressed in Megabyte are 4 x 119235 = 476940 MB

```
[root@caldera01 ~]# pvdisplay
--- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vgdb
  PV Size               232.88 GiB / not usable 2.18 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              59618
  Free PE               0
  Allocated PE          59618
  PV UUID               mymhL6-Ta6m-cx3V-LjKM-Zo2T-K7Xj-MI1jb3

  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               vgdb
  PV Size               232.89 GiB / not usable 7.18 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              59617
  Free PE               0
  Allocated PE          59617
  PV UUID               0dIQWO-jjaa-zgl3-1o8E-8kGR-4xjc-BxPmwO

  --- Physical volume ---
  PV Name               /dev/sdd
  VG Name               vgdb
  PV Size               232.89 GiB / not usable 7.18 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              59617
  Free PE               16834
  Allocated PE          42783
  PV UUID               Df0M7R-6KDB-TUeZ-l1Gw-2r5J-ci4c-MsyNw4

[root@caldera01 ~]# fsadm -e -y resize /dev/vgdb/lvol1 476940M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/vgdb-lvol1 to 122096640 (4k) blocks.
The filesystem on /dev/mapper/vgdb-lvol1 is now 122096640 blocks long.
```

Reduce the Logical Volume and remove the last LUN from the Volume Group
```
[root@caldera01 ~]# lvreduce -L 476940M /dev/vgdb/lvol1
  WARNING: Reducing active logical volume to 465.76 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol1? [y/n]: y
  Size of logical volume vgdb/lvol1 changed from 632.88 GiB (162018 extents) to 465.76 GiB (119235 extents).
  Logical volume lvol1 successfully resized

[root@caldera01 ~]# vgreduce vgdb /dev/sdd
  Removed "/dev/sdd" from volume group "vgdb"
[root@caldera01 ~]#
```

Remove the physical volume
```
[root@caldera01 ~]# pvremove /dev/sdd
  Labels on physical volume "/dev/sdd" successfully wiped
[root@caldera01 ~]#
```

And check again the file system and mount back it
```
[root@caldera01 ~]# e2fsck -ff /dev/vgdb/lvol1
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vgdb/lvol1: 403/30531584 files (10.9% non-contiguous), 2116084/122096640 blocks
[root@caldera01 ~]#

[root@caldera01 ~]# mount /dev/vgdb/lvol1 /db
[root@caldera01 ~]# df -Th /db
Filesystem             Type  Size  Used Avail Use% Mounted on
/dev/mapper/vgdb-lvol1 ext3  459G  653M  439G   1% /db
[root@caldera01 ~]#
```

Start the mysql service and check for data integrity
```
[root@caldera01 ~]# systemctl start mysqld
[root@caldera01 ~]# cd /db/test_db/
[root@caldera01 test_db]# mysql  -t < test_employees_md5.sql
+----------------------+
| INFO                 |
+----------------------+
| TESTING INSTALLATION |
+----------------------+
+--------------+------------------+----------------------------------+
| table_name   | expected_records | expected_crc                     |
+--------------+------------------+----------------------------------+
| employees    |           300024 | 4ec56ab5ba37218d187cf6ab09ce1aa1 |
| departments  |                9 | d1af5e170d2d1591d776d5638d71fc5f |
| dept_manager |               24 | 8720e2f0853ac9096b689c14664f847e |
| dept_emp     |           331603 | ccf6fe516f990bdaa49713fc478701b7 |
| titles       |           443308 | bfa016c472df68e70a03facafa1bc0a8 |
| salaries     |          2844047 | fd220654e95aea1b169624ffe3fca934 |
+--------------+------------------+----------------------------------+
+--------------+------------------+----------------------------------+
| table_name   | found_records    | found_crc                        |
+--------------+------------------+----------------------------------+
| employees    |           300024 | 4ec56ab5ba37218d187cf6ab09ce1aa1 |
| departments  |                9 | d1af5e170d2d1591d776d5638d71fc5f |
| dept_manager |               24 | 8720e2f0853ac9096b689c14664f847e |
| dept_emp     |           331603 | ccf6fe516f990bdaa49713fc478701b7 |
| titles       |           443308 | bfa016c472df68e70a03facafa1bc0a8 |
| salaries     |          2844047 | fd220654e95aea1b169624ffe3fca934 |
+--------------+------------------+----------------------------------+
+--------------+---------------+-----------+
| table_name   | records_match | crc_match |
+--------------+---------------+-----------+
| employees    | OK            | ok        |
| departments  | OK            | ok        |
| dept_manager | OK            | ok        |
| dept_emp     | OK            | ok        |
| titles       | OK            | ok        |
| salaries     | OK            | ok        |
+--------------+---------------+-----------+
+------------------+
| computation_time |
+------------------+
| 00:00:14         |
+------------------+
+---------+--------+
| summary | result |
+---------+--------+
| CRC     | OK     |
| count   | OK     |
+---------+--------+

```


text:
## Modify the Command Line Prompt
The PS1 variable is the character string that is displayed as the prompt on the command line. Most distributions set PS1 to a known default value, for example, the user and the hostname as in:
```
[root@caldera01 ~]#
```
This could prove useful if you are working in multiple roles and want to be always reminded of who you are and what machine you are on.

```
[root@caldera01 ~]# echo $PS1
[\u@\h \W]\$
[root@caldera01 ~]#
[root@caldera01 ~]# export PS1='[\u@\h \W(customt)]# '
[root@caldera01 ~(customt)]#
[root@caldera01 ~(customt)]# echo $PS1
[\u@\h \W(customt)]#
[root@caldera01 ~(customt)]#
[root@caldera01 ~(customt)]# export PS1='[\u@\h \W]# '
[root@caldera01 ~]#
```