Find out the elapsed time of a running process #process #terminal

There are a lot of processes running on your Linux system. Here is a command that will let you know how long the process has been running:

#ps -eo "%p %c %t"|grep "sshd"

In response to the above command, you will get the following output:

2850 sshd 172-01:37:22
29532 sshd 125-09:07:10

In the above command %p is pid, %c is command and %t is elapsed time.

Clearing a log/text file #file

$ >filename

How to check the date and time the system was rebooted #terminal #reboot #security

Here is a simple command to check the system's reboot date and time:

#last reboot

reboot system boot 2.6.18-53.el5 Sat Aug 6 18:02 (8+04:45)
wtmp begins Sat Aug 6 18:02:07 2011

The command below will give you the date and time the system was booted:

#who -b

system boot 2011-08-24 09:43

Securing files #terminal #security #file

Here is a simple tip to password protect your files:

vi -x test

This command will ask for an encryption key. You have to type the key twice. Then save and quit the opened file.
Now, whenever you open this file, it will ask for that password first.

To display network statistics #netstat

netstat -s

To display all open network sockets #netstat

netstat -uta

To display the kernel routing table #netstat

netstat -rn

To display the kernel interface table #netstat

netstat -i

Cut specific logs #file #vi #cut

If you need to cut specific logs from the complete log of any application, here is a tip that will

be of help.

Open the log file in a vi editor and set the editor to display the line number:

vi server.log

:set nu

The above process will provide you the line numbers in the logs. You can then search for

the specific string and note down the line number (e.g., 550). Now, note down the last line

number by using Shift+G (e.g., 780)

sed -n 550,780p server.log > new.log

So the new only contains lines from 550 to 780.

Print a file with line numbers #file #nl

If you want a file with line numbers (say for printing), you can use the 'nl' command in Linux:

$ nl file.c

This prints the file with line numbers to standard output or this can be even redirected to a

file as shown below:

$nl file.c > output.txt

Here, output.txt will have the codes of file.c with each line having a line number.

Measuring the network throughput between two Linux systems #networking #throughput

Iperf is a tool that measures the bandwidth and the quality of a network link. It can be installed very easily on any Linux system. One host must be set as the client and the other one as the server. Make sure that iperf is installed on both systems. If it is not installed, then use your package manager to install it before trying this tip.

Now run iperf on one of the Linux systems as the server, as shown below:

linux-erv3:/home/test/Desktop # iperf -s

------------------------------------------------------------
Server listening on TCP port 5001

TCP window size: 85.3 KByte (default)
------------------------------------------------------------

Go to the second Linux system and run iperf -c as the client:

linux-6bg3:~ # iperf -c 192.168.1.100

------------------------------------------------------------
Client connecting to 192.168.1.100, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------

[ 3] local 192.168.1.109 port 39572 connected with 192.168.1.100 port 5001

^C[ ID] Interval Transfer Bandwidth

[ 3] 0.0- 6.3 sec 6.38 MBytes 8.51 Mbits/sec

By default, the iperf client connects to the iperf server on the TCP port 5001 and the bandwidth displayed by iperf is the bandwidth from the client to the server. In the above example, it is 8.51 Mbits/sec between two Linux test systems connected over a wireless network.

Find your OS and distribution name #terminal #system configuration

Here is a tip that will let you know the name of the OS, along with other details:

[root@vl-pun-blg-qa27]# lsb_release -a

LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description: CentOS release 5.5 (Final)
Release: 5.5
Codename: Final

View the contents of tar and rpm files #tar #rpm

Here are two simple commands to show you the contents of the tar and rpm files.

1. To view the content of a tar file, issue the following command:

#tar -tvf /path/to/file.tar

2. To view the content of an rpm file, use the command given below:

#rpm -qlp /path/to/file.rpm

Find your MySQL configuration file #mysql #grep #file

We often have to administer a system that has been set up by someone else. In such a situation, it's difficult to find the correct configuration files for different applications. Here is a tip to find the correct configuration file for MySQL:

mysql -? | grep ".cnf”

Replacing '\n' with 'space' in each line of a file #replace #awk

You can use the awk statement given below to remove the '\n' from each line and replace it with a blank space:

awk '$1=$1' ORS=' ' /etc/passwd

Comment out hashes in large configuration files

Here is a small tip for system administrators, who need to tackle large configuration files, which include lots of commented lines (marked by #). With this tip you can remove all those hashes and provide only an uncommented configuration view for faster lookup into the file.
If you want to check the configuration file of the Squid proxy server, run the following command:

#cat squid.conf | egrep -v ^#

This will show only lines that do not start with a hash mark, thus giving the configuration parameter that is being used in the current set-up.

Run a linux command after every reboot

This tip allows you to run any Linux command or script just after system reboot. You can use the @reboot cron keyword.
If you have a script in your /home directory and it needs to be run on every boot, open the cron file in editable mode and add the following:

$crontab -e

@reboot /home/xyz/myscript.sh

Do remember to enable crond on boot.

Connecting to a remote Linux Machine

 
If you want to execute any command or script on a remote Linux machine, you can use ssh. Below are a few examples.

The syntax for running a command or script on a remote server is:
ssh [USER]@[IP] [command or script]

Let us look at how this can be done. Suppose you want to display the directory contents of /root of a remote host, you can run the following command:

[user@ubunu]$ ssh root@192.168.0.128 ls -l /root
root@192.168.0.128's password:
total 12

drwxr-xr-x. 2 root root 4096 Apr 12 00:05 a_folder

drwxr-xr-x. 2 root root 4096 Apr 12 01:31 b_folder

drwxr-xr-x. 2 root root 4096 Apr 12 01:32 c_folder