Open Source Software Technical Articles

Want the Best of the Wazi Blogs Delivered Directly to your Inbox?

Subscribe to Wazi by Email

Your email:

Connect with Us!

Current Articles | RSS Feed RSS Feed

CentOS system administration using text-based user interfaces

  
  
  

Administering CentOS 6 from the command line is not always intuitive and easy, especially when it comes to complex commands with hard-to-remember arguments and strict syntax. Many CentOS servers lack an X Window System graphical interface, which supports applications that could ease administration tasks. They can, however, run text-based user interfaces (TUI), which are often in the form of wizards, and which provide options for navigation, accept text input, and show progress bars.

CentOS has plenty of TUIs available from the official CentOS repository. For instance, system-config-network-tui helps you configure the CentOS network. This powerful Python script saves you the burden of editing numerous files to modify your network interfaces, routes, and DNS information. Similarly, system-config-firewall-tui helps you configure your firewall. It offers basic options to just allow incoming services, such as DNS and FTP, and more advanced options to fine-tune the iptables rules, such as configuring trusted interfaces and masquerading.

To find more TUIs, run yum search tui, or look for executables whose names end with -tui by using the argument whatprovides with a regular expression: yum whatprovides *bin/*-tui. This command turns up, among others, the package authconfig and its executable /usr/bin/authconfig-tui, which guides you through CentOS's authentication configuration. It allows you to configure advanced settings such as the use of LDAP authentication servers and caching of passwords. Without a TUI, authentication configuration is one of the most unpleasant administration tasks in CentOS because of the need to edit numerous files and perform various checks.

You may also find a TUI by just searching for system-config, though some of the results will require graphical interfaces. For example, system-config-users, a tool for managing users and groups, does require an X interface, while system-config-keyboard, a tool for configuring the keyboard and its layouts, is a pure command-line TUI.

Unfortunately, there is no reliable way to tell whether a system-config script requires an X interface or runs at the command line. You can try figuring it out by reading the description of the package with the command yum info PACKAGENAME. You may also be able to tell whether X is required by the dependencies of a package. Run yum deplist PACKAGENAME to see if a package has any X-related dependencies. For example, the package system-config-date requires the package gnome-python2-canvas, part of the GNOME X desktop, so clearly it requires an X interface.

Other system TUI scripts are not related to any specific keyword. One example is the script /usr/sbin/ntsysv, part of the ntsysv package, which helps you configure the runlevels for all installed services, including whether the services should be started and stopped with the system. It's a big help for those who are not familiar with the /sbin/chkconfig command, which manages the startup scripts and runlevels.

With such an abundance of TUIs, how do you remember them all and associate them with the tasks they are for? Turn to the package setuptool and the command it provides, /usr/sbin/setup, to detect all installed system-related TUIs and show them in a well-organized menu.

Besides the native CentOS TUIs, you can find many TUIs that come from third-party software providers for their respective applications. These TUIs not only make things easier but in some cases are the only feasible way to perform an installation and configuration. For example, the 389 Directory Server comes with a few TUIs, and you should use its main configuration script, /usr/sbin/setup-ds-admin.pl, for the initial configuration.

While TUI scripts look attractive in comparison to the daunting tasks of running complex commands, TUIs are limited and cannot be complete replacements for all configuration tasks. For example, ntsysv does not allow you to manage a service for a program that you have installed manually. For that purpose you still need to use the command line, as described in the article How to write CentOS initialization scripts with Upstart. For more advanced tasks and uncommon scenarios you will still need to run commands with numerous arguments and edit text configuration files.

Create your own TUI

By now you should be convinced of the benefits of TUIs, and you may wish to create your own. In CentOS, most TUIs implementations are based on the Curses library. For Bash, the most popular Curses implementation is called Dialog. Dialog integrates seamlessly with Bash scripts and provides good-looking TUIs through simple code. Install it with the command yum install dialog.

Dialog provides various TUI components, such as progress bars, menus, and text boxes. Here is an example Dialog script with a menu text box:

#!/bin/bash

#The dialog part. By default, the chosen menu item goes to STDERR (2). From there it's redirected to a temp file (/tmp/temp_file) in the script.
/usr/bin/dialog --title "Delete /tmp content" --menu "Delete /tmp content older than:" 10 30 0 1 "1 day" 2 "2 days" 2> /tmp/temp_file

#Get the choice by reading the /tmp/temp_file
option=`cat /tmp/temp_file`

#Remove the temp file
rm -f /tmp/temp_file

#Clear the screen for better readibility
/usr/bin/clear

#Create a function to delete the temp content. It accepts as an argument a number of days. Any content older than this number will be deleted.
function delete_tmp {
    /bin/echo "Deleting files and directories older than $1 day(s) in /tmp"
    /bin/find /tmp/ -type f -mtime +$1 -exec rm {} -rf \;
    #Show a dialog gauge with the progress 
    /bin/echo "50" | /usr/bin/dialog --gauge "Deleting files" 10 30 0
    #For better visual effect wait 1 second before continuing
    /bin/sleep 1
    /bin/find /tmp/ -type d -mtime +$1 -exec rm {} -rf \;
    #Show a dialog gauge with the progress again
    /bin/echo "100" | /usr/bin/dialog --gauge "Deleting directories" 10 30 50
    /bin/sleep 1
    #Clear the screen
    /usr/bin/clear
}

#Go through a conditional statement
#First check if $option is not null. Option is null usually when the cancel button from the menu is pressed.
if [ -z $option ]; then
    /bin/echo 'Cancel pressed. Exiting.'
#Check if the first option is chosen
elif [ $option -eq 1 ]; then
    delete_tmp 1
#Check the second option
elif [ $option -eq 2 ]; then
    delete_tmp 2
fi

This TUI script first gives you a menu with two options – to delete content older than one or two days in the /tmp directory. Once you make your choice, the script displays a gauge with the progress. See how after both the menu and the gauge parameters there are two numbers – 10 30? These specify the height and width of the text box measured in text characters. The third number after the two basic dimensions is the height of the menu. In the case of the gauge this third number denotes where the progress bar should start from in terms of percent.

As you can see, the CentOS shell is not just about dull commands with long arguments. With TUIs, the shell becomes interactive and easier to use.

 




This work is licensed under a Creative Commons Attribution 3.0 Unported License
Creative Commons License.

Comments

If it is difficult and non-intuitive that means you are likely to make mistakes so unless you afre trying to script it you should use a GUI instead. 
 
Recently I had to rebuild the RAID array of a server. Using the command line it meant typing a long list of seemingly random numbers without any structuring element who would have made easier to chjeck for mitskes. In addition this was the kind of commands you use once inj a lifetime so the odds of a mistakes were high. Getting it wrong would have meant several days of a downtime on server and my head on the chopping block. So I used the GUI utility and got the work done with near zero chances of doing it wrong. 
 
BTW: As far a as I remember Redhat's (and Centos) system-config-network-tui is deinitely crippled respective to the GUI utility.
Posted @ Monday, June 17, 2013 6:21 AM by JFM
The same is true of Microsoft's BASH knock-off, "Power Shell". Any time you have a scripting interface like this, you need to know the commands. Fortunately, that's what man pages and Google are for, so if I forget a UNIX or Linux command, I just Google for what I want to do. That's how I recently reminded myself of the "iostat" command, which I hadn't used in a while. Then..."man iostat", and I was in business in short order. 
 
For typical MCSE's, the GUI is best, since they typically don't know what they're doing anyway. :-) But if you have a clue (and a few MCSE's actually do), the command line is where the real power is at. It does, however, come with the requisite complexity that any command-line interface with any real power would tend to have. And that applies to both GNU/Linux and Microsoft systems. 
 
--SYG
Posted @ Monday, June 17, 2013 8:40 AM by Sum Yung Gai
I use the command line when I script something or when using it takes less time and is less error prone than clicking over dozens of files. I don't use it just for the sake of it or when using it is asking for trouble. BTW man pages are for sissies. _Real_ men read the source and infer what the command is for and how to use it.
Posted @ Monday, June 17, 2013 9:48 AM by JFM
How useful is that anyway? 
All this *tui crap is for people who doesn't know Linux from their microwave and they are unable to use their microwave properly either. All this distro-specific stuff is not helping anybody.
Posted @ Wednesday, July 03, 2013 5:22 PM by al3x
fdsfsf
Posted @ Thursday, December 19, 2013 6:21 AM by fffsdfds
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics