Don
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
.
Go there...
http://www.openlogic.com/wazi/bid/297159/centos-system-administration-using-text-based-user-interfaces
0 comments:
Post a Comment