Tuesday, November 27, 2012

How to increase file descriptors max limit on Linux

Today my DBA reported that the server she was working on was spitting out “too many open files” errors and no new processes could be started.
This is a common problem with DB servers with heavy transactions. In my environment there are 6 DB instances running on the server. No quite the optimized setup I would say.
The fix was to increase the total file descriptors kernel parameter count in the /etc/sysctl.conf file. I doubled my limit from 8192 to 16384.
The walk through,
1. Find out what the current open file descriptor limit is.
~# more /proc/sys/fs/file-max

~# 8192
or
~# sysctl -a | grep fs.file-max

~# fs.file-max = 8192
2. View how many open file descriptors are currently being used.
~# more /proc/sys/fs/file-nr

~# 8191
3. View how many files are open. The number returned might defer as 1 file descriptor can have multiple open files attached to it.
~# lsof | wc -l

~# 10325
4. Edit the kernel paramneter file /etc/sysctl.conf and add line “fs.file-max=[new value]” to it.
~# vi /etc/sysctl.conf

fs.file-max = 331287
5. Apply the changes.
~# sysctl -p
~# fs.file-max = 331287
Problem fixed.

How to set default session timeout in Linux

My DC operation guys access Linux servers on a daily basis but somehow they never remember to log out. This is a security risk as anyone could gain access to the open console and create caos.
Today, yet again I’m forced to play the bad guy by dummy proofing my Linux servers by implementing default timeout for user sessions.
Bash and Korn both support the TMOUT variable which I will use to set the default timeout.
The etc/.bashrc file will apply the timeout system wide but if you need it to be user specific then modify the ~/.bashrc file instead.
Here’s how it’s done.
echo "TMOUT=300 >> /etc/bashrc
echo "readonly TMOUT" >> /etc/bashrc
echo "export TMOUT" >> /etc/bashrc
Log off, start a new session and wait for 5 minutes. Your session should terminate

How to keep your Linux session alive

Recently I wrote about implementing session timeouts on Linux. For admin’s who know what they are doing(most times) this can sometimes be an annoying experience.
There’s a simple noop script over at bashcurescancer to help work around session timeouts. This will work for ssh and also the default virtual consoles.


Watch noop in action.



Source: BashCuresCancer

How to change the system date in Linux

A friend needed help changing the system date on his Linux box today. This is usually a simple task for Linux users but newbies tend to get confused by the "date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]" line in the man page.
To simplify, this is how you do it.
Set the current date to April 7 2008 8:42:45pm.
The easy way,
#date -s "7 April 2008 20:42:45"
The harder way,
#date 040720422008.45
The break down: MM DD hh mm YYYY ss
MM = month = 04
DD = day = 07
hh = hour = 20
mm = minute = 42
YYYY = year = 2008
ss = second = 450

sample output,
[root@klmsyslog01p ~]# date -s "7 April 2008 20:42:45"
Mon Apr 7 20:42:45 MYT 2008
[root@klmsyslog01p ~]#

[root@kmmserver01p ~]# date 040720422008.45
Mon Apr 7 20:42:45 MYT 2008
[root@kmmserver01p ~]#

Google Earth for Linux

A spanking new release of Google Earth is out. The latest 4.3 release comes with Photo Realistic 3D modeling for buildings, “Swoop navigation” for an improved navigation experience, “Light and shadow” to catch sunrise and sunset from anywhere and finally the popular “Street View” which was previously a Google Maps only feature.
Now something to excite Linux users, Google Earth is now available for Linux. Not on WINE as but as a native application based on Qt and openGL. I’m yet to try it but for those who have I have heard nothing but good reviews with some minor glitches.
However Google Earth for Linux currently only supports the i386 architecture and seems to only work with 32bit processors.
The embedded video speaks for itself.
Download the latest Google Earth

How to limit ssh access to specific users or groups

Its sometimes necessary to limit who has access to a server via SSH. Most Linux security hardening checklist today require this to be enforced.
Fortunately this can be easily done with openSSH. Just edit the /etc/ssh/sshd_config file and add the desired directives shown below. You don’t need them all, just use what suits you needs.
openSSH provides 4 directives, AllowUsers, AllowGroups, DenyUsers and DenyGroups
AllowUsers buddy john doe
Only users buddy, john and doe will be able to log in via ssh.
AllowGroups sysadmin bkpadmin
Only users within groups sysadmin and bkpadmin will be able to log in via ssh.
DenyUsers rambo tina
This is the opposite of AllowUsers. All users except for rambo and tina will be able to log in via ssh.
DenyGroups hr payroll
This is the opposite of AllowGroups. All groups except for hr and payroll will be able to log in via ssh.

How to send a process to the background

Sending a process to the background in Linux is quite easy. All you need is bg, fg, &, and ctrl+Z ( ^Z ).
For this example I will use a simple bash script test.sh I put together to print “Test” every 5 seconds.
#!/bin/bash
#This script will print "Test" every 5 seconds
#
while [ true ]
do
echo "Test at `date`"
sleep 5
done
#End

Now let’s see how it’s done.
[user@abubu root]$./test.sh &
This starts test.sh and sends it to the background. You will be back at shell but should see the “Test” message every 5 seconds.
[user@abubu root]$jobs
[1]+ Running ./test.sh &

The jobs command will print all the background processes. Each process is represented by a number to it’s left. For example, tesh.sh is represented by 1.
[user@abubu root]$fg 1
The fg command will send the test.sh process to the foreground and return control to the shell.
[user@abubu root]$ ./test.sh (hit ctrl+Z (^Z) now)
Test at Tue Jun 3 15:11:38 MYT 2008
[1]+ Stopped ./test.sh

The test.sh process is temporarily suspended.
[user@abubu root]$bg 1
The bg command will send test.sh to the background.
[user@abubu root]jobs
[1]+ Running ./test.sh &

The jobs command will print all the background processes. Each process will be represented by a number to it’s left. tesh.sh is represented by 1.
[user@abubu root]$fg 1
The fg command will send the test.sh process to the foreground and return control to the shell.
That’s it.