Browsing articles in "Linux"
Nov 16, 2011
jason

Simple ls command to list only files

I’ve been looking for ls option to hide directory. After a few googling, most people suggested quite complicated commands. Finally, I’ve found the really very simple command for this simple task from Devshed forums.

Here it is. You just need to ignore “/” from ls output.

ls | grep -v "/"

Source : Devshed forums

Oct 30, 2011
jason

How to install sshd and config for authentication with private key only

Once the system is up and running, the first thing for most linux system admin to do is to install sshd and configure authentication. Nowadays, most IT companies will not allow password based authentication anymore. Most coporate environment favours ssh key based authentication. Of course, there are hundreds of different methods to do this. Here is one of my favourite and simplest guide.

I’ve assumed you use Debian based linux and logon under root. If you are using other linux, change the command to yum or whatever package management system you used.

  • First install ssh server.
  • ~$apt-get install openssh-server

  • Edit sshdconfig file
  • ~$vi /etc/ssh/sshd_config

  • Disable password authentication by putting the following line
  • PasswordAuthentication no

  • For keys file location, I prefer to put in file by user name
  • AuthorizedKeysFile /etc/ssh/keys/%u

  • Save the file
  • Copy the public key of the users and put in /etc/ssh/keys/
  • Restart the ssh demon by entering the following command
  • /etc/init.d/ssh restart

That’s it! Your users need to use their private key to login to the server from now on.

Source : OpenBSD

Oct 29, 2011
jason

How to add users into sudoers

First of all, you need to have sudo package installed. You can check if your system have by typing visudo command.

root@SUNNYVALE-133:~# visudo
-su: /usr/sbin/visudo: No such file or directory

If you receive the above error, that means you don’t have sudo module. You can install by typing the following command:

root@SUNNYVALE-133:~# apt-get install sudo

Once you’ve got sudo package installed, you can add users to sudoers file. The following is my sample config.

  • First type visudo command
  • The add the following line under root ALL=(ALL) ALL line
    mysampleuser ALL = (ALL) ALL, NOPASSWD: ALL

That’s it! From now on, you can simple type sudo su – whenever you want to become root from your own username.

Source : Debian Wiki

Oct 28, 2011
jason

How to resolve “Can’t locate DBD/Pg.pm” error

I was having the following perl module missing error.

root@SUNNYVALE-133:/# ./myscript.pl
Can’t locate DBD/Pg.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.1 /usr/local/share/perl/5.10.1 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at ./myscript.pl line 5.
BEGIN failed–compilation aborted at ./myscript.pl line 5.

After googling for a while, most websites directed me to do the following.

$cpan
cpan>install DBI
cpan>install DBD::Pg

Even after I’ve done that, I’m still having the same error message. Eventually I’ve found out that since I’m using Debian, I should rather just install the perl pg library. The following command is the one that solve my problem.

root@SUNNYVALE-133:/#apt-get install libdbd-pg-perl

I hope this would be helpful to you as well if you’re getting the same error message.

Source : Perl DBI Support

Sep 16, 2011
jason

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

Sep 15, 2011
jason

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

Sep 14, 2011
jason

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.

sudoers_screenshot

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

Sep 14, 2011
jason

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.

grub_ubuntu_screenshot

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

Sep 13, 2011
jason

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

Aug 27, 2011
Jason

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.

Pages:12»