Ubuntu Server 12.04 LTS Home Media Server

  • Tutorial
Hello, Habr! During the discussion of one article about the "ideal" home network, a dispute arose, which is better, a hardware NAS or a mini-computer with Linux distribution. The author suggested using a hardware NAS, because it is supposedly easier to administer, does not require Linux knowledge, and indeed the NAS is quiet. But at the same time, to watch a video that he does not support on a DLNA TV, he suggested switching on a laptop with transcoding DLNA. This, to put it mildly, surprised me, because this should not be in an ideal network. Therefore, I want to present my vision of one of the key components of the home network - a centralized data warehouse, and it will be based on a mini-PC with the Ubuntu Server OS.

What do we need?

First of all, NAS requires, of course, reliable data storage and convenient access to it. First of all, RAID is required for reliability, because losing the entire home media archive due to a failed hard drive is at least stupid. To access the data you need to configure FTP and Samba access. Of course, everyone has their own needs, so if you use MacOS or Linux, then you will probably need other protocols (NFS, AFP), but I will describe the configuration in the way I did for myself.
To access media data from smart TVs, we need a DLNA server. And for the convenience of downloading, we need a torrent client. Well, it’s advisable to administer all this through the web interface.

Why not a hardware NAS?

It would seem that manufacturers have long taken care of the users, and have long produced ready-made boxes specifically for home use. But they have flaws:
1) They are expensive. You are unlikely to find cheaper than 20,000 rubles. NAS with the ability to connect 4 hard drives with an Atom processor. Those that are inexpensive, they usually use a weak processor, which is already not enough for the same torrent while downloading two data streams (watching a movie on DLNA and copying, for example, photos). But I managed to assemble a full-fledged mini-PC based on a mini-ITX motherboard with Atom and 4 GB of memory for only 6,000 rubles!
2) They are limited. That is, it provides only those functions that were laid down by the manufacturer. To expand its capabilities, “dancing with a tambourine” is usually required, since the core in the firmware can be greatly truncated. Using Ubuntu, you are practically unlimited in anything - a huge repository of all kinds of software will allow you to do anything from your server, up to raising virtual machines.

Why not FreeNAS or OpenFiler?

You ask. Firstly, see point 2 of the drawbacks of hardware NAS, that is, increasing the functionality of these distributions is very problematic, while Ubuntu has a huge repository of already configured software. Secondly, these are huge system requirements, in particular, FreeNAS 8 requires a minimum of 2 GB of RAM, and new versions of OpenFiler are no longer released for the x86 architecture. In addition, FreeNAS is somehow not smoothly developing - version 0.7, which has a torrent client and a DLNA server, is outdated a long time ago, in the eighth commercial version I still couldn’t configure DLNA, and it’s somehow difficult with the proposed ZFS file system, in case of system failure, how do you order data recovery? Complicated.

Why is the Server 12.04 LTS distribution selected?

LTS (Long Term Support) is a distribution with a long period of support and the release of updates. Since we need a server that, if possible, once configured, could work quietly in the future for years, it is better to choose this version of the distribution kit.
The server version is obviously chosen, because ideally we absolutely do not need to waste resources on the graphical shell. Although if you are just getting acquainted with linux, or have already worked with the desktop version of ubuntu, then in principle you can choose the usual version of the distribution, this is not important.

Let's start

The installation is completely transparent, so I will not describe it in detail. I’ll just dwell on the breakdown of hard drives. I took a budget motherboard without support for hardware RAID, and in my practice the hardware RAID built into the motherboard often does not show its best side, so we will organize the so-called "software" RAID. Two brand new hard drives will be used for data storage. I didn’t have any extra storage media, so I will split the disk into two sections, one of which will be the system one, and the second for the data. Both partitions on two hard disks will be combined into RAID 1 (I perform all operations for convenience on a virtual machine, so do not pay attention to the small size of the partitions).

First, create a partition table on the first disk and break it into two parts. We mark them as the "partition for RAID", although this is not necessary. The second disk is similarly divided. Then we select the item "Configuring software RAID". We say “Create an MD device”, select the first partitions on two disks. Similarly with sections for data. By the way, RAID can be dynamically changed and expanded, so if you have only one hard drive so far, but plan to purchase a second one, feel free to configure it and you can easily pick it up after the purchase. After creating the RAID, mark them for use. We select the ext4 file system and assign mount points: the system partition as the root (/), and the data partition to an arbitrary location (I prefer to mount it in the / mnt folder).

Further, the system will notify if we want to boot the system if the RAID array fails. I advise you to answer “no”, because if the hard drive fails, you won’t even notice it - the system will continue to work with one drive, but if the second drive fails, then you will have to bring them to the data recovery company. I won’t create a swap partition, because firstly it can be made a file, and secondly I personally do not need it - on my mini-PC 4 GB is installed, while the memory usage has never exceeded more than 10% (400 MB ), but in the normal state even less (right now only 130 MB are used). Although if you plan to raise virtual machines, you may need it, so after installation I will describe how to create a swap file, but now we respond negatively to the proposal to create a swap partition.

After a short process of copying files, the system will start updating data from the repositories, and after asking how updates will be installed. Since our system administration is minimized, we choose automatic updating. The system will then ask which packages should be installed immediately. I chose OpenSSH (we need a remote command line), LAMP (needed for the web interface), Print server (in this article I will not describe how to connect the printer), and of course Samba file server for access from windows machines.

Well, in the final stage, the system will ask for a password for MySQL and a request to install GRUB. Reboot - the system is installed! We log in to see what IP address DHCP assigned to us (this can also be done using the ifconfig command), in my case the address was issued. Everything, you can turn off the monitor and remove the system unit in a convenient place, then we will work with it through SSH. I use PUTTY for this.


1) swap file
First of all, I will describe how to configure the swap file, if you really need it, everything is done in just a few lines of commands.
Create a file filled with zeros:
> sudo dd if=/dev/zero of=/swap bs=1M count=2048

Prepare it for use as a swap:
> sudo mkswap /swap

Add our created file to the fstab file for use as a swap file:
> sudo nano /etc/fstab
/swap none swap sw 0 0

> sudo shutdown -r now

2) software update
Immediately update all packages, this is done by two commands:
> sudo apt-get update
> sudo apt-get upgrade

3) Web interface
There is a webim package for managing the system through the web interface, but unfortunately it is not in the repository, so download the prepared package manually:
> wget http://prdownloads.sourceforge.net/webadmin/webmin_1.580_all.deb

Installing webim will require some dependent packages, in my case it’s such a list, you may need to include something else.
> sudo apt-get install libnet-ssleay-perl libauthen-pam-perl libio-pty-perl apt-show-versions

Well, actually the installation:
> sudo dpkg --install webmin_1.580_all.deb

All you can go to the web interface:

4) Configure ftp access
For ftp, I use pure-ftpd (although you can choose to your taste - proftpd and vsftpd)
Create a public folder:
> sudo mkdir /mnt/data/public

Install pure-ftpd from the repository:
> sudo apt-get install pure-ftpd

In principle, you can already log in under the system account, but this is not entirely good for everyday use. Let's create a virtual account with access only to the public folder:
> sudo pure-pw useradd public -u local -g nogroup -d /mnt/data/public

Update the database:
> sudo pure-pw mkdb

Let's enable the use of virtual users:
> sudo ln -s /etc/pure-ftpd/conf/PureDB /etc/pure-ftpd/auth/50pure

Restart the service:
> sudo service pure-ftpd restart

5) Samba
We will configure access to the server from windows-based machines; moreover, my family personally is large and I need to share rights between several users. And for convenient editing of rights to folders directly from Windows (through the "security" tab in the properties), we will use ACLs.
We don’t have a domain, so we’ll have to create users the same as on windows machines:
> sudo useradd -d /home/PaulZi -s /bin/true -g users PaulZi

Set the password, the same as on windows:
> sudo passwd PaulZi

Add the created user to Samba:
> sudo smbpasswd -a PaulZi

To manage extended rights, you can install utilities (optional):
> sudo apt-get install acl
> sudo apt-get install attr

In order for samba to work with ACLs, a file system with POSIX ACL support is required, ext4 is quite suitable, but by default it is mounted without this support. To enable this feature, add the “acl” option to the / etc / fstab file. But more than that, support for inheritance of rights is implemented in Windows, so that this is also implemented in linux, samba needs to store additional data somewhere. To do this, enable the extended file attributes, the option "user_xattr". At the same time, we prohibit the execution of files on the entire data section using the “noexec” option (for security):
> sudo nano /etc/fstab
/dev/md0 /mnt/data       ext4    defaults,noexec,acl,user_xattr        0       2

> sudo shutdown -r now

We edit the samba settings (for brevity, I only give changes and additions):
> sudo nano /etc/samba/smb.conf
   workgroup = Home
   netbios name = Server
   security = user
   # add settings
   admin users = PaulZi			# действие этих пользователей будут производиться от root 
   map acl inherit		= yes	# включаем наследование acl
   store dos attributes	= yes	# включаем хранение dos атрибутов
   # отключаем хранения windows атрибутов:
   map archive		= no
   map system		= no
   map hidden		= no
   map readonly		= no
# public share
   comment = Public
   path = /mnt/data/public
   browseable = yes				# шара видна
   read only = no				# включаем возможность записи
   guest ok = yes				# разрешаем гостевой доступ
   inherit permissions = yes	# включаем наследование прав
   inherit acls = yes			# включаем наследование windows-прав
   inherit owner = yes			# включаем наследование владельца
   hide unreadable = yes		# прятать файлы недоступные для чтения

Restart the service:
> sudo service smbd restart

6) DLNA / UPnP - server
As the DLNA server, I chose minidlna. I chose it for one simple reason that it does not pull a bunch of unnecessary dependencies, like MediaTomb and Serviio (they pull Java or graphic libraries). However, if you need transcoding, I advise you to install one of them, instead of minidlna.
Installation from the repository:
> sudo apt-get install minidlna

> sudo nano /etc/minidlna.conf

> sudo service minidlna restart

7) torrent
Well, the last service covered in this article is a torrent client. I use Transmission as a well-established web-based client.
> sudo apt-get install transmission-daemon

We stop the service, otherwise all changes will be erased after the process is completed:
> sudo service transmission-daemon stop

> sudo nano /etc/transmission-daemon/settings.json
"download-dir": "/mnt/data/public/torrents"
"rpc-password": "local"
"rpc-username": "local"
"rpc-whitelist-enabled": false

Here we change four settings - we set the download path, username and password for the web interface, and also disable the white list of access to the interface - allow for everyone. We specify the password in clear form, after the subsequent launch it will be encrypted.
We start the service:
> sudo service transmission-daemon start

We go into the web interface, make sure that everything is fine:


As a result, we got a fully-fledged home server. Of course, the article indicates only the basic settings of the services, and most likely you will need to configure something for yourself. Yes, and you may need some additional services, but as you can see from the article, all this is done quite simply, without special “dancing with a tambourine”, you just need to google - there are a lot of information on setting up services in Ubuntu.

Also popular now: