You are here

Linux Tricks

This page contains various linux commands and tricks.
All these commands are tested in Ubuntu 12.04 LTS system. It may work on linux machines, but not verified.

Ubuntu 12.04 - Enable/Disable Laptop's Mousepad or Touchpad

To enable laptop mousepad execute

modprobe psmouse

To disable laptop mousepad execute

rmmod psmouse
Ubuntu 14.04 LTS FTP installation

To install ftp package and restart the service.

  1. sudo apt-get install vsftpd
  2. vi /etc/vsftpd.conf
    # Uncomment this to enable any form of FTP write command.
    write_enable=YES
    local_enable=YES
  3. sudo service vsftpd restart
Find out fingerprint of a certificate using different algorithm
  1. SHA-256
    openssl x509 -noout -fingerprint -sha256 -inform pem -in [certificate.crt]
  2. SHA-1
    openssl x509 -noout -fingerprint -sha1 -inform pem -in [certificate.crt]
  3. MD5
    openssl x509 -noout -fingerprint -md5 -inform pem -in [certificate.crt]
  4. Example: openssl x509 -noout -fingerprint -sha256 -inform pem -in ca-certificates.crt
Ubuntu 12.04 - Enable an user with root capability

To enable an user new_user with root capability you can execute

sudo usermod -aG sudo new_user
Ubuntu 12.04 - How to execute a command when system starts up

Goto init.d path rc.local file

vi /etc/init.d/rc.local

After line #! /bin/sh enter the command, save the file, reboot the system.

Ubuntu 12.04 - Kill several processes having same name

To kill several processes having same name (say apache) execute

kill -9 `ps -ef | grep apache | grep -v grep | awk '{print $2}'`
Ubuntu 12.04 - Increase maximum number of threads in system

To increase maximum number of threads in system execute

echo 500000 > /proc/sys/kernel/threads-max

This shall increase max thread counts to 500000.

Ubuntu 12.04 - Increase maximum number of memory map areas in system

To increase maximum number of memory map areas in system execute

echo 245530 > /proc/sys/vm/max_map_count

This will set maximum memory map areas to 245530

Ubuntu 12.04 - Increase maximum number of semaphores in system

To increase maximum number of semaphores in system execute

printf '500 64000 64 256' > /proc/sys/kernel/sem

Above integer parameter corresponds max_sem_per_id max_sem_total max_ops_sem_call max_sem_ids

Ubuntu 12.04 - Increase shared memory in system

To increase shared memory in system execute

printf '20096' > /proc/sys/kernel/shmmni

Though many places people have recomonded to increase shaed memory count in /proc/sys/kernel/shmmax or in /proc/sys/kernel/shmall files, what we have found is, increasing shared memory count in /proc/sys/kernel/shmmni is quite effective.

Ubuntu 12.04 - Increase stack size in system

To increase stack size in system execute

ulimit -s 4096

This limits stack size to 4MB.

Ubuntu 12.04 - Check free memory in system

Execute

free -m

The output comes as

total used free shared buffers cached
Mem: 490 419 70 0 8 48
-/+ buffers/cache: 362 127
Swap: 2047 139 1908

See the line -/+ buffers/cache: 362 127.
So used memory is 362MB and remaining free memory (in RAM) is 127MB.

Also you can see Swap: line, it looks 139MB of swap memory is used. So what it means, at one point RAM was not sufficient (as it is just 512 MB), so swap memory was used by system for some applications.

Ubuntu 12.04 - Completely remove a package from machine

To completely remove a package from system execute

apt-get --purge remove package-name

For example to remove kamailio completely from your system

apt-get --purge remove kamailio

This will completely remove the kamailio package, delete the associated configuration files from your system.

Ubuntu 12.04 - How to generate core file

Normally core will not be generated in the working directory, to enable this please follow the below steps.

  1. Go to file /etc/security/limits.conf
  2. Add/Modify one line in /etc/security/limits.conf
    * soft core unlimited
  3. Execute below commands in a terminal
    sudo ulimit -c unlimited
    sudo echo "1" /proc/sys/kernel/core_uses_pid

Thats all, you have successfully enabled the core settings. Typically core file is generated in working directory where your executables are located.

Linux - How to modify a word to a new word throughout directory and subdirectory
  1. Execute
    find ./ -type f -exec sed -i 's/192.168.1.194/100.100.100.100/g' {} \;

    Above command modifies all files present in current path. It searches 192.168.1.194 and modifies it to 100.100.100.100.
    Please note type -f is required as it applies only to files.

Linux - How to all files containing a specific string from source path to destination path

Lets say you want to move all files that contains a particular string "Host: google" from source to destination path. You can do so using below command.

grep -lir 'Host: google' /home/lm/source/* | xargs mv -t /home/lm/destination/
Ubuntu 12.04 - How to add few lines of code to *.c files recursively in a directory

Create script file with below content

ALL_FILES=`find . -type f -name "*.c"`
for id in $ALL_FILES; do
sed -i '1s/^/#ifdef _MEM_LEAK\n#include "MemoryManagerHeader.h"\n#endif\n/' $id
done

When you execute just created script file, it will add below code to all .c files recursively in directory.

#ifdef _MEM_LEAK
#include "MemoryManagerHeader.h
#endif
Steps to increase file descriptors and port range in the Linux (Ubuntu 12.04 LTS)

This section contains kernel settings for increasing file descriptors and port range. Thereby it helps to have maximum socket connections with remote servers.

  1. Go to file /etc/sysctl.conf
  2. Add one line at the end of file /etc/sysctl.conf for permanent changes.
    fs.file-max = 100000
  3. Go to file /etc/security/limits.conf
  4. Add the below lines at the end of file /etc/security/limits.conf for permanent changes.
    * hard nofile 500000
    * soft nofile 500000
    root hard nofile 500000
    root soft nofile 500000
  5. Go to file /etc/sysctl.conf
  6. Add the below line in /etc/sysctl.conf file for permanent changes.
    net.ipv4.ip_local_port_range=1024 65000
  7. Reboot your system.

Thats all, you have successfully enabled the network settings.

Find total number of thread
ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'
Steps to specify location of .so file in Linux (Ubuntu 14 LTS)

This section explains steps involved to specify location of a .so file.

  1. Go to /etc/ld.so.conf. It contains include line as below.
    include /etc/ld.so.conf.d/*.conf

    So it contains all .conf files from path /etc/ld.so.conf.d/.

  2. Next go to path /etc/ld.so.conf.d/. You will see few .conf files. You add your own .conf file say loadmultiplier.conf file is added. Add below line in loadmultiplier.conf file.
    #load multiplier default so file paths
    /usr/local/mypath/

    You can put the path as you want.Now you have to add the loadmultiplier.conf in the top of the file /etc/ld.so.conf as given below.

    include /etc/ld.so.conf.d/loadmultiplier.conf
    include /etc/ld.so.conf.d/*.conf

    Now if you will build the project then your API will link to the .so file as per loadmultiplier.conf since it will include first.It will not link to the system's .so file.

  3. Next save the file and execute command ldconfig as root.
    Thats all. Now path /usr/local/mypath/ is one of the default library path. You can keep your .so files here and while linking/executing this path will be referred by system in addition to other default paths as mentioned in other .conf files in /etc/ld.so.conf.d/.
Ubuntu 14 - Reduce SSH connect time

This section explains steps involved to reduce SSH connect time to SSH server.

  1. In SSH server machine, go to /etc/update-motd.d directory
  2. Make a temporary folder say tmp
  3. Move 90-updates-available file to temporary folder tmp

That's it. Now you can do SSH quite fast.

Ubuntu 14 - Steps to correct broken system (Ubuntu 14 LTS)
sudo apt-get update
sudo apt-get clean
sudo apt-get autoremove
sudo apt-get update && sudo apt-get upgrade
sudo dpkg --configure -a
sudo apt-get install -f