Linux : Taking control of Virtual Terminals (VT) from command line
When you use Linux in text mode (as opposed to with an X server), you readily have access to multiple Virtual Terminals (aka VT for short) by hitting one of your
This lets you access one of the VTs which are initialized at boot time, but won’t let you create new ones even if your kernel configuration would allow more VTs. Furthermore, what if you want to deal with VTs from a script ?
This post covers the 3 commands which will let you control your VTs from the command line or from a script.
Unix : the “script” command
The script command is a must for any unix sysadmin.
Once invoked, it will faithfully write anything you typed as well as any output generated in your terminal into a file of your choice (defaults to “typescript”).
This is great when you want to document everything you did on a specific server, for example.
spaghetti:~$ script<br /> Script started, file is typescript<br /> spaghetti:~$<br />
When launched, you don’t see anything, but everything displayed goes to a file as well as the terminal.
Web Security : What are XSS?
XSS (Cross Site Scripting) are a kind of attacks which are fairly popular these days and could target anyone, but are not nearly well known from most people.
In this post, I’ll try to give a short explanation of what they are.
awk : one-liner to select a field
Getting a specific field of a line with awk is really simple. For example :
spaghetti% echo "test1 test2 test3" | awk '{print $2}'<br /> test2
A more “real life” example is as following, which will find in /etc/hosts the IP address of localhost (pick another host if you wish 🙂 ) :
spaghetti% cat /etc/hosts | awk '/localhost/ { print $1;}'<br /> 127.0.0.1<br /> ::1<br />
If the field separator is not a space, awk will let you change it with the FS variable :
spaghetti% echo "test1:test2:test3" | awk 'BEGIN { FS = ":"} { print $2 }'<br /> test2<br />
Apache : Perform load tests and bench Apache
If you need to simulate a load on an Apache server (or any web server actually), you can use Apache Bench, which is included in the standard Apache HTTPd distribution. This tool will launch connections to your webserver as instructed to simulate multiple users and will help you to tune your Apache settings.
You can find the synopsis at the Apache website. Most common options are :
- -n : number of requests to perform
- -c : number of concurrent requests
Other options allow you to control precisely the request to send, proxy settings, user authentication, cookies and much more.
vi : replacing text globally (search and replace)
If you want to replace a text throughout a file in the vi text editor, you can use the following command :
:1,$s/text/replacement/g
Here is the breaking down of this command:
- “:” : places you in “ex mode”
- 1,$ is a range specification, meaning from the first to the last line (this can be shortened as % in some versions of VI (vim does, for instance) — Thanks Brandon !
- s means “substitute”
- /text/replacement/ means to replace the “text” pattern by “replacement”
- g : means globally, which will replace all the occurrences of the pattern instead of the first of each lines
If you want to see how to script a text replacement, check out my previous post about text replacement with sed.
sed : replacing a text in a file
To replace a text in a file, you can invoke sed as in the following example :
% cat file.txt | sed -e 's/text/replacement/g' > result.txt
This will change all the occurences of “text” to “replacement” in “file.txt” and output the result in “result.txt”
Note : As suggested by Matthias from adminlife in the comments, if you wanted to do “in place” text replacement (that is modify the file without a temporary file in between), you can do the following :
Linux – RedHat (or CentOS) : update system and packages with yum
In order to keep your system updates (especially important for security fixes) on a RedHat linux system (or CentOS), you can simply perform the following command :
yum update
You’ll then be presented with a list of available updates for your system.
Doing so from time to time will help you to keep your system secure and to get the latest versions of your softwares.
If you wonder how to do the same with a Debian Linux system, check out my previous post about Apt, the Debian package manager.
Linux – RedHat (or CentOS) : list installed packages
If you need to list packages which are installed on a RedHat system, you can do so by issuing the following command :
rpm -aq
Alternatively, if you prefer to use the package manager, you can try this command :
yum list installed
if you want to know how to do this on Debian, check out my previous post on Apt.
Unix : Cron daemon, crontabs and where to find them
Cron is a fairly standard daemon which you’ll find on most (if not all) Unix machines. Its purpose is to schedule the execution of commands at a specified time.
Sometime you’ll log at some performance graph (CPU load for example) and find out that every day/week/month/other there is an unexpected peak and you’d like to know why. Of course if this is regular you’ll think of Cron as a good trail to follow.