Skip to main content

Ubuntu

Installing relational databases with MySQL in Ubuntu server

In this recipe, we will learn how to install and configure the MySQL database on an Ubuntu server.

Getting ready

You will need access to a root account or an account with sudo privileges.

Make sure that the MySQL default port 3306 is available and not blocked by any firewall.

How to do it…

Follow these steps to install the relational database MySQL:

To install the MySQL server, use the following command:

$ sudo apt-get update

$ sudo apt-get install mysql-server-5.7

The installation process will download the necessary packages and then prompt you to enter a password for the MySQL root account. Choose a strong password:

Once the installation process is complete, you can check the server status with the following command. It should return an output similar to the following:

$ sudo service mysql status

mysql.service - MySQL Community Server

Loaded: loaded (/lib/systemd/system/mysql.service

Active: active (running) since Tue 2016-05-10 05:

Next, create a copy of the original configuration file:

$ cd /etc/mysql/mysql.conf.d

$ sudo cp mysqld.cnf mysqld.cnf.bkp

Set MySQL to listen for a connection from network hosts. Open the configuration file /etc/mysql/mysql.conf.d/mysqld.cnf and change bind-address under the [mysqld] section to your server’s IP address:

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

bind-address = 10.0.2.6

For MySQL 5.5 and 5.6, the configuration file can be found at /etc/mysql/my.cnf

Optionally, you can change the default port used by the MySQL server. Find the [mysqld] section in the configuration file and change the value of the port variable as follows:

port = 30356

Make sure that the selected port is available and open under firewall.

Save the changes to the configuration file and restart the MySQL server:

$ sudo service mysql restart

Now open a connection to the server using the MySQL client. Enter the password when prompted:

$ mysql -u root -p

To get a list of available commands, type \h:

mysql> \h

How it works…

MySQL is a default database server available in Ubuntu. If you are installing the Ubuntu server, you can choose MySQL to be installed by default as part of the LAMP stack. In this recipe, we have installed the latest production release of MySQL (5.7) from the Ubuntu package repository. Ubuntu 16.04 contains MySQL 5.7, whereas Ubuntu 14.04 defaults to MySQL version 5.5.

If you prefer to use an older version on Ubuntu 16, then use following command:

$ sudo add-apt-repository ‘deb http://archive.ubuntu.com/ubuntu trusty universe’

$ sudo apt-get update

$ sudo apt-get install mysql-server-5.6

After installation, configure the MySQL server to listen for connections from external hosts. Make sure that you open your database installation to trusted networks such as your private network. Making it available on the Internet will open your database to attackers.

There’s more…

Securing MySQL installation

MySQL provides a simple script to configure basic settings related to security. Execute this script before using your server in production:

$ mysql_secure_installation

This command will start a basic security check, starting with changing the root password. If you have not set a strong password for the root account, you can do it now. Other settings include disabling remote access to the root account and removing anonymous users and unused databases.

MySQL is popularly used with PHP. You can easily install PHP drivers for MySQL with the following command:

$ sudo apt-get install php7.0-mysql

See also

The Ubuntu server guide mysql page at https://help.ubuntu.com/14.04/serverguide/mysql.html

Monitoring network in Ubuntu

When we are talking about a server, its network is the most important resource. Especially in the cloud network, when it is the only communication channel to access the server and connect with other servers in the network. The network comes under an Input/Output device category. Networks are generally slow in performance and are an unreliable communication channel. You may lose some data while in transit, data may be exposed to external entities, or a malicious guy can update original data before it reaches you.

The Ubuntu server, as well as Linux in general, provides tons of utilities to ease network monitoring and administration. This recipe covers some inbuilt tools to monitor network traffic and its performance. We will also look at a few additional tools that are worth a space on your system.

Getting ready

Some commands may need sudo access.

You may need to install a few tools.

How to do it…

We will start with a commonly used command, that is, ifconfig. We mostly use this command to read the network configuration details such as the IP address. When called without any parameters, ifconfig displays details of all active network interfaces as follows:

These details contain the IP address assigned to each network interface, its hardware address, the maximum packet size (MTU) and basic statistics of received (RX) and transmitted (TX) packets, and the count of errors or dropped packets, and so on.

If you are only interested in quick network statistics, use ifconfig with flag -s, as follows:

If you do not see a specific network interface listed in the active list, then query for all available interfaces with the -a option to ifconfig.

Another commonly used command is ping. It sends ICMP requests to a specified host and waits for the reply. If you query for a host name, ping will get its IP address from DNS. This also gives you confirmation that the DNS is working properly. Ping also gives you the latency of your network interface. Check for the time values in the output of the ping command:

Next, comes netstat. It is mainly used to check network connections and routing tables on the system. The commonly used syntax is as follows:

$ sudo netstat -plutn

The preceding command should list all TCP (-t) / UDP (-u) connections, plus any ports that are actively listening (-l) for connection. The flag, -p, queries the program name responsible for a specified connection. Note that flag -p requires sudo privileges. Also check flag -a to get all listening as well as non-listening sockets, or query the routing table information with flag -r as follows:

$ netstat -r

You can also get protocol level network statistics using the netstat command as follows:

$ netstat -s

One more utility very similar to netstat is ss. It displays detailed TCP socket information. Use ss without any parameters to get a list of all the sockets with a state established.

Another command, lsof, gives you a list of all open files. It includes the files used for network connections or sockets. Use with flag -i to list all network files, as follows:

$ sudo lsof -i

To filter output, use flag -s with protocol and state as filter options:

$ sudo lsof -iTCP -sTCP:LISTEN

Next, we will look at a well-known tool, tcpdump. It collects network traffic and displays it to a standard output or dump in a file system. You can dump the content of the packets for any network interface. When no interface is specified, tcpdump defaults to the first configured interface, which is generally eth0. Use it as follows to get a description of packets exchanged over eth0:

$ sudo tcpdump -i eth0

To log raw packets to a file, use flag -w. These logged packets can later be read with the -r flag. The following command will log 100 packets from the interface eth0 to the file tcpdump.log:

$ sudo tcpdump -i eth0 -w tcpdump.log -c 100

$ tcpdump -r tcpdump.log

Next, to get statistics of network traffic, use the command sar. We have already used sar to get CPU and memory statistics. To simply extract all network statistics, use sar as follows:

$ sar -n ALL 1 5

This will log all network statistics at an interval of 1 second. You can also enable periodic logging in the file /etc/default/sysstat. For network specific usage of sar, check flag -n in the man pages.

There is one more utility named collectl which is similar to sar. In the same way as sar, you will need to separately install this command as well:

$ sudo apt-get install collectl

Once installed, use collectl with the -s flag and value sn to get statistics about the network. Using it without any parameters gives you statistics for the CPU, disk, and network:

How it works…

This recipe covers various network monitoring commands including the commonly used ifconfig and ping, netstat, tcpdump, and collectl.

If you have been working with Linux systems for a while, you should have already used the basic network commands, ifconfig and ping. Ifconfig is commonly used to read network configuration and get details of network interfaces. Apart from its basic use, ifconfig can also be used to configure the network interface. See article 2Networking, to get more details on network configuration. With netstat, you can get a list of all network sockets and their respective processes using those socket connections. With various parameters, you can easily separate active or listening connections and even separate connections with the protocol being used by the socket. Additionally, netstat provides details of routing table information and network statistics as well. The command ss provides similar details to netstat and adds some more information. You can use ss to get memory usages of socket (-m) and the process using that particular socket (-p). It also provides various filtering options to get the desired output. Check the manual pages of ss with the command, man ss.

There's more…

Following are some more commands that can be useful when monitoring network data. With a limit on page count, it is not possible to cover them all, so I am simply listing the relevant commands:

nethogs: Monitors per process bandwidth utilization

ntop / iftop: Top for network monitoring

iptraf: Monitors network interface activity

vnstat: Network traffic monitoring with logging

ethtool: Queries and configures network interfaces

nicstat / ifstat / nstat: Network interface statistics

tracepath: Traces a network route to destination host

Setting Nginx as reverse proxy in Ubuntu

Apache and Nginx are two popular open source web servers. Both are very powerful, but at the same time have their own disadvantages as well. Apache is not good at handling high load environments with multiple concurrent requests and Nginx does not have inbuilt support for dynamic content processing. Many administrators overcome these problems by using both Apache and Nginx together. Nginx handles all incoming requests and only passes requests for dynamic content to Apache. Additionally, Nginx can provide a catching option which enables the server to respond to a request with results from a similar previous request. This helps to reduce the overall response time and minimize the load sent to Apache.

In this recipe, we will learn how to set up a web server configured with a reverse proxy. We will use Nginx as a reverse proxy, which will serve all static content and pass the requests for dynamic content to Apache.

Getting ready

You will need access to a root account or an account with sudo privileges.

I assume that Apache is installed and running with a virtual host, example.com.

How to do it…

Follow these steps to set Nginx as a reverse proxy:

Install Nginx with the following command:

$ sudo apt-get update

$ sudo apt-get install nginx

Create a new site configuration under /etc/nginx/sites-available and add the following content to it:

$ sudo nano /etc/nginx/sites-available/reverse_proxy

server {

listen 80;

root /var/www/example.com;

index index.php index.html index.htm;

server_name example.com;

location / {

try_files $uri $uri/ /index.php;

}

location ~ \.php$ {

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_set_header Host $host;

proxy_pass http://127.0.0.1:8080;

}

location ~* \.(js|css|jpg|jpeg|png|svg|html|htm)$ {

expires 30d;

}

location ~ /\.ht {

deny all;

}

}

Enable this new configuration by creating a symbolic link under sites-enabled:

$ sudo ln -s /etc/nginx/sites-available/reverse_proxy \

/etc/nginx/sites-enabled/reverse_proxy

Optionally, disable the default site by removing the symbolic link from sites-enabled:

$ sudo rm /etc/nginx/sites-enabled/default

Next, we need to change the Apache settings to listen on port 8080. This will leave port 80 to be used by Nginx:

$ sudo nano /etc/apache2/ports.conf

listen 127.0.0.1:8080

Also change NameVirtualHost, if you are using it:

NameVirtualHost 127.0.0.1:8080

Change the virtual hosts settings to listen on port 8080:

$ sudo nano /etc/apache2/sites-available/example.com

ServerName example.com

ServerAdmin webmaster@example.com

DocumentRoot /var/www/example.com/public_html

Save the changes and restart Apache for the changes to take effect:

$ sudo service apache2 restart

Now, restart Nginx:

$ sudo service nginx restart

Check for open ports with the following command:

$ sudo netstat -pltn

Open your browser and point it to the IP address of your server. It should load the page configured under the Apache virtual host, example.com.

How it works…

With the proxy_pass parameter, we have simply asked Nginx to pass all requests for PHP scripts to Apache on 127.0.0.1 on port 8080. Then, we set Apache to listen on the loopback IP and port 8080, which will receive requests forwarded by Nginx and process them with an internal PHP processor. All non-PHP content will still be served by Nginx from the /var/www directory. The try_files $uri $uri/ /index.php; option sets Nginx to search for the file with a specified name and then look for the folder; lastly, if both file and folder are not found, send the request to index.php, which will then be processed by Apache.

Other options used with proxy pass ensures that Apache and PHP scripts receive the actual hostname and IP of the client and not of the Nginx server. You can use an additional module named libapache2-mod-rpaf on Apache. This module provides an option to set a proxy IP address and rename the parameters sent by the proxy server. You can install the module with the following command:

$ sudo apt-get install libapache2-mod-rpaf

The configuration file for this module is available at /etc/apache2/mods-available/rpaf.conf.

You can find various other proxy options and their respective explanations in the Nginx documentation at http://nginx.org/en/docs/http/ngx_http_proxy_module.html

Finally, with Nginx set as a frontend, Apache will not have to interact directly with HTTP clients. You may want to disable some of the Apache modules that will not be used in this setup:

$ sudo a2dismod deflate cgi negotiation autoindex

As always, do not forget to reload Apache after any changes.

There's more…

Nginx can be set to cache the response received from the backend server and thereby minimize repeated requests on backend servers, as well as the response time. Nginx can cache the content in local files and serve new requests from the cache. The cache can be invalidated or even disabled based on the request received. To enable caching, add the following settings to the Nginx site configuration:

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=backend- cache:8m max_size=50m;

proxy_cache_key "$scheme$request_method$host$request_uri$args";

server {

## add other settings heres

location / {

proxy_pass 127.0.0.1:8080;

proxy_cache backend-cache;

proxy_cache_bypass $http_cache_control;

add_header X-Proxy-Cache $upstream_cache_status;

proxy_cache_valid 200 302 10m;

proxy_cache_valid 404 1m;

}

}

You may need to create the proxy path directory /data/nginx/cache and set the appropriate file permissions. Set the directory ownership to www-data and restrict permissions to 700. You can use any location for cache data and not necessarily /data/nginx/cache.

This configuration sets the cache validity of 10 minutes, which is quite a lengthy period. This will work if you have static content that rarely changes. Instead, if you are serving dynamic content that is frequently updated, then you can take advantage of microcaching by setting the cache validity to a very small period of a few seconds. Add the following parameters to further improve your caching configuration for microcaching:

proxy_cache_lock on: Queues additional requests while the cache is being updated

proxy_cache_use_stale updating: Uses stale data while the cache is being updated

HAProxy and Varnish

HAProxy and Varnish are other popular options for the reverse proxy and the caching proxy, respectively. Both of them can offer improved performance when compared with Nginx. HAProxy can also be used as a Layer 4 and Layer 7 load balancer. We covered HAProxy in article 2Networking, in the Load Balancing with HAProxy recipe.

See also

Nginx admin guide on reverse proxies at https://www.nginx.com/resources/admin-guide/reverse-proxy/

Understanding Nginx proxying, load balancing, and caching at https://www.digitalocean.com/community/tutorials/understanding-nginx-http-proxying-load-balancing-buffering-and-caching

Nginx proxy module documentation at http://nginx.org/en/docs/http/ngx_http_proxy_module.html

Receiving updates with Git pull

In the last recipe, we learned how to set up a remote repository and send local changes to a remote using the git push command. The story is not complete yet. When the repository is shared by multiple people, everyone will push their own changes. The central repository will keep on updating. When you want to synchronize or push your changes to the central repo, you need to download any updates made by other users and then push your modifications on top of that. A git pull command will be used to pull down any updates to the remote central repository to your local repository.

This recipe covers the git pull command. We will use this command to resolve a rejected push, but it is generally used simply to update your local copy.

Getting ready

You will need one central remote repository; it may be hosted on GitHub or anywhere else.

Secondly, you will need two local copies of the central repo. Use the git clone command to create a local replica of the remote repository. These two copies are used for demonstration purposes; in the real world, you will already have multiple copies with different users of your repository:

$ git clone https://github.com/sawantuday/mynewproject.git local_copy_1

$ git clone https://github.com/sawantuday/mynewproject.git local_copy_2

Now enter local_copy_1, create a new file with random content and then commit and push the changes back to the remote repository:

$ cd local_copy_1

$ echo "// Modifications by user 1" >> index.php

$ git add .

$ git commit -m "Index.php created with comments"

$ git push origin master

Your push command should complete without any errors or warnings.

Next, enter local_copy_2 and create a new file with random contents:

$ cd local_copy_2

$ echo "\\ Modifications by user 2" >> main.php

How to do it…

Suppose you are user two working on a copy, local_copy_2. You cloned the repository and started working with the code base. In the meantime, user one completed his work and pushed his changes back to the central repo. Now, after you have completed your work, you are ready to send updates to the remote repo:

Commit your modifications to the local repository:

$ git add .

$ git commit -m "main.php created with comments"

Try to push your commit to the central repo:

$ git push origin master

This time, your push should fail, saying someone else had already updated the remote repository. Git will give you details of a rejected push, as follows:

Now you need to pull remote changes; first, with git pull, merge any potential conflicts, and then try to push again:

$ git pull origin master

You will be asked to enter a merge message in nano or a similar editor. Simply accept the pre-filled message and save the file by pressing Ctrl + O, then press Enter to save, and then Ctrl + X to exit.

Now try to push again. This time it should complete successfully:

$ git push origin master

How it works…

As we saw in the previous example, git pull is used to pull the remote modifications to the local repository. It is a good idea to use git pull before starting your work on the local copy. This way you can be sure that you have all remote updates in your local repository, thus reducing the chances of a rejected push.

The git pull command can be used any time, even to simply update your local codebase with the remote copy. I have used it in a commit and push flow just to demonstrate the rejected push and merge scenario.

The example demonstrates the simple automated merge. It may happen that both user one and user two are working on the same file and incidentally modify the same part of the code. Git will report a Merge conflict, as follows:

Now, in this case, Git may not be able to automatically merge both updates. It will combine both updates in single file and mark them in a special format, as follows:

In this case, you need to decide what to keep and what to remove. Once you are done with solving conflicts, remove the special tags added by Git and commit the conflicting file. After that, you can push your updates along with the new commit for merging.

See also

You can read more by following these links:

Git pull: https://git-scm.com/docs/git-pull

Git merge: https://git-scm.com/docs/git-merge

Git fetch: https://git-scm.com/docs/git-fetch

How to Install DNS in Ubuntu server

DNS, also known as name server, is a service on the Internet that provides mapping between IP addresses and domain names and vice versa. DNS maintains a database of names and related IP addresses. When an application queries with a domain name, DNS responds with a mapped IP address. Applications can also ask for a domain name by providing an IP address.

DNS is quite a big topic, and an entire article can be written just on the DNS setup. This recipe assumes some basic understanding of the working of the DNS protocol. We will cover the installation of BIND, installation of DNS server application, configuration of BIND as a caching DNS, and setup of Primary Master and Secondary Master. We will also cover some best practices to secure your DNS server.

Getting ready

In this recipe, I will be using four servers. You can create virtual machines if you want to simply test the setup:

ns1: Name server one/Primary Master

ns2: Name server two/Secondary Master

host1: Host system one

host2: Host system two, optional

All servers should be configured in a private network. I have used the 10.0.2.0/24 network

We need root privileges on all servers

How to do it…

Install BIND and set up a caching name server through the following steps:

On ns1, install BIND and dnsutils with the following command:

$ sudo apt-get update

$ sudo apt-get install bind9 dnsutils

Open /etc/bind/named.conf.optoins, enable the forwarders section, and add your preferred DNS servers:

forwarders {

8.8.8.8;

8.8.4.4;

};

Now restart BIND to apply a new configuration:

$ sudo service bind9 restart

Check whether the BIND server is up and running:

$ dig -x 127.0.0.1

You should get an output similar to the following code:

;; Query time: 1 msec

;; SERVER: 10.0.2.53#53(10.0.2.53)

Use dig to external domain and check the query time:

Dig the same domain again and cross check the query time. It should be less than the first query:

Set up Primary Master through the following steps:

On the ns1 server, edit /etc/bind/named.conf.options and add the acl block above the options block:

acl "local" {

10.0.2.0/24; # local network

};

Add the following lines under the options block:

recursion yes;

allow-recursion { local; };

listen-on { 10.0.2.53; }; # ns1 IP address

allow-transfer { none; };

Open the /etc/bind/named.conf.local file to add forward and reverse zones:

$ sudo nano /etc/bind/named.conf.local

Add the forward zone:

zone "example.com" {

type master;

file "/etc/bind/zones/db.example.com";

};

Add the reverse zone:

zone "2.0.10.in-addr.arpa" {

type master;

file "/etc/bind/zones/db.10";

};

Create the zones directory under /etc/bind/:

$ sudo mkdir /etc/bind/zones

Create the forward zone file using the existing zone file, db.local, as a template:

$ cd /etc/bind/

$ sudo cp db.local zones/db.example.com

The default file should look similar to the following image:

Edit the SOA entry and replace localhost with FQDN of your server.

Increment the serial number (you can use the current date time as the serial number, 201507071100)

Remove entries for localhost, 127.0.0.1 and ::1.

Add new records:

; name server - NS records

@ IN NS ns.exmple.com

; name server A records

ns IN A 10.0.2.53

; local - A records

host1 IN A 10.0.2.58

Save the changes and exit the nano editor. The final file should look similar to the following image:

Now create the reverse zone file using /etc/bind/db.127 as a template:

$ sudo cp db.127 zones/db.10

The default file should look similar to the following screenshot:

Change the SOA record and increment the serial number.

Remove NS and PTR records for localhost.

Add NS, PTR, and host records:

; NS records

@ IN NS ns.example.com

; PTR records

53 IN PTR ns.example.com

; host records

58 IN PTR host1.example.com

Save the changes. The final file should look similar to the following image:

Check the configuration files for syntax errors. It should end with no output:

$ sudo named-checkconf

Check zone files for syntax errors:

$ sudo named-checkzone example.com /etc/bind/zones/db.example.com

22.

If there are no errors, you should see an output similar to the following:

zone example.com/IN: loaded serial 3

OK

23.

Check the reverse zone file, zones/db.10:

$ sudo named-checkzone example.com /etc/bind/zones/db.10

If there are no errors, you should see output similar to the following:

zone example.com/IN: loaded serial 3

OK

Now restart the DNS server bind:

$ sudo service bind9 restart

Log in to host2 and configure it to use ns.example.com as a DNS server. Add ns.example.com to /etc/resolve.conf on host2.

Test forward lookup with the nslookup command:

$ nslookup host1.example.com

You should see an output similar to following:

$ nslookup host1.example.com

Server: 10.0.2.53

Address: 10.0.2.53#53

Name: host1.example.com

Address: 10.0.2.58

Now test the reverse lookup:

$ nslookup 10.0.2.58

It should output something similar to the following:

$ nslookup 10.0.2.58

Server: 10.0.2.53

Address: 10.0.2.53#53

58.2.0.10.in-addr.arpa name = host1.example.com

Set up Secondary Master through the following steps:

First, allow zone transfer on Primary Master by setting the allow-transfer option in /etc/bind/named.conf.local:

zone "example.com" {

type master;

file "/etc/bind/zones/db.example.com";

allow-transfer { 10.0.2.54; };

};

zone "2.0.10.in-addr.arpa" {

type master;

file "/etc/bind/zones/db.10";

allow-transfer { 10.0.2.54; };

};

A syntax check will throw errors if you miss semicolons.

Restart BIND9 on Primary Master:

$ sudo service bind9 restart

On Secondary Master (ns2), install the BIND package.

Edit /etc/bind/named.conf.local to add zone declarations as follows:

zone "example.com" {

type slave;

file "db.example.com";

masters { 10.0.2.53; };

};

zone "2.0.10.in-addr.arpa" {

type slave;

file "db.10";

masters { 10.0.2.53; };

};

Save the changes made to named.conf.local.

Restart the BIND server on Secondary Master:

$ sudo service bind9 restart

This will initiate the transfer of all zones configured on Primary Master. You can check the logs on Secondary Master at /var/log/syslog to verify the zone transfer.

A zone is transferred only if the serial number under the SOA section

on Primary Master is greater than that of Secondary Master.

Make sure that you increment the serial number after

every change to the zone file.

How it works…

In the first section, we have installed the BIND server and enabled a simple caching DNS server. A caching server helps to reduce bandwidth and latency in name resolution. The server will try to resolve queries locally from the cache. If the entry is not available in the cache, the query will be forwarded to external DNS servers and the result will be cached.

In the second and third sections, we have set Primary Master and Secondary Master respectively. Primary Master is the first DNS server. Secondary Master will be used as an alternate server in case the Primary server becomes unavailable.

Under Primary Master, we have declared a forward zone and reverse zone for the example.com domain. The forward zone is declared with domain name as the identifier and contains the type and filename for the database file. On Primary Master, we have set type to master. The reverse zone is declared with similar attributes and uses part of an IP address as an identifier. As we are using a 24-bit network address (10.0.2.0/24), we have included the first three octets of the IP address in reverse order (2.0.10) for the reverse zone name.

Lastly, we have created zone files by using existing files as templates. Zone files are the actual database that contains records of the IP address mapped to FQDN and vice versa. It contains SOA record, A records, and NS records. An SOA record defines the domain for this zone; A records and AAAA records are used to map the hostname to the IP address.

When the DNS server receives a query for the example.com domain, it checks for zone files for that domain. After finding the zone file, the host part from the query will be used to find the actual IP address to be returned as a result for query. Similarly, when a query with an IP address is received, the DNS server will look for a reverse zone file matching with the queried IP address.

See also

Checkout the DNS configuration guide in the Ubuntu server guide at https://help.ubuntu.com/lts/serverguide/dns-configuration.html

For an introduction to DNS concepts, check out this tutorial by the DigitalOcean community at https://www.digitalocean.com/community/tutorials/an-introduction-to-dns-terminology-components-and-concepts

Get manual pages for BIND9 at http://www.bind9.net/manuals

Find manual pages for named with the following command:

$ man named

Uploading contents and creating catalogs in Ubuntu

So, we have installed the Ampache streaming server. Now, we will learn how to upload our audio/video content and create our first catalog.

Getting ready

You will need audio and video files to be uploaded on your server and enough space to save all this content. I will be using podcasts from Ubuntu podcasts in the MP3 format.

Upload all content to your Ampache server and note the directory path. I will be using the podcasts directory under home for my user.

Open the Ampache server homepage and log in with admin credentials.

How to do it…

Ampache provides the admin page, where you can perform all administrative tasks, such as catalogue management, user management, and other configurations. We will create a new catalogue from the admin panel and then point it to already uploaded content:

From your Ampache homepage, click on the admin icon in the upper-left corner of the screen. This should list all administrative tools:

Now, click on the Add a Catalog link. This should load the Add a Catalog page:

Enter the catalog name. Use a name that describes your content. I will use Ubuntu podcasts.

Set the Catalog Type to local, as we will be loading content from your local filesystem.

Enter the path for your MP3 (or video) files, /home/ubuntu/podcasts in my case.

Click on the Add Catalog button. This will create a new catalog and import all content to it. The process will check for meta tags and try to collect more information about the content. It will take some time to process all the files and add details to the Ampache database:

Finally, click Continue to complete catalog creation and go to the catalog list:

Once catalog creation is complete, you can go to the homepage by clicking the home icon (first) in the upper-left of the screen and then clicking on the song title link. This should list all the files available under your catalog directory:

From this song list, you can play songs/podcasts, add or remove ratings, add them to playlists, and more:

How it works…

Catalog creation simply reads the content from the upload directory and adds the respective details to the MySQL database. The process tries to gather more details about content using information collected from meta tags and track titles or file names. This information is then used to group the content by artist and album. Note that Ampache is not tagging software where you upload random content and receive a well-organized media library. For Ampache to work well, you need to have properly tagged and well-organized content.

Managing Ubuntu Users and Groups

In this article, we will cover the following recipes:

  • Creating a user account
  • Creating user accounts in batch mode
  • Creating a group
  • Adding group members
  • Deleting a user account
  • Managing file permissions
  • Getting root privileges with sudo
  • Setting resource limits with limits.conf
  • Setting up public key authentication
  • Securing user accounts

Introduction for Working with Containers in Ubuntu

Containers are quite an old technology and existed in the form of chroot and FreeBSD Jails. Most of us have already used containers in some form or other. The rise of Docker gave containers the required adoption and popularity. Ubuntu has also released a new tool named LXD with Ubuntu 15.04.

A container is a lightweight virtual environment that contains a process or set of processes. You might already have used containers with chroot. Just as with containers, we create an isolated virtual environment to group and isolate a set of processes. The processes running inside the container are isolated from the base operating system environment, as well as other containers running on the same host. Such processes cannot access or modify anything outside the container. A recent development in the Linux kernel to support namespaces and cgroups has enabled containers to provide better isolation and resource-management capabilities.

One of the reasons for the widespread adoption of containers is the difference between containers and hypervisor-based virtualization, and the inefficiencies associated with virtual machines. A VM requires its own kernel, whereas containers share the kernel with the host, resulting in a fast and lightweight isolated environment. Sharing the kernel removes much of the overhead of VMs and improves resource utilization, as processes communicate with a single shared kernel. You can think of containers as OS-level virtualization.

With containers, the entire application can be started within milliseconds, compared to virtual minutes. Additionally, the image size becomes much smaller, resulting in easier and faster cloud deployments. The shared operating system results in smaller footprints, and saved resources can be used to run additional containers on the same host. It is normal to run hundreds of containers on your laptop.

However, containerization also has its own shortcomings. First, you cannot run cross-platform containers. That is, containers must use the same kernel as the host. You cannot run Windows containers on a Linux host, and vice versa. Second, the isolation and security is not as strong as hypervisor-based virtualization. Containers are largely divided into two categories: OS containers and application containers. As the name suggests, application containers are designed to host a single service or application. Docker is an application container. You can still run multiple processes in Docker, but it is designed to host a single process.

OS containers, on the other hand, can be compared to virtual machines. They provide user space isolation. You can install and run multiple applications and run multiple processes inside OS containers. LXC on Linux and Jails on BSD are examples of OS containers.

In this article, we will take a look at LXC, an OS container, and Docker, an application container. In the first part of the article, we will learn how to install LXC and deploy a containerized virtual machine. In subsequent recipes, we will work with Docker and related technologies. We will learn to create and deploy a container with Docker.

Adding users to Samba server on Ubuntu

In the previous recipe, we installed the Samba server and created a public share accessible to everyone. In this recipe, we will learn how to add authentication to the Samba server and password protect shared directories.

Getting ready

You will need access to a root account or an account with sudo privileges.

Make sure that the Samba server is installed and running.

How to do it…

Follow these steps to add users to the Samba server:

Create a new user account. You can use any existing account or add a new Samba only account with the following command. Change smbuser to your desired username:

$ sudo useradd -d /home/smbuser -s /sbin/nologin smbuser

Now, we need to allocate a Samba password to this new user. First, enter your sudo password, followed by the new password for your Samba account, and then verify the password:

$ sudo smbpasswd -a smbuser

Create a shared directory for this user and change its ownership:

$ sudo chown smbuser:smbuser /var/samba/share/smbuser

Next, edit the Samba configuration to add the preceding share:

[Private]

path = /var/samba/shares/smbuser

browsable = yes

writable = yes

valid users = smbuser

Save the changes to the configuration file and reload the Samba server:

$ sudo service smbd reload

Now, check in Windows Explorer. You should see the new shared directory. On trying to open that directory, you will be asked for a Samba username and password:

How it works…

Samba allows various different types of configuration for shared resources. In the previous recipe, we learned how to set up a public share, and in this recipe we have created a private share for a single user. We have created a new user with the nologin permission. This will allow smbuser to access only the Samba shared directory and nothing else. You can also use existing user accounts on the Ubuntu server.

After adding a user, we set a password to be used with the Samba server. Samba maintains a database of passwords separately from Ubuntu passwords. You can enable or disable Samba users with the following commands:

Enable a Samba user:

$ sudo smbpasswd -e username

Disable a Samba user:

$ sudo smbpasswd -d username

Remove a Samba user:

$ sudo smbpasswd -x username

To enable multiple users to access a shared resource, you can specify the list of users under the valid users line, as follows:

valid users = userone, usertwo, userthree

Similarly, you can limit write permissions to a set of users, as follows:

write list = userone, usertwo

Samba also supports the sharing of users, home directories. This will enable users to create shares for all existing Ubuntu users with a single block of configuration. Add the following lines to the Samba configuration to enable the sharing of home directories:

[homes]

browseable = No

valid users = %S

After this configuration, user's home directories will be available at //server-name/user-name. You will be required to provide a username and password to access these shares. Home directories are by default shared as read only. To enable write permissions, add the following line to the preceding block:

writable = yes

Note that on Windows, you will not be able to access multiple home directories from a single Windows system. Windows does not allow multiple user authentications to a single host.

Alternatively, to share a directory with a group of users, you can use group sharing. Use the following line to share a directory with a group of users:

path=/var/samba/shares/group-share

valid users = @groupname

Then, set group ownership on the directory, group-share:

$ sudo chgrp groupname /var/samba/shares/group-share

There are some other directives such as create mask, directory mask, force user, and force group. These directives can be used to determine the permissions and ownership of the newly created files under Samba share.

After any changes to the Samba configuration file, use testparm to check the configuration for any syntax errors:

$ testparm

It should show the Loaded services file OK message, as listed in following screenshot:

There's more…

With the release of version 4, Samba can be set as a domain controller. Check the official documentation for more details at the following link:

https://wiki.samba.org/index.php/Setup_a_Samba_Active_Directory_Domain_Controller

You can also configure the Samba server to authenticate against the LDAP server. LDAP installation and configuration is covered in article 14Centralized Auth Service. For more details on Samba and LDAP integration, check out the Ubuntu server guide at https://help.ubuntu.com/lts/serverguide/samba-ldap.html .

See also

Linux home server Samba guide at http://www.brennan.id.au/18- Samba.html#useraccounts

Introduction on Handling Databases in Ubuntu server

In this article, we will learn how to set up database servers. A database is the backbone of any application, enabling an application to efficiently store and retrieve crucial data to and from persistent storage. We will learn how to install and set up relational databases with MySQL and NoSQL databases with MongoDB.

MySQL is a popular open source database server used by various large scale applications. It is a mature database system that can be scaled to support large volumes of data. MySQL is a relational database and stores data in the form of rows and columns organized in tables. It provides various storage engines, such as MyISAM, InnoDB, and in-memory storage. MariaDB is a fork of a MySQL project and can be used as a drop-in replacement for MySQL. It was started by the developers of MySQL after Oracle took over Sun Microsystems, the owner of the MySQL project. MariaDB is guaranteed to be open source and offers faster security releases and advanced features. It provides additional storage engines, including XtraDB by Percona and Cassandra for the NoSQL backend. PostgreSQL is another well-known name in relational database systems.

NoSQL, on the other hand, is a non-relational database system. It is designed for distributed large-scale data storage requirements. For some types of data, it is not efficient to store it in the tabular form offered by relational database systems, for example, data in the form of a document. NoSQL databases are used for these types of data. Some emerging NoSQL categories are document storage, key value store, BigTable, and the graph database.

In this article, we will start by installing MySQL, followed by storing and manipulating data in MySQL. We will also cover user management and access control. After an introduction to relational databases, we will cover some advanced topics on scaling and high availability. We will learn how to set up the web administration tool, PHPMyAdmin, but the focus will be on working with MySQL through command line access. In later recipes, we will also cover the document storage server, MongoDB.