Linux Tricks

31)Serve the current directory at http://<server ip address>:8000/ on /centos/rhel/fedora/ubuntu.

[root@server1 ~]# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

Now Open Browser and enter IP address of system with port no., refer screen shot.

python

If your firewall on allow 8000 port on it (For centos/rhel/fedora/ systems).

[root@server1 ~]# firewall-cmd --permanent --add-port=8000/tcp
success
[root@server1 ~]# firewall-cmd --reload
success
[root@server1 ~]#

And for Ubuntu systems firewall.

[gaurav@client3 ~]# sudo ufw allow 8000
[gaurav@client3 ~]#

To use a different port, specify it as the next argument:

# python -m SimpleHTTPServer 8080

32) Save a file you edited in vim without the needed permissions.

 :w !sudo tee %

33) Find the last command that begins with “cd” but avoid running it.

[root@server1 ~]#!cd:p
cd /etc/vsftpd/vsftpd.conf
[root@server1 ~]#

34) Create a new file.

# > file.txt

[root@server1 ~]# ls
Readme.txt  test  zombie
[root@server1 ~]# > file1
[root@server1 ~]#ls
Readme.txt  test  zombie  file1

35) Reset terminal.

#reset

36) Shows currently mounted file systems beautifully.

First mounted filesystem list looks without “column -t” command:
#mount

[root@server1 ~]$ mount
/dev/cciss/c0d0p5 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/mapper/ERPLVM01-LogVol00 on /home type ext3 (rw)

now try with “column -t” command:

#mount | column -t

[root@server1 ~]$ mount | column -t
/dev/cciss/c0d0p5                      on  /                         type  ext3         (rw)
proc                                   on  /proc                     type  proc         (rw)
sysfs                                  on  /sys                      type  sysfs        (rw)
devpts                                 on  /dev/pts                  type  devpts       (rw,gid=5,mode=620)
/dev/mapper/ERPLVM01-LogVol00          on  /home                     type  ext3         (rw)

37) Shutdown a Windows machine from Linux Machine.

#net rpc shutdown -I IP_ADDRESS -U username%password

If you got “net: command not found” error, then install “samba-common” package.

[gaurav@server1 ~]$ net rpc shutdown -r -I 10.88.12.206 -U administrator%Window12%64

To reboot a machine use the -r switch to net rpc:

#net rpc shutdown -r -I IP_ADDRESS -U username%password

38) List 10 most often used commands.

# history | awk ‘{a[$2]++}END{for(i in a){print a[i] ” ” i}}’ | sort -rn | head

[gaurav@server1 ~]$history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
35 su
32 cd
21 ls
19 exit
16 cat
13 ps
12 vim
8 crontab
5 source
4 rpm

39) Find out which programs listen on which TCP ports.

#netstat -tlnp Where, “t” Used to list information about TCP sockets. “l” used to list information about listening sockets. “n”  not to do reverse lookups on the IPs.“p” print the PID and name of the program.

[gaurav@server1 ~]$ netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      -

40) Execute a command from the current shell.

#(cd /tmp && ls)

[gaurav@server1 ~]$ (cd /tmp && ls)
failure
i4jdaemon__openfire_openfire_bin_openfire
secure.seek
[gaurav@server1 ~]$