Server set-up: Fixing locale and unknown key issues on VPS / Ubuntu “cloud” images

Here are a couple of problems I’ve seen with newly created instances of virtual servers, and their solutions.

locale:

When running apt-get upgrade for the first time, many errors appeared regarding an “unset” locale.

My locale is US/English, so the fix was:

sudo locale-gen en_US en_US.UTF-8
dpkg-reconfigure locales

missing keys:

Errors regarding unknown or missing keys.

[MISSINGKEY] is the string in the error message.

apt-key adv --recv-key --keyserver keyserver.ubuntu.com [MISSINGKEY]

Trailing slash or no trailing slash with an rsync path argument?

Nicely illustrative explanation of trailing slash behavior in rsync:
devblog.virtage.com/…/to-trailing-slash-or-not-to-trailing-slash-to-rsync-path/

Essentially:

  • An rsync source path with no trailing slash will recreate the top-level directory of the source at the destination.
  • With a trailing slash the subdirectories of the source path are created directly in the target directory.

comparing rancid files on two different servers

Usually, I just use rsync to keep files in sync, but this runs through all of the files.

Starting on server #1, inside the rancid base directory (/var/lib/rancid for me),
with $LIST_OF_GROUPS from /etc/rancid/rancid.conf,
using ssh with port 2022 ,
$REMOTE = DNS name of remote server :

for i in $LIST_OF_GROUPS ; do echo $i ; rsync -n -i -e 'ssh -p 2022' /var/lib/rancid/$i/router.db $REMOTE:/var/lib/rancid/$i/router.db ; done

To run vimdiff to see or manually sync a single file:

vimdiff /var/lib/rancid/ABC/router.db scp://$REMOTE_SRV:2022//var/lib/rancid/ABC/router.db

Note that this uses 2022 as an alternate ssh port for scp (as called by vimdiff).

The automated sync operation is actually done from jenkins. This is probably not the ideal formula, but for self-documnetation purposes, here it is:

crontab on server1 to remote server2. (REMOTE_SRV = server2’s DNS name)
to copy files from server1 to remote server2.

@daily export JENKINS_HOME=http://JENKINS.brunhilda.edu:8080 ; java -jar /usr/share/jenkins/external-job-monitor/java/jenkins-core-*.jar "rancid_sync srv1 to srv2" rsync --exclude 'configs' --exclude 'bin' --exclude 'logs' --exclude 'CVS' --compress --itemize-changes -Cavh -e 'ssh -p 2022' --checksum $REMOTE_SRV:/var/lib/rancid/ /var/lib/rancid/

Working with dates and timestamps in GNU Linux

With the GNU date command line utility.

Print time as seconds from the epoch, or, “seconds since 1970-01-01 00:00:00 UTC” :

$ date +%s
1365439166

Convert timestamp or epoch dates into familiar day and time :

$ date --date='@1365439166'
Mon Apr 8 11:39:26 CDT 2013

Usage from GNU coreutils 8.x, and may work elsewhere.

sort files by size

du -sh * | sort -h

Output should be sorted like this (K, M, G for Kilobytes, Megabytes, Gigabytes):

8.0K totem.txt
8.0K em.vlan.txt
20K dot.png
68K pid5367.log
104K dmesg.txt
384K hwinfo.txt
1.2M textarchive.tar.gz
2.9M Archive
78M minicom.cap
226M vorbis
688M 09283.ar
1.2G VirtualBox VMs
2.5G Downloads

du and sort from GNU coreutils 8.23   

Stripping leading whitespace and comments with sed

In trying to consolidate some bash aliases (found in .bashrc and .bash_aliases), I needed to strip out comments and leading whitespace to sort for unique lines.

To strip spaces, I used
egrep alias .bashrc | sed 's/^[ ]*//'

To remove spaces and tabs in the whitespace (see info on tabs after this example):
egrep alias .bashrc | sed 's/^[ ]*//'
NOTE: In the expression, sed 's/^[ ]*//' , the expression in the brackets is actually a space followed by a tab. At the command line, I had to create the tab by typing Control-V and then hitting TAB on the keyboard. You may need to represent tabs in a different fashion.

To get rid of leading whitespace, comments, and indented comments, I settled on
egrep alias .bashrc | sed 's/^[ ]*//' | egrep -v '^#'

To gather and sort aliases from both .bash and .bash_aliases, then sending to a temporary file:
egrep -h alias .bash[r_][ca]* | sed 's/^[ ]*//' | egrep -v '^#' | sort -u >> tmp

The -h flag tells the first grep not to print the file name.

Getting ‘Scroll Lock’ to work under Linux

I’ve got a couple of keyboards with Scroll Lock keys that are not functional by default under Linux.
For my Ubuntu systems, the following procedure worked to fix the problem. After this, the LED for scroll lock lights and the key function as expected.

The steps are as follows:

  1. Find an unused modifier with xmodmap -p
  2. Use xmodmap to assign Scroll_Lock to this modifier
  3. Make the change persistent by adding it to your .Xmodmap file

First, use xmodmap -p to identify an unused mod key.
For example,

$ xmodmap -p
xmodmap: up to 4 keys per modifier, (keycodes in parentheses):

shift Shift_L (0x32), Shift_R (0x3e)
lock Caps_Lock (0x42)
control Control_L (0x25), Control_R (0x69)
mod1 Alt_L (0x40), Alt_R (0x6c), Meta_L (0xcd)
mod2 Num_Lock (0x4d)
mod3
mod4 Super_L (0x85), Super_R (0x86), Super_L (0xce), Hyper_L (0xcf)
mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)

mod3 is the open modifier. So, we’ll use it with xmodmap -e :

xmodmap -e "add mod3 =Scroll_Lock"

Capitalization is important, so “Scroll_Lock” has to be written exactly as printed.
You can have a space, or no space, after the equal sign.

Check that it works, then add the command content to your home directory (file does not have to exist):

echo "add mod3 =Scroll_Lock" >> ~/.Xmodmap

send man page to text file

Printing a manpage to a text file (perhaps more portable for cross-platform or bedtime reading):

generic Unix/Linux, using `col`:
man rcs | col -b > /tmp/man_rcs.txt

These alternatives would require that you reset PAGER or MANPAGER:
alternative BSD :
export MANPAGER=cat
man pf.conf > man_pf.conf.txt

alternative BSD :
export PAGER=cat
man pf.conf > man_pf.conf.txt

Ubuntu, Debian, Mint, etc. allow all of these alternatives:

-P pager, --pager=pager
Specify which output pager to use. By default, man uses pager -s. This option overrides the $MANPAGER environment variable, which in turn overrides the $PAGER environment variable. It is not used in conjunction with -f or -k.

pink noise

Pink noise can be used to mask distracting sounds in the environment around you. It can be useful for providing a neutral sonic background for concentration.

The program speaker-test comes in the alsa-utils package, and provides pink noise by default. This is described by its man page as “perceptually uniform noise”. There are a number of other options, but for a quick noise generator, speaker-test is very handy.

Command line fun for linux : included utilities

obelix@entity:~$ info util-linux-ng

Next Page »