How to renew dhcp lease in Linux
I’ve tried this command on Debian/Ubuntu, but since dhcpclient is the universal across all distros, I expect this to work on all *.nix variants.
First thing to take note is if you are ssh to the server, you should be careful before executing the commands.
sudo dhclient –r
The above command will cause all network adapters to release DHCP lease. So if you are ssh through one of the DHCP interface, you’ll get disconnected and unless you’ve physical or console access, there is no way to bring it back.
So I’ve come up with nice commands that you can use even if you don’t have console or physical access to the server. But you should be able to ssh to the host by using hostname in case DHCP server lease a new different IP addresses. If DHCP server responded with the same IP address, you won’t even get disconnect from ssh session.
sudo dhclient -r; sudo killall -9 dhclient
That’s the command I’ve used frequently and very convenient for me.
Source : Ubuntu Manpages
Change hostname in Ubuntu or Debian
If you’ve changed your mind about your Linux hostname or you’ve set to wrong hostname, here is how to fix it.
Firstly, I’ve tried the following command:
$sudo hostname mybrandnewname
But this will revert back to prior hostname whenever the machine reboots. So how can you change it permanently? First you’ll need to edit /etc/hostname file
$sudo vi /etc/hostname Then delete the old name and type whatevername you desire
It is also advisable to change new hostname in /etc/hosts file
Source : UbuntuManual
How to add new user and grant permission?
Once you’ve got your new favourite Linux distro installed, you’ll need to add users and grant permission as per your organization requirement.
The steps are very simple and straightforward.
sudo adduser
If you want to grant a user to all permissions as root, you should add that user to admin group. For example :
sudo adduser jason admin
Make sure the line “%admin ALL=(ALL) ALL” is present in the /etc/sudoers. The below is the screenshot of sample config file.
After that you might want to set specific password for the new user. You can do this by using the following command:
sudo passwd
Source : AskUbuntu
How to config ubuntu/debian to boot in text mode
I like Linux boxes to boot directly to terminal mode. If you’ve just moved to Ubuntu distro from Red Hat/ Fedora, you’ll think about changing the inittab. Well, bad news is in Debain or any of its derivatives, runlevels 2 to 5 are the same multi-user with display GUI. So if you type, sudo init 3, nothing will happen.
So here is how I make Ubuntu to boot into text mode by default. The requirement for this is you have to use grub as boot loader.
Just edit the /etc/default/grub using any of your favourite text editor, look for the line
GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”
Then changed it to
GRUB_CMDLINE_LINUX_DEFAULT=”text”
The following is the screenshot for what my grub file looks like after the changes.
After that save the file and run sudo update-grub. That’s it. Starting from next reboot, your ubuntu/debian variants will start in text mode.
Source : UbuntuForums
How to reverse a string using sed
I had got issue with having to reverse a string of number manually. Of course, that gave me a big headache. Thanks to Peter from Catonmat, I’ve got the following sed script.
sed ‘/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//’
As you can imagine, this is quite complicated, but luckily he’s given nice explanation as well.
sed ‘
/\n/ !G
s/\(.\)\(.*\n\)/&\2\1/
//D
s/.// ‘
The first line “/\n/ !G” appends a newline to the end of the pattern space if there was none.
The second line “s/\(.\)\(.*\n\)/&\2\1/” is a simple s/// expression which groups the first character as \1 and all the others as \2. Then it replaces the whole matched string with “&\2\1″, where “&” is the whole matched text (“\1\2″). For example, if the input string is “1234″ then after the s/// expression, it becomes “1234\n234\n1″.
The third line is “//D”. This statement is the key in this one-liner. An empty pattern // matches the last existing regex, so it’s exactly the same as: /\(.\)\(.*\n\)/D. The “D” command deletes from the start of the input till the first newline and then resumes editing with first command in script. It creates a loop. As long as /\(.\)\(.*\n\)/ is satisfied, sed will resume all previous operations. After several loops, the text in the pattern space becomes “\n4321″. Then /\(.\)\(.*\n\)/ fails and sed goes to the next command.
The fourth line “s/.//” removes the first character in the pattern space which is the newline char. The contents in pattern space becomes “4321″ — reverse of “1234″.
But what I want is the output to be separated by “.” aka dot character. So here’s what I come up with.
$ echo 123456789 | sed ‘/\n/!G;s/\(.\)\(.*\n\)/&\2.\1/;//D;s/.//’
.9.8.7.6.5.4.3.2.1
Still there’s one small issue, I don’t want dot at the start of the string. So after reading his explanation, I realize that I need to put one more “.” to be replaced at the fourth line. This is final script.
$ echo 123456789 | sed ‘/\n/!G;s/\(.\)\(.*\n\)/&\2.\1/;//D;s/..//’
9.8.7.6.5.4.3.2.1
So obviously, if you want “,” aka comma instead of dot, you can just substitute it. If you are curious about getting to know sed, why don’t you head over to catonmat.net. That site got so many cool sed one liners.
Source : Catonmat
How to rename multiple files in Linux
From time to time, we may need to rename multiple files in Linux. It is quite frustrating to find out that mv old_name* new_name* is not working actually.
So after a few trials and errors, I’ve got the following script, which is really time saver for me.
This is what my files look like before the script.
$ ls -lth
total 12K
-rw-r–r– 1 username group 6 Aug 26 01:38 old_name_3.txt
-rw-r–r– 1 username group 6 Aug 26 01:38 old_name_2.txt
-rw-r–r– 1 username group 5 Aug 26 01:38 old_name_1.txt
I want to rename all those old_name to new_name. Here is the script to do that:
$ for nn in `ls -1 old*`; do NAME=`echo $nn | sed -e ‘s/^old_name/new_name/g’`; mv $nn $NAME; done
This is the aftermath.
$ ls -lth
total 12K
-rw-r–r– 1 username group 6 Aug 26 01:38 new_name_3.txt
-rw-r–r– 1 username group 6 Aug 26 01:38 new_name_2.txt
-rw-r–r– 1 username group 5 Aug 26 01:38 new_name_1.txt
I hope this script will be helpful for you as well. As always, feel free to leave a comment if you need any help.
Simple bash mail script
If you want to automate something on any Unix based servers and want to send you email in the bash script, here is the simple one.
#!/bin/bash
FROM="from_address@example.com"
TO=”to_address@example.com”
SUBJECT="Test Subject”
EMAILMESSAGE=”Test Message”
…
/bin/mail -s "$SUBJECT" "$TO" < $EMAILMESSAGE — -f $FROM
I hope this script will be helpful for you and feel free to leave a comment if you need any help.
How to check if the file is empty by using bash script
Here’s the simple script to check if the file is empty by using bash shell.
#!/bin/bash
TEMPFILE=”$REPORTPATH/testfile”
if [ -s $TEMPFILE ]; then
echo “File is not empty”
else
echo “File is empty”
fi
Of course you can substitute anything you desire in those echo lines.
You can upgrade your Google account up to 16TB
Google has really changed the webmail ecosystem since they launched Gmail five years ago. Remember the day when Hotmail offers only 2MB? Now free Gmail is offering over 7GB. But if this is not enough for you, for merely 5 bucks a year, you can get extra 20GB.
Here is the list of plans.
If you’re excited and ready to purchase, just click the source link. Just to remind you that if you do upgrade, you can use that extra space not only in Gmail, also in the rest of Google products including Google Docs, Picasa Web Albums.
Source : Google Purchase Storage
How to delete every other row in excel
If you want to delete every other row in Excel, you’ll need to use the following macro.
- On the Tools menu, point to Macro, and then click Visual Basic Editor.
- For Excel 2007, click Visual Basic in the Code group on the Developer tab.
- On the Insert menu, click Module.
- Then copy paste the following code :
Sub Delete_Every_Other_Row() ' Dimension variables. Y = False ' Change this to True if you want to ' delete rows 1, 3, 5, and so on. I = 1 Set xRng = Selection ' Loop once for every row in the selection. For xCounter = 1 To xRng.Rows.Count ' If Y is True, then... If Y = True Then ' ...delete an entire row of cells. xRng.Cells(I).EntireRow.Delete ' Otherwise... Else ' ...increment I by one so we can cycle through range. I = I + 1 End If ' If Y is True, make it False; if Y is False, make it True. Y = Not Y Next xCounter End Sub
- Switch to the worksheet that contains the data, and then select the cell that you want to delete.
- To run the macro, point to Macro on the Tools menu, and then click Macros. (For Excel 2007, click Macros in the Code group on the Developer tab.)
- Select the Delete_Every_Other_Row macro, and then click Run.
Hopefully, future Excel versions will have built-in functions to delete every other row.
Source : Microsoft KB
Recent Posts
Recent Comments
Tags
Archives
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- November 2009
- July 2009
- May 2009
- April 2009
- January 2009
- October 2008
- September 2008
- August 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- November 2006
- February 2006
- January 2006
- December 2005


