Tuesday, November 27, 2012

How to give someone root access in Linux

Giving someone root access in linux is easy. Why would someone need to be root I don’t know but this is how you do it using the usermod command.
To add root access
[root@abika root]# id sys_admin
uid=508(sys_admin) gid=508(sys_admin) groups=508(sys_admin)
[root@abika root]# usermod -G root sys_admin
[root@abika root]# id sys_admin
uid=508(sys_admin) gid=508(sys_admin) groups=508(sys_admin),0(root)

To remove root access.
[root@abika root]# usermod -G sys_admin sys_admin
[root@abika root]# id sys_admin
uid=508(sys_admin) gid=508(sys_admin) groups=508(sys_admin)

cannot allocate memory error

Had a weird memory error my Linux servers at work today. These were heavy duty machines runningĂ‚  heavy jobs throughout the day. /var/log/messages kept reporting that the kernel had no more memory to process new jobs. Most of the errors read ” cannot allocate memory ”
Some digging around later we found the fix. Kernel 2.6 has a new parameter “min_free_kbytes” which allows it to reserve a dedicated amount of memory for itself to process jobs. This kept the kernel from choking up when the servers were faced with sudden spikes in heavy jobs.
I set my server’sĂ‚  “min_free_kbytes” to “4096″ kbytes which was the recommended value. It’s more of a trial and error configuration so I’ll have to monitor the server over a period of time and increase the value if needed till I hit the sweet spot.
How to set it?
To have the new value take effect immediately, edit the “/proc/sys/vm/min_free_kbytes” file. Remember!, reboot and the changes will be forgotten.
echo "4096" > /proc/sys/vm/min_free_kbytes
To have it permanent, add “vm.min_free_kbytes=65536″ to the /etc/sysctl.conf file.
echo "vm.min_free_kbytes=4096" >> /etc/sysctl.conf

Get your PID with $$

“$$” is a useful Linux variable you could use in your script to get it’s PID. The “$$” variable always holds the PID of the executing process.
Why do you need it? Maybe to check if the script is already running? This is what I normally use it for.
Sample Script;
#!/bin/bash
echo "My PID is $$"
sleep 2

Sample Output;
[root@keke ~]# ./test1.sh
My PID is 8909

System uptime with uptime

Want to know how long your Linux box has been up for?
Simple, just run the “uptime” command and you will be rewarded with the answer plus a bit more.
8:58pm  up  19:54,  1 user,  load average: 0.47, 0.62, 0.35
Above is the typical reply from uptime. On the left is the current time, followed by the system’s uptime, logged in users and finally the system’s load average.
Sample output;
danesh@pandora:~> uptime
8:58pm  up  19:54,  1 user,  load average: 0.47, 0.62, 0.35

Simple sort with the sort command

You can easily sort your outputs in Linux using the “sort” command. Simply pipe “|” your output to a “sort” command and you should see the sorted results.
See sample usage below. This is just to start off, I’ve cover more in future posts.
[root@hantu ~]# cat numbers
5
4
3
2
1
0
6
7
8
9

[root@hantu ~]# cat numbers | sort
0
1
2
3
4
5
6
7
8
9

[root@hantu ~]# cat numbers | sort -r
9
8
7
6
5
4
3
2
1
0

Happy sorting!!

Reload you /etc/inittab file

You made changes to your /etc/inittab file but can’t effort any downtime.
There’s a simple trick to reload and apply changes in your /etc/inittab file without a reboot.
Run “init q” or “init Q”
[root@snoopy ~]# init q
[root@snoopy ~]#
[root@snoopy ~]# init Q
[root@snoopy ~]#

Download with cURL

Here’s how to download a file using cURL.
curl -O [full url to file]
curl -O http://downloads.wordpress.org/plugin/simple-tags.1.6.6.zip
Sample Output;
[root@kmon01 bin]# curl -O http://downloads.wordpress.org/plugin/simple-tags.1.6.6.zip
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 585k 0 585k 0 0 95317 0 --:--: