Globally replace a placeholder with sed

sed -i [replace command] file


sed -i s/MINISHIFT_IP/ my-config-file.yml

You can even do:

sed -i s/MINISHIFT_IP/$(minishift ip)/g my-config-file.yml

More info about sed:


Pretty printing json in Bash + gzipped response with curl

To decompress response in curl add: --compressed to curl command.

To pretty print JSON response, pipe the result to: python -m json.tool

All in one line:

$ curl --compressed "http://someUrl"  | python -m json.tool

/sbin/ip route add … RTNETLINK answers: File exists

When starting opevpn I encountered strange issue. VPN was connecting but I could not access services behind it.
What I found in its log was:

Thu Oct 16 12:15:50 2014 /sbin/ip route add via
RTNETLINK answers: File exists

It turned out the newly added route was conflicting with other route (a leftover from messing up vpn-things).

First, I removed the conflicting rule:

sudo route del -net netmask

Then I manually added the route the vpn was trying to add (but I think it’s optional and simply restarting the VPN would do the thing)

/sbin/ip route add via


Starting on system startup and respawning apps with upstart in ubuntu


Following is an example config file for starting app on system startup and auto-respawning.
File name should be put in /etc/init, its name should end with .conf

The name of the task is derived from file name. For file name taskName.conf, the task is called taskName.

start on runlevel [2345]

description "your app description"

# Start a job as username, a member of usergroup
setuid username
setgid usergroup

# set required environments (what you normally do with export VARIABLE=VALUE)
env ENV_TO_SET = environment\ value

# run the app
exec /home/myuser/bin/myapp

# Restart the process if it dies with a signal
# or exit code not given by the 'normal exit' stanza.

# Give up if restart occurs 10 times in 90 seconds.
respawn limit 10 90


To start a task run:

$ sudo start taskName

And to stop it:

$ sudo stop taskName

More info:


When you get some strange errors reading files, you might need to add an env which defines locale, e.g.:

env LC_ALL=pl_PL.UTF-8

For running java apps, remember to specify JAVA_HOME


setting up iptables to forward 80 (http) and 443 (https) to other ports

If you want to serve http content from application run by a non-root user, you can set up iptables to forward http and https ports to “user space ports”.

Setting up

Assuming your app uses port 8080 for http and 8081 for https, configure iptables as follows:

$ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
$ sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8081

Saving and restoring

Test the configuration and if it works save it, e.g. with:

sudo iptables-save > /etc/iptables/rules

Now you have to add reading iptables configuration on system startup.
If your network is configured manually you can add iptables-restore < /etc/iptables/rules to your /etc/network/interfaces:

auto eth0
iface eth0 inet dhcp
        pre-up iptables-restore < /etc/iptables/rules

If your /etc/network/interfaces is generated automatically, you can create a new file in /etc/network/if-pre-up.d/ with following content:


iptables-restore < /etc/iptables/rules
exit 0

ssh tunneling to access restricted resources

To access a resource from restricted area (let the resource be: when you can ssh to a gateway (gatewayHost):

ssh login@gatewayHost -L 8013:restrictedUrl:443

Now you can access restrictedUrl:443 on localhost:8013 on your pc.