Rss

Top 5 Linux Commands of 2013

Run the last command as root

sudo !!

Useful when you forget to use sudo for a command. “!!” grabs the last run command.

Serve current directory tree at http://$HOSTNAME:8000/

python -m SimpleHTTPServer

 

Save a file you edited in vim without the needed permissions

:w !sudo tee %

I often forget to sudo before editing a file I don’t have write permissions on.When you come to save that file and get the infamous “E212: Can’t open file forwriting”, just issue that vim command in order to save the file without the need tosave it to a temp file and then copy it back again.

change to the previous working directory

cd -

Runs previous command but replacing

^foo^bar

Really useful for when you have a typo in a previous command. Also, arguments default to empty so if you accidentally run:

echo "no typozs"

you can correct it with

^z

Linux / Unix: curl Command Pass Host Headers

Share

How do you send a header to your Web server on a Apple OS X or Unix or Linux based system using a curl command line option for testing and debugging my web apps or server nodes behind a load balancer?

The curl command supports -H or --headeroption to pass extra HTTP header to use when getting a web page from your web server. You may specify any number of extra headers. When you add a custom header that has the same name as one of the internal ones curl would use, your externally set header will be used instead of the internal one. The syntax is:

 
curl -H 'YOUR-EXTRA-HEDER-HERE' apache-server-ip
curl -H 'YOUR-EXTRA-HEDER-1-HERE' -H 'YOUR-EXTRA-HEDER-2-HERE' blog.urfix.com

 

For example, send Host header to blog.urfix.com to get HTML response from 75.126.153.206:80, run:

 
curl -H 'Host: blog.urfix.com' 71.201.204.10:80

This is also useful when 75.126.153.206 has multiple virtual host set. The default is not to response anything when virtual host header is not sent:
$ curl -I 75.126.153.206:80
Sample outputs:

HTTP/1.1 200 OK
Date: Sat, 03 Nov 2012 20:09:20 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.4
Expires: Sat, 27 Oct 2012 20:09:20 GMT
Last-Modified: Sat, 03 Nov 2012 20:09:20 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Pingback: http://blog.urfix.com/xmlrpc.php
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8

Now, sent blog.urfix.com as Host header:
$ curl -I -H 'Host: blog.urfix.com' curl -H 'Host: blog.urfix.com' 71.201.204.10:80
Sample outputs:

HTTP/1.1 200 OK
Date: Sat, 03 Nov 2012 20:13:22 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.4
Expires: Sat, 27 Oct 2012 20:13:22 GMT
Last-Modified: Sat, 03 Nov 2012 20:13:22 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Pingback: http://blog.urfix.com/xmlrpc.php
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8

You can use this command to test Apache server node behind a load balancer (only work with your own VLAN/LAN setup):

 
## see if 192.168.1.11:95 apache node #1 is working or not ##
curl -I --header 'Host: blog.urfix.com' 'http://192.168.1.11:95/'

Sample outputs:

HTTP/1.1 200 OK
X-Whom: urfix.com
Vary: Cookie
Last-Modified: Wed, 31 Oct 2012 18:54:00 GMT
Cache-Control: max-age=77, must-revalidate
Content-type: text/html
Date: Wed, 31 Oct 2012 18:57:43 GMT
Server: lighttpd

This option can be used multiple times to add/replace/remove multiple headers:

 
curl blog.urfix.com \
-H "Accept-Language: en" \
-H "Host blog.urfix.com" \
-H "User-Agent: curl"
References

See curl command man page for more options:
$ man curl