Remove line numbers from file (NOT lines)

Consider you have a file named t.txt which has below content.

root@localhost:/home/# cat t.txt
1 hjjhhjh3klk 5 ;k;lk;k
2 kjhjkhjkh656 iuyiuy2
3 jghjhghgjh

Notice the line numbers at the starting of each line.We will remove the line numbers but keep the lines intact.
It can be done by below command

root@localhost:/home/# sed -i s'/ *[0-9]* .//' t.txt

Above command will remove the line numbers.Here’s how the file looks after that.

root@localhost:/home/# cat t.txt
hjjhhjh3klk 5 ;k;lk;k
kjhjkhjkh656 iuyiuy2
jghjhghgjh

Reset root password in Linux

Steps below will show how to reset root password in debian like machines.

1. Boot the machine and enter in GRUB menu.
2. Press e to go in edit mode.
3. Scroll down to the line starting with kernel.
4. Go to the end of the line and press space bar and type linux single.Press space bar again and type init=/bin/bash
5. Press ctrl X to boot.
6. After machine is booted.type below command.
mount -rw -o remount /
7. Type passwd command to change root password.
8. Reboot

Remote desktop to Ubuntu 12.04

If you want to connect to Ubuntu machine through remote desktop, xrdp can be used.Below are the steps.

Install xrdp

sudo apt-get install xrdp

Configure xrdp for user e.g sysadmin.Change directory to sysadmin’s home.

sysadmin@server02:~$ cp .Xauthority .xsession
sysadmin@server02:~$ > .xsession

Run the command below to add the entry.

sysadmin@server02:~$ echo "gnome-session --session=ubuntu-2d" > .xsession

Restart xrdp

sudo service xrdp restart

Now you should be able to connect through rdp clients such as krdc.

NOTE: The steps mentioned above does not work with ubuntu 12.10.you need to install xfce4 desktop in higher versions.

Show server processes with netstat

netstat is a powerful tool to troubleshoot issues with network connections, routing tables, interface statistics etc.It can be also used to show currently running processes(server) with PID detail.Below is the command with example.

netstat -plntu

Output:
Active Internet connections (only servers)
Proto Recv-Q   Send-Q   Local Address    Foreign Address State      PID/Program
tcp   0        0        0.0.0.0:22       0.0.0.0:*       LISTEN     543/sshd
tcp   0        0        127.0.0.1:631    0.0.0.0:*       LISTEN     961/cupsd
tcp   0        0        0.0.0.0:37121    0.0.0.0:*       LISTEN     985/rpc.statd
tcp   0        0        0.0.0.0:389      0.0.0.0:*       LISTEN     1230/slapd
tcp   0        0        0.0.0.0:40011    0.0.0.0:*       LISTEN     -
tcp   0        0        0.0.0.0:111      0.0.0.0:*       LISTEN     947/rpcbind
tcp   0        0        0.0.0.0:80       0.0.0.0:*       LISTEN     1295/nginx
tcp6  0        0        :::49941         :::*            LISTEN     985/rpc.statd
tcp6  0        0        :::22            :::*            LISTEN     543/sshd

Restrict git push to master branch

Like other version control system git also provide hook facilities to perform customized task.hooks are basically scripts which can be written in shell, perl, python, ruby etc.

In order to restrict git push to master(or other specific branch), changes should go in repo.git/hooks/update.

1.Copy update.sample script in hooks directory to update.

cp repo.git/hooks/update.sample repo.git/hooks/update

2.Empty the newly copied update file

> repo.git/hooks/update

Skip the above two steps if you are already using update hook for other purposes.

3.Copy the below script and paste in update file

#!/bin/bash

if test $USER == "git-admin"; then
echo "--------------PERMITTING PUSH FOR GIT ADMIN--------------"
else

[ "$1" != refs/heads/master ] || {
echo "--------------ERROR: $USER is NOT ALLOWED TO UPDATE MASTER--------------" >&2
exit 1
}
fi

git push should be now restricted to "git-admin" user. Others will not be able to push to master branch.

Make permanent changes in dynamic resolv.conf

Most of the time you get your DNS servers ip through DHCP in well configured network.But if you want to override the dns servers, it can be achieved by performing the steps below.

Open the file

sudo vim /etc/resolvconf/resolv.conf.d/base

Add the servers like below

nameserver 8.8.8.8
nameserver 8.8.4.4

Now update with resolvconf

sudo resolvconf -u