Introduction
After showing that one of the known pitfalls of Linux isn’t as bad as most expected, it’s about time to get things into gear and show one of the strengths of Linux. It’s a known fact that Linux (or any other flavour of ‘Nix) is a great OS to run on that low power, the-day-before-yesterday’s-generation hardware. That’s because the strength of Linux lies in its Command Line Interface (CLI). While scary to those who are used to point and click, CLI offers a no-nonsense, straight to the point and low-overhead powerhouse.
Many accustomed to graphical interfaces find the CLI old and obsolete. But they just don’t grasp the strength of the interface. Since most things in Linux are done through the manipulation of (small) files, there often is no need for some eye candy. With a relative small number of commands you can totally change the functioning of a Linux system. CLI isn’t hard to learn and Linux offers a great tool for learning it, the “man” pages.
But enough of the chatting, Brett asked me to write a guide on how to install a home file server, with a GUI (sigh). In addition to that, he wanted it to run headless, thus requiring a remote administration. Also, a web-based P2P solution was offered to run on the box, so that you can shut down your power hungry workstation, and leave the downloading for the economic box. And to finish things up, I’ll briefly touch the area of scripting. Scripting is the edge that Linux has to offer over Windows – Mac users will know of it a bit if you are familiar with Automator. Most tasks can be automated through scripts – a (lesser) known saying in the Linux world goes: “If you need to do something more than 3 times, you should write a script for it."
But enough of the chatting, lets get this party started…
Scared of command lines and Linux? Don’t be. We’ll take you through step by step to
build one of the most useful boxes you could ever get your hands on – from spare parts.
Hardware
As already touched in the introduction, Linux needs next to no hardware to run on. I know plenty of people who run a Linux gateway on a old 486. That is an impressive clock speed of 16MHz! Unfortunately we aren’t building a gateway right now. Given the requirements of this article, we need something a lot more beefy. Certainly things like an X- and web-server require a fair bit more power. Certainly the X server.
That’s why I chose Xubuntu. Ubuntu is considered as one of the most newbiefriendly distributions out there, but it uses the heavier Gnome window managers, while Xubuntu uses XFCE. Minimum requirements for Gnome are a 500MHz CPU, opposed to the 133MHz needed for XFCE. However, since our box will be multi role, the minimum specification of 133MHz might be a little low. Don’t get me wrong, all will work, but not as smoothly. Anything 750MHz or up will do just fine.
RAM wise, Xubuntu once again is the limiting factor. The install requires 192MB to complete. Afterwards, the system isn’t as RAM hungry, so 192MB should be plenty. In the area of disk space, we are building a file server so the more the better. The OS doesn’t require a lot, about 2.5GB. Disk space is considered to be cheap, so nothing problematic on this front either.
Basically, anything halfway decent from two years ago will do. Maybe you could use that hardware you had to throw out to run Vista? In case you have some lesser hardware lying around, don’t fear. By letting go of the GUI you can lower the requirements quite a bit. Also, all Linux flavours are different, yet so similar. To get the requirements down you can just use the “Ubuntu Server” install. Then you won’t get the GUI, but none the less all will function. If you want even more control, try another distribution, like Debian or Gentoo if you are feeling lucky.
Personally, I’ve ‘assembled’ a virtual PC, thanks to the guys over at VirtualBox. The system has 256MB of RAM and 10GB of disk space at its disposal. For those of you working on a real machine, here’s a tip – the first thing I do when I build a server is walk around in the BIOS. I then disable all unneeded things that I discover on my journey.
If it isn’t needed, turn it off. That way it can’t break.
Xubuntu
Before we can start to install something, we first need an installation medium. The easiest way of installing a system is through the use of a bootable CD. So, let’s get ourselves some ISO images, shall we?
Head on over to the guys at Xubuntu and download a Fiesty Fawn (7.04) Desktop CD from one of the mirrors. If you have some bandwith to spare, be kind – use the torrent download, and share the love. The file isn’t small despite the meager resources of the system needed to run it, so anything helps.
After you have downloaded the ISO file, burn the image to a CD and boot from it. Wait for a moment or two, and you’ll be greeted with the XFCE interface. Don’t you love Live CDs? The disc itself is a fully working OS, so feel free to play around a bit. Of course, there will be a better time to play soon enough, so you don’t need to dally just yet.
Once you’re ready, it’s time to head on to the installation.
Installing Xubuntu is actually quite easy, just a 7 step process. However, I’ll help you through the install anyway. Let’s start by double-clicking the nice “Install” icon on the desktop. Just in case you didn’t know, that’s what it’s for. First, we select our language. Conveniently, it’s already at English. So let’s move on to the next screen…
Next, we set our localisation. Either click on the map, or select it from the list. Feel free to choose something close if your city isn’t on the map. And on to the next screen, which asks us about our keyboard layout. The intelligent system knows from the localisation what keyboard layout is normally used. But, since Linux is about choice, you can choose another one if required. Let’s not stand still too long and move on to the tough questions…
Disk partitioning… if you’re building a system from the command line, this can be tricky. But don’t worry, the Xubuntu installer handles this nicely. I used an empty hard disk and let the installer handle all the partitioning. if there already is a partitioning table on the disk, you’ll have an extra option. But since we won’t be dual booting or anyting, just use the entire disk and proceed to the next screen. Here we can migrate documents and settings from the disk, but since it’s empty, there’s nothing to migrate…
Now, lets get personal. The information you have to input in this screen is actually quite important – especially the “log in name” which serves as your username, your password (try using a strong password), and the name of the computer (hostname). Once all are filled in correctly (it may be wise to write these down for a little while), proceed to the final step, where it gives a summary. Re-read your choices and brace yourself. After clicking on the “Install” button the actual install starts.
Now, sit back and relax… Everything is automated. It takes some time to install a system, so go ahead, grab a cup of tea. Or even better, scout around. You are in a fully functional environment remember? If you are a first time Linux user this is the ideal moment to check things out. When the installation completes, you’ll get this fancy box asking you whether you want to stay inside the LiveCD, or rather boot to your own installation. So let’s restart now and start updating and installing.
Give your system the time to reboot (make sure you boot from the harddisk, not the CD). After your system has booted, you’ll be greeted by GDM, the login manager Ubuntu uses. Input your username and password and you’ll once again see the already familiar interface.
Now, let’s introduce ‘Synaptic’, the Ubuntu package manager. For those who haven’t got a clue what a package manager is, it’s really easy. A package is a piece of software, and a package manager installs, removes and updates those packages. But it does more than that – it also installs dependencies when required. A dependency occurs when a package is to be installed, but it requires a second package to function.
Now that you know what packages and their dependencies are, stop worrying about it. Synaptic solves all of our problems with no thought required. So, using the GUI, open “Synaptic package manager” located under “Applications” -> “System”. You will be asked to input your password again. This is because you’ll be messing around with critical things on your system. Call it a sort of protection – like Windows Vista UAC, only about one-millionth as annoying and ten times as effective.
First, let’s clean up some unwanted stuff and create some more free space for all our data. Click search and search for OpenOffice. While this is a great desktop utility, it isn’t required on our server. Look for “openoffice.org-common”, right click on it, and select mark for complete removal.
You’ll see that removing the common package implies that all the packages depending on it will be removed too. Nice feature isn’t it? Of course, not all OpenOffice packages are removed unless you select them (click on the header of the first column to sort on installed packages). Also, remove all “abiword”, “gaim”, “gimp” and “thunderbird” packages. Beware though, if you try to uninstall a package that gives “xubuntu-desktop” as a dependency, do NOT go through with it. The Ubuntu developers listed a couple of packages as required. If you remove them, you will remove your entire GUI. If you are satisfied, click “Apply”
Now that the unwanted packages are removed, let’s do some updates. See the button “Mark all upgrades”? This will search for all packages which should be updated, and does it. So click “Mark” to select them, and “Apply” again to update. You’ll see Synaptic doing its job. When it’s done, just exit Synaptic just like you’d do with any Windows application.
Now, only one final thing needs to be done. Since our server will be tucked away somewhere, possibly headless, a screen saver only sucks up resources. Therefore, we will disable it. The screen saver settings are located under “Applications” -> “Settings” -> “Screen saver Settings”. Completely disable the screen saver.
See, Linux isn’t hard to learn! Well, Ubuntu surely isn’t…
File Sharing (Samba & proFTPd)
The goal of this guide was setting up a file server. So after the OS has been installed, it is time to get that part going. Unfortunately, no OS is all that keen on file sharing natively out of the box, and Linux is no different. That’s not because it can’t – more just because the goal of Linux is to have what you want, and some people don’t need file sharing. So, it’s a separate package.
One of the best know packages in Linux is Samba. Samba is, among other things, a server and client that allows file sharing among workstations, regardless of their OS. Think of it as something along the lines of “Windows Filesharing”, only much more versatile. If you get into the depths that Samba has to offer, you’ll see that the sharing of files is just the start of what it has to offer. But enough chit-chat, lets get this party started.
First things first, we have to get Samba, or at least the rest of it. So fire up Synaptic, search for Samba and mark it for installation. You’ll see that a lot of Samba is included in the base Xubuntu install and that we only need the Samba package (server).
The install will progress quite fast and you’ll have samba installed in no-time. We’ll configure Samba in a terminal. So open one up, “Applications” -> “Accesories” -> “Terminal”.
Welcome to the wonderfull world of CLI. Don’t fear the dark terminal, instead you should embrace it. It wields more power then you can imagine. But before we start manipulating files, I’d like to explain the directory structure for a moment. Linux has a well defined tree structure, with the userdata residing under /home, the config files under /etc, temporary files under /tmp and so on. Since most config files (all global) are under /etc, that is the main workfield now. Also, Linux is known for their great user rights system. An unpriviledged user cannot edit the config files. That’s why we have to become root, the super user.
Because of the (in Linux terms) weird way of handling rights in Ubuntu, we make use of the command sudo su to become root. Just enter your user password if required. Then, we change our working directory to those of Samba, by typing cd /etc/samba which simply means Change Directory to /etc/samba. Easy isn’t it? Now we will edit the main config file of the samba server, named “smb.conf”. For this, I use nano, a great and easy terminal file editor. Just type nano smb.conf and you are ready to start editing. If you would like something more graphical, you could always install “gedit” (through Synaptic) and replace nano with gedit.
Replace all the contents of the file with the following. Make sure you replace "Name" and "Server Name" with the correct data from your XUbuntu installation!
[global]
panic action = /usr/share/samba/panic-action %d
workgroup = "Name"
netbios name = "Server name"
invalid users = root
security = user
wins support = no
log file = /var/log/samba.log
log level = 3
max log size = 1000
syslog = 1
encrypt passwords = true
passdb backend = smbpasswd
socket options = TCP_NODELAY
dns proxy = no
passwd program = /usr/bin/passwd %u
passwd chat =*Enter\snew\sUNIX\spassword:* %n\n *Retype\snew\sUNIX\spassword:* %n\n .
obey pam restrictions = yes
pam password change = no
null passwords = no
#Share Definitions
[homes]
comment = Home Directories
browseable = yes
writable = yes
security mask = 0700
create mask = 0700
Save and exit in nano is done through the keyconbination Ctrl + X , y and enter.
Now, we restart the samba server by typing /etc/init.d/samba restart. You’ll see the server stopping and restarting. Now we are sure that new config is loaded. One minor difficulty in Samba is that it uses a separate password back-end. This is great for security, but in this case adds an other step. We have to add all the users that use samba to that back-end. Don’t worry, it’s an easy step. Type smbpasswd -a Username and enter the password for that user.
Now, lets check if our hard work did it’s job. From another workstation (a Windows XP one, for instance), open
\\(whatever the IP of the server is)\homes
You should be prompted for the username and password, which you just freshly created moments ago. Enter them in, and you’ll be greeted with the contents of your home directory.
FTP Server
Next, we will be adding an FTP server to our build. This is completely optional and therefore not required if you don’t need it. It just adds extra functionality to our server. Many people don’t see a need for FTP, but there are always the odd times where you wish you had the ability to zip a file over to yourself, or to pull something off of your home computer.
To install this service, I’ll only use CLI just to show how easy it is. This is also the way the non-GUI users will install services to their system. Once you’ve built a few of these, you too may wish to skimp on the extra space and resources that any GUI takes up, at least on your server. So, let’s fire up that terminal once again.
Once in the terminal, we have to become root user again, so sudo su. Now it’s time for us to install the ftp server. I have chosen Proftpd because of its reliability, security and ease of use. Installing things in the CLI really is as easy as doing it through the GUI. Just type apt-get install proftpd. Apt-get is the command line equivalent of Synaptic – that’s all the GUI is really doing!
You’ll get lots of information, which you should read, and then you’ll be asked if you want to continue. We do, so just hit enter. After a few moments the installer will ask us whether we want the server to run as a standalone server, or through the inetd service. Choose standalone, because of security and manageability reasons.
As with Samba, we need to configure the server. Fire up nano again by issuing nano /etc/proftpd/proftpd.conf. Then just replace the contents with the following:
ServerName "FTP Server"
Serverident on "FTP"
ServerType standalone
DeferWelcome off
TimesGMT off
MultilineRFC2228 on
ShowSymlinks on
TimeoutNoTransfer 600
TimeoutStalled 600
TimeoutIdle 1200
DisplayLogin welcome.msg
DisplayFirstChdir .message
ListOptions "-l"
DenyFilter \*.*/
AllowForeignAddress on
AllowRetrieveRestart on
Port 21
SocketBindTight on
PassivePorts 11000 20000
MaxInstances 30
User nobody
Group nogroup
Umask 022 022
AllowOverwrite on
AllowForeignAddress on
AllowRetrieveRestart on
AllowStoreRestart on
UseReverseDNS off
IdentLookups off
DefaultRoot ~
ExtendedLog /var/log/proftpd.all ALL
DelayEngine off
User ftp
Group nogroup
UserAlias anonymous ftp
DirFakeUser on ftp
DirFakeGroup on ftp
RequireValidShell off
MaxClients 10
DisplayLogin welcome.msg
DisplayFirstChdir .message
AccessGrantMsg "Anonymous access granted for user %u connecting."
MaxClientsPerHost 1
#DenyAll
TransferRate RETR 50
DenyAll
Once you’re done, restart the server by typing /etc/init.d/proftpd restart. Proftpd uses the standard password backend, so no extra configuration is required.
Remote administration (x11VNC && SSH)
Now, we have ourselves a fileserver – but it isn’t practical to keep a keyboard, mouse and monitor connected to it. To solve this problem, we have 2 main options. For the CLI people we have SSH, which offers us a secure way of connecting to the server. The SSH server is a base for me on every Linux server, and can be useful under a variety of circumstances.
For the GUI people, we have VNC. VNC is a well know graphical protocol which works on Windows too, although with subtle differences compared to the Linux counterpart. Don’t worry though, both are compatible. VNC works by sending a picture of your screen to the client computer, so it’s a lot slower than SSH. Of course, it is also a lot more like "being there" if you’re CLI-challenged (or just lazy — Ed.).
The best part is, you can have the best of both worlds. Having both a SSH and a VNC server installed is no problem at all, and the combination allows you a tremendous flexibility for a secure connection. And let’s be honest…isn’t that why you’re building this thing?
So let’s not waste any more time – open up Synaptic and get both services installed. First, the SSH server. It’s configured quite securely from the start, so no extra steps are needed. All you need to do is select the “openssh-server” package before moving on to the VNC.
Because of the differences between the Linux and the Windows VNC protocol, we will select “x11vnc”. X11vnc is as close as it gets, without being very complex, to the Windows counterpart. X11vnc, as most vnc servers, use a separate password back-end then the standard Linux one – this is for security reasons. Once again, we have to add our users to the back-end. So type in a terminal vncpasswd ~/.vnc/passwd and hit enter, then verify your password.
Also, the port on which the server runs has to be entered. A simple echo 5900 > ~/.vnc/port will handle that. Finally, we create a custom command to call when we log in (which will be automated too, hold your horses). Issue a sudo nano /usr/local/bin/sharex11vnc and paste:
#!/bin/sh
x11vnc -nap -bg -many -rfbauth ~/.vnc/passwd -desktop "VNC ${USER}@${HOSTNAME}" \
|grep -Eo "[0-9]{4}">~/.vnc/port
Don’t forget to save and exit.
In case you were wondering, you just made your first script! But before we can use it, it needs to get the correct user rights. That’s easily fixed with a simple sudo chmod 755 /usr/local/bin/sharex11vnc. What the command “chmod” can do is beyond the scope of this guide, but issuing man chmod in a terminal will give a lot of information (And a cure for insomnia — Ed.). Now we need to autostart our freshly made script. The tool we use for that is located under “Applications” -> “Settings” -> “Autostarted Applications”. Click “Add” and fill in the fields Name and Command with “sharex11vnc”.
Now, let’s make our account autologon (this is required by VNC). So “Applications” -> “Settings” -> “Login Window” is the place to be. Under the tab “Security” you can find the checkbox “Enable Automatic Logon”. Once you have checked it you will also need to select a user which will log on. Logic says this is our main (and only, at the moment) user.
There! VNC and SSH services are set up. Now you can remotely control your server both through the GUI and the CLI. It’s time to tuck the box away somewhere, and forget it’s there. A word of caution is due, though – I noted that VNC is different on Windows than it is on Linux. In a Windows VNC server, you connect to a port of the machine; on Linux ,you connect to a screen on a machine. This is noticed when establishing a connection. Instead of connecting to “machine:port”, you connect to “machine:screen”.
As an additional security measure, I’ll show you how to connect your VNC through an SSH tunnel. I’ll use putty as the ssh client and use the UltraVNC client for the GUI.
We start by making the SSH tunnel. Fire up putty, type in the IP of the server. Don’t press “Open” just yet, we have to define the tunnel too. Under “Connection” -> “SSH” you’ll find a entry “Tunnel”. The source port is “5900” and the destination is “localhost:5900” (localhost refers to the remote machine).
Click “Add”, and then “Open” to create the connection. If all goes well you’ll be asked if you trust the key of the remote server, which we do. Then you can log in with your username and password (as used on the server).
Now, fire up the UltraVNC client (viewer) and connect to “localhost:0”. This refers to the local machine on the default port and screen 0. Because we made a tunnel from the local machine to the server, connecting to that port will end up on the server. After clicking connect you’ll be asked for a password. This is the password we set for the VNC server.
And there you go, a secure VNC connection to your server has been established.
P2P (Torrentflux via Apache, MySQL, PHP)
Now, on to the web-based torrenting solution. This is the heaviest requirement that our server has, because Torrentflux, one of the best web-based solutions, has quite a lot of dependencies.
To get it up and running we need a fully functional LAMP server (Linux Apache MySQL PHP). It’s certainly not like this will be hard to setup, it just requires some steps. Also, we’ll be installing Torrentflux from latest release we get from the torrentflux website, just to illustrate this way of installing software (rather than the package manager). Don’t worry, it’s not hard either.
If you really don’t want a web-based torrenting solution or to install a LAMP server, you can just use an alternative torrent solution from the Synaptic package manager, and VNC into the server to control it. But once again, what fun would that be? After all, you’ve come this far…
Yeah, that’s what I thought. Now, let’s get to work.
Before we install torrentflux, we need to first handle the dependencies. So fire up Synaptic and select “mysql-server-5.0” and “phpmyadmin”. Again, due to the handling of dependencies, apache2 will be installed alongside (apache is a dependency of phpmyadmin). After the install has finished, fire up “Firefox”, which can be found in the toolbar or under “Applications” -> “Network” -> “Firefox Web Browser”. Yes, some things still came pre-installed.
Now we browse to “localhost”. You’ll be greeted by a plain directory listing showing 2 directories. If you take a look in the “apache2-default” directory, you’ll be greeted by a big “It works!”. It’s an easy and to-the-point way that the Apache development team left you to test the web server. If you get the message, it will just work.
Now, on to MySQL. Go back a step and head for the “phpmyadmin” directory. Upon entering you’ll be greeted by a nice login screen. Just log in with username “root” and a blank password. Note, this is NOT the root password of your system. It’s the root password of the database. Upon logging in you’ll see some fancy data about your database.
First, we will set a root password. This should be required, as it will make your whole system considerably safer. Therefor, select any database from the “Database” drop down box. I took “Information_schema” but any will do. Now, click on “SQL” on the top of the page. The query you have to enter is: SET PASSWORD FOR root@localhost=PASSWORD(‘MYNEWPASSWORD’);, of course changing “MYNEWPASSWORD” for something safer. Confirm by clicking “Go."
So, now that that’s done, browse to “http://www.torrentflux.com” and download the latest Linux version to your home directory. The software comes in a .tar.gz package. This is a gzipped archive – think of it as something along the lines of a Windows .rar file. At the moment, the latest version is “torrentflux_2.3.tar.gz”. Now, to extract the archive (and also configure Torrentflux), fire up a terminal once again. We’ll start by extracting the archive. Issue a tar xvzf torrentflux_2.3.tar.gz to extract the archive. The syntax is quite straightforward. Tar, the command to be run, and you want to eXtract a gZipped File Verbosely, named torrentflux_2.3.tar.gz. See? CLI is easy.
Now, to get torrentflux running we need to configure a bit. First we need to create a torrentflux database user. This is done easily through CLI. A simple mysqladmin –u root –p create torrentflux will do that for us. Just enter your MySQL root password and the new user will be created. Now, we have to create the database itself. Don’t worry, the guys over at Torrentflux have made a sql query which handles it all. So, change to the sql directory by doing cd sql, and issue mysql –u root –p torrentflux < mysql_torrentflux.sql. When asked for a password, enter the MySQL root password once again. Now, for the final configuration, change to the html directory, by doing a “cd ../html” and edit the config.php file (I once again use nano, you could use gedit if preferred), so nano config.php. In the config.php file, search for the line starting with $cfg[“db_pass”] and input the MySQL root password.
There, most of the configuration is done. Now we need to copy all the data into the correct place. First we create a new directory in the webserver by doing a sudo mkdir /var/www/tf. When asked for a password, this time you’ll input your system password. Now we copy over all the data, by issuing sudo cp –r * /var/www/tf. This concludes the installation of Torrentflux!
However, it might be handy to have one central shared directory for all users. The logical place for this directory is directly under /home. Therefore, we type cd /home to change to the root of the home directories, and then we create a directory named "shared" by typing sudo mkdir shared. To make it shared, we set the user rights to 777 (user, group and world read, write and executable) by entering sudo chmod 777 shared. Inside the shared directory we will create a “torrent” directory where we will put our torrent files into. So issue cd shared, mkdir torrent and chown 777 torrent. Open up Firefox once again and head over to localhost. You’ll see an extra directory named “tf”.
Enter that directory and log in with username “root” and your MySQL root password. Change the path variable to “/home/shared/torrent” (or /home/Username if you chose not to create a shared directory) and confirm at the bottom of the page. You can reach torrentflux now from any workstation on your LAN, just by browsing to “(ip of the server)/tf” and logging in with the aforementioned credentials.
How to use Torrentflux is beyond the scope of this guide, but I doubt you’ll need the help. It’s quite straightforward and easy.
An introduction to scripting
The ultimate edge that Linux has to offer over Windows is the ability to do extensive scripting. Nearly all tasks on a Linux station can be done through the use of scripts. There are many scripting languages supported out of the box or with minimal installation on a Linux stations, like PHP, perl,… But a lot of people are surprised that even the (standard) Linux shell, BASH, offers extensive scripting.
These so-called shell scripts are really easy to learn because they use the commands that you actually use in a shell. So there’s no need to remember functions or syntaxes specific to the language. Giving a detailed guide about Linux scripting is beyond the scope of this article – there are hundreds of sites devoted just to shell scripting. But why not show you what scripting can do with an example?
You will probably remember that you had to add a user to the samba password back-end separately. Now, if you want to add another user, you have to remember all of those steps – because if you don’t, the user cannot access the samba shares. So first we have to add the user to the system, and afterwards we have to add him to the samba password back-end. If the user gets VNC access, that’s a whole additional step. So let’s write a small and easy script that handles this for us.
Fire up a terminal. Root or not doesn’t matter that much to write the script, however, it does matter in the execution of the script. It is logic that only root can add users to the system, right? So lets build a check for this in the script too. We will call the script ‘useradd.sh’, so we issue “nano useradd.sh”. This will create a file named ‘useradd.sh’ in our parent directory (since it’s a fresh terminal, that would be your home directory) and let nano (the file editor) open it.
To start our script, we have to tell the system what to use to execute the script. This is going to be a shell script, so we will use bash. We tell this to the system by the special string “#!” followed by the path to the script interpreter. Therefor, the first line of the script will be “#! /bin/bash”. From now on each line will be interpreted as ‘BASH language’ In BASH, comments start with a “#” and continue to the end of the line. Let’s personalise our script in the OSS spirit, by claiming credits at the start of the script. so, line 2 will become something along the lines of “# Useradd script written by Ken Gypen (Glider)”. Be creative, as long as it starts with a “#”, all is good.
So, now on to the serious work. First we shall check if we execute this script as root. There are a lot of ways of doing this, but probably the easiest way is through the use of the ‘whoami’ command. If you type ‘whoami’ in a terminal, the system will return your username. Used in a script it wil return the username of the one executing the script, but in BASH system commands have to be placed between backtics “`”(not single quotes). So we want to know if the output of the ‘whoami’ command is “root”, if it isn’t, the script has to exit with a fancy exit message. So let’s break it down line by line in BASH code.
We start with the comparison, using an ‘if’ structure. So, “if [ `whoami` != “root” ]” will be our next line. If the output of the command is not equal to root. Easy isn’t it? Now, on the following line, we just write “then”. So if it’s not equal to root, then… And the things to do go on the following lines. First we shall inform the user by showing a fancy line of tekst, and then we exit the script. Showing text is done through the command “echo”. So the line under ‘then’ will be something along the lines of “echo “You should be root to add users!””, followed by “exit“ on the next line. Now we have to let the script know that the end of the if statement is reached. We do that in BASH by typing “fi” (if backwards) on the next line.
Several popular Linux sites, including LinuxOnline and LinuxQuestions.org offer scripts for download that you can tweak to fit your needs.
There, now we are sure that everything that is after that if statement is run as root, because otherwise the script exits. Now, we will ask the user to input the username of the new user, which we will use in the “useradd” and “smbpasswd” command. The former command to add the user to the system, the latter to add the user to the samba password backend. We ask for input through the “read” command. The input is saved in a variable, which we will call ‘username’. So, next 2 lines in our script will ask for a username, and store it in the variable. Just type “echo -n “Username: “” and “read username” on the next 2 lines.
Now, on to the adding of the user. Useradd takes quite a few switches to set things like the location of the users homedir and the users default shell. For more information about useradd, just type “man useradd” in a terminal. In our case, we want to set the shell to /bin/bash, and the userdir to /home/ and also make sure that directory is created. This gives us following command: “useradd -d /home/$username -m -s /bin/bash $username” notice the “$” in front of the username variable. This is BASHs way of calling variables. So, if we entered “bit” as username, the command will become “useradd -d /home/bit -m -s /bin/bash bit” Now, we also want to set a password for the user, this is done through the “passwd” command. So the next line will simply be “passwd $username”.
Now, the final step is adding the user to the samba backend. The command “smbpasswd” with the correct switch (-a in our case) will do that for us. So, the final line will become "smbpasswd -a $username". There you go, save and exit and you have just written your first BASH script. It should be something along the lines of:
#! /bin/bash
# Useradd script written by Ken Gypen (Glider)
if [ `whoami` != “root” ]
then
echo “You should be root to add users!”
exit
fi
echo -n “Username: “
read username
useradd -d /home/$username -m -s /bin/bash $username
passwd $username
smbpasswd -a $username
Try running it as a your regular user by issuing sh useradd.sh. You’ll be greeted by your message and the script will just stop. Now try “sudo sh useradd.sh” (or change to user root first by issuing sudo su and then call the script). You’ll see the script does indeed work.
I hope you understand the power that scripts possess. There are plenty of (more complex) scripts floating around the net, usually being offered on the various forums dedicated to Linux and/or Open Source Software. Almost all are Open Source, meaning it’s hard to not find what you’re looking for or be able to tweak it to do just what you need. I’ve seen scripts do as little as echo the system variables, or as much as sorting entire collections of MP3s by reading their ID3 tags, automatically ripping DVDs (http://www.starrhorse.com/kubuntu-hacks/), converting MPEG movies to AVI, and pretty much anything else that can be done on the computer.
Conclusion
This concludes this small how-to which just briefly touched the powers that are vested in Linux. You can turn a piece of junk hardware which is now being used as a doorstop into a useful, low power machine.
But this guide is just one approach, every distro has its disadvantages, and also its advantages. Ubuntu is user-friendly, but the user-friendliness is often frustrating to power users (like many of you often feel in Windows). Then we have Debian, a bit less user-friendly but many times more powerful.
There’s also Gentoo, considered as one of the most powerful distributions out there, but with that a fair deal of user-friendliness goes out the door, certainly upon installation. (Or during installation – Ed.)
A great thing about all the different distributions is that although each one has its subtle differences, they are all of the same basic idea. And most things work nearly identically on all distributions. Also, all major distributions offer great package managers.
Ubuntu (and Debian) use apt, Gentoo uses portage, Redhat/Fedora use yum,… Different names, different ways of using them, but all great pieces of software to get your package (and its dependencies) installed in no time. Most even offer default configs for the different packages, which are tailored to the needs of an average user.
With all that said, try doing this with a Windows box. If you’ve been following along, you have set up a fully-working file fetcher and server for your network inside of two hours, including download time. Who said that Linux wasn’t user-friendly?
Hi there, this weekend is pleasant designed for me, because this occasion i
am reading this impressive educational piece of writing here at my home.