Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

Mar 182010
 

Save a file in vim without the needed permissions

Answer:

Very often, you opened a file using vim, edited it and find out that the file is opened as read only, i.e. you don't have permission to make any change(s).

What will you do? Simplest method is to save it into a temp file and rename it afterward.

However, there exist a simple method that allow you to save the file anyway.

Instead of using :wq, you enter the following.

:w !sudo tee %

That is what you need.

Mar 162010
 

Check what Linux kernel modules are loaded

Answer:

To find out what Linux kernel modules are loaded:

#  lsmod

Module                  Size  Used by
ipt_REJECT              3584  1
ipt_LOG                 6404  1
xt_limit                3236  2
xt_tcpudp               3616  10
xt_state                2432  6
ipt_addrtype            2912  4
ip6_tables             22576  0
nf_nat_irc              2688  0
nf_conntrack_irc        6552  1 nf_nat_irc
nf_nat_ftp              3584  0
nf_nat                 22164  2 nf_nat_irc,nf_nat_ftp
nf_conntrack_ipv4      16376  8 nf_nat
nf_defrag_ipv4          2400  1 nf_conntrack_ipv4
nf_conntrack_ftp        9016  1 nf_nat_ftp
...

To find out more information for a specific module

#  modinfo ip6_tables
filename:       /lib/modules/2.6.31-14-server/kernel/net/ipv6/netfilter/ip6_tables.ko
description:    IPv6 packet filter
author:         Netfilter Core Team 
license:        GPL
srcversion:     17E938A990111BB4A2F854B
depends:        x_tables
vermagic:       2.6.31-14-server SMP mod_unload modversions
Mar 102010
 

How many worker_processes should I use in Nginx?

Answer:

In the Nginx configuration (nginx.conf), the default worker_processes is 1.

e.g.

worker_processes  1;

However, as nowadays most servers are multi-core/multi-processor machine, it is a waste to only use 1 worker process.

The recommended worker_processes is equal to the number of CPU cores, e.g. if you have a dual Quad Core server, you should set to 8.

worker_processes  8;
Mar 092010
 

Speedup sed search and replace

Answer:

If you have a very large file, sometimes you can speed up sed by using the following method:

Original method:

# sed 's/foo/bar/g' filename

Optimized method:

# sed '/foo/ s/foo/bar/g' filename

The second method is faster since substitution is only performed when the search string is found.