<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>MDLog:/sysadmin &#187; Tips &amp; Tricks</title> <atom:link href="http://www.ducea.com/category/tips-tricks/feed/" rel="self" type="application/rss+xml" /><link>http://www.ducea.com</link> <description>The Journal Of A Linux Sysadmin</description> <lastBuildDate>Mon, 06 Sep 2010 06:29:25 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Background a running process</title><link>http://www.ducea.com/2010/08/17/background-a-running-process/</link> <comments>http://www.ducea.com/2010/08/17/background-a-running-process/#comments</comments> <pubDate>Tue, 17 Aug 2010 22:17:37 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[bash]]></category> <category><![CDATA[screen]]></category> <category><![CDATA[tips]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=1110</guid> <description><![CDATA[Everyone knows and loves screen for running longtime scripts in the background without worrying that the ssh connection will drop and will have to run it again. Still, I have found myself many times in the situation where I started a process and needed to put it in the background and run something else on [...]]]></description> <content:encoded><![CDATA[<p>Everyone knows and loves <strong>screen</strong> for running longtime scripts in the background without worrying that the ssh connection will drop and will have to run it again. Still, I have found myself many times in the situation where I started a process and needed to put it in the <strong>background</strong> and run something else on the console. Uff.. <em>If only I started it with screen</em>. But wait, there is hope. This quick tip will show how to put a process in the background and then start it back in foreground.</p><p>This works in bash and uses the &#8216;<strong>suspend</strong>&#8216; key (<strong>CTRL+Z</strong>) and the <strong>bg</strong> &#8211; background and <strong>fg</strong> &#8211; foreground commands. Let&#8217;s say we were running an intensive rsync command, and are wanted to check if we still have the available space on the disk without opening a new ssh session (yes, I know):<br
/> <code>rsync -ar server:/source/ /destination/<br
/> <strong> ^Z</strong><br
/> Stopped</code></p><p>Let it run in the background:<br
/> <code><strong>bg</strong><br
/> [1] rsync -ar server:/source/ /destination/ &amp;</code></p><p>Now we can run some other commands like du:<br
/> <code>du -h</code></p><p>We can see the background process with ps or jobs:<br
/> <code><strong>jobs</strong><br
/> [1]	Running		rsync -ar server:/source/ /destination/</code></p><p>And finally we can bring it back to foreground with fg:<br
/> <code><strong>fg</strong></code></p><p><em>Note</em>: this works only on the running ssh/bash session and it will be closed once you exit. Logout should warn about open/running jobs and that they will be lost if exit.</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2010/08/17/background-a-running-process/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Multiple java versions on debian</title><link>http://www.ducea.com/2010/08/09/multiple-java-versions-on-debian/</link> <comments>http://www.ducea.com/2010/08/09/multiple-java-versions-on-debian/#comments</comments> <pubDate>Mon, 09 Aug 2010 23:56:14 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Debian]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[java]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=1098</guid> <description><![CDATA[Debian has a nice way to handle multiple java installations on a the same machine. Let&#8217;s say that for some reason you want to have sun-java 1.5 and also 1.6 installed on the server, we can easily configure the default one with the update-java-alternatives command (part of the java-common package). Here is how it can [...]]]></description> <content:encoded><![CDATA[<p>Debian has a nice way to handle <strong>multiple java installations</strong> on a the same machine. Let&#8217;s say that for some reason you want to have <em>sun-java 1.5</em> and also <em>1.6</em> installed on the server, we can easily configure the default one with the <strong>update-java-alternatives</strong> command (part of the <em>java-common</em> package). Here is how it can be used:</p><p>To see what versions of java we have installed on the system (from debian packages):<br
/> <code>update-java-alternatives -l<br
/> java-1.5.0-sun 53 /usr/lib/jvm/java-1.5.0-sun<br
/> java-6-sun 63 /usr/lib/jvm/java-6-sun</code></p><p>We can see that the default version is 1.6 in my case (as it was the last installed):<br
/> <code>java -version<br
/> java version "1.6.0_20"<br
/> Java(TM) SE Runtime Environment (build 1.6.0_20-b02)<br
/> Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)</code></p><p>We can change the default version with: update-java-alternatives &#8211;jre -s &lt;ver&gt; , like:<br
/> <code><strong>update-java-alternatives</strong> --jre -s java-1.5.0-sun</code><br
/> and now the default is 1.5:<br
/> <code>java -version<br
/> java version "1.5.0_22"<br
/> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)<br
/> Java HotSpot(TM) Client VM (build 1.5.0_22-b03, mixed mode, sharing)</code></p><p>This is quite handy if you need to have multiple java versions installed, and need a quick way to change the default one (you can access any of them directly from their own path of course).</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2010/08/09/multiple-java-versions-on-debian/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Using instance-specific metadata in Eucalyptus</title><link>http://www.ducea.com/2009/08/17/using-instance-specific-metadata-in-eucalyptus/</link> <comments>http://www.ducea.com/2009/08/17/using-instance-specific-metadata-in-eucalyptus/#comments</comments> <pubDate>Mon, 17 Aug 2009 12:22:48 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Cloud Computing]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[amazon]]></category> <category><![CDATA[ami]]></category> <category><![CDATA[aws]]></category> <category><![CDATA[ec2]]></category> <category><![CDATA[emi]]></category> <category><![CDATA[eucalyptus]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=977</guid> <description><![CDATA[One of the great features of Amazon EC2 is the possibility to dynamically query and use instance specific metadata, or even custom data. This can be useful for various reasons, and the greatest advantage I&#8217;ve personally seen into this, is the possibility to allow the instance to have some information on how to configure itself [...]]]></description> <content:encoded><![CDATA[<p>One of the great features of <strong>Amazon EC2</strong> is the possibility to <em>dynamically query and use <strong>instance specific metadata</strong></em>, or even <strong><em>custom data</em></strong>. This can be useful for various reasons, and the greatest advantage I&#8217;ve personally seen into this, is the possibility to allow the instance to have some information on how to configure itself when first booting (using chef or puppet, or some other configuration management tool).</p><p>The <a
href="http://docs.amazonwebservices.com/AWSEC2/2007-03-01/DeveloperGuide/AESDG-chapter-instancedata.html" target="_blank">Amazon documentation</a> explains how to get this information, basically just by using simple <em>http get requests</em> on the ip: <strong>169.254.169.254</strong>, like for ex (for the metadata index):<br
/> <code>curl http://169.254.169.254/latest/meta-data/</code><br
/> or for the custom data:<br
/> <code>curl http://169.254.169.254/latest/user-data</code></p><p><a
href="http://www.eucalyptus.com/" target="_blank"><strong>Eucalyptus</strong></a> supports this great feature (starting with<strong> v1.4</strong>), but we obviously need to target a different ip to retrieve this information (as the amazon ip has nothing to do with our internal cloud <img
src='http://www.ducea.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). We need to use the <strong>cloud controller IP</strong> for the request and the port it is bound (by default <strong>8773</strong> if you have not changed it). This will look like this (you need to run it from inside the actual instance):<span
id="more-977"></span><br
/> <code>curl <strong>http://&lt;CC_IP&gt;:8773/latest/meta-data</strong><br
/> block-device-mapping/<br
/> security-groups<br
/> ami-manifest-path<br
/> ancestor-ami-ids<br
/> public-keys/<br
/> reservation-id<br
/> ramdisk-id<br
/> public-keys/0/<br
/> ami-launch-index<br
/> kernel-id<br
/> instance-type<br
/> local-hostname<br
/> local-ipv4<br
/> hostname<br
/> product-codes<br
/> public-ipv4<br
/> instance-id<br
/> public-hostname<br
/> ami-id<br
/> placement/</code></p><p>For the <strong>user data</strong> we can start the eucalyptus instance with:<br
/> <code>ec2-run-instances &lt;EMI&gt; <strong>-d "myhostname"</strong> ...other params...</code><br
/> and later we can then retrieve that information from inside the instance using:<br
/> <code>curl <strong>http://&lt;CC_IP&gt;:8773/latest/user-data</strong><br
/> myhostname</code></p><p>While the <a
href="http://open.eucalyptus.com/" target="_blank">documentation for Eucalyptus</a> is getting better with every new version there are still missing parts like this one, and hopefully people looking for this information will find this article useful.</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/08/17/using-instance-specific-metadata-in-eucalyptus/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Running s3sync in parallel</title><link>http://www.ducea.com/2009/08/12/running-s3sync-in-parallel/</link> <comments>http://www.ducea.com/2009/08/12/running-s3sync-in-parallel/#comments</comments> <pubDate>Wed, 12 Aug 2009 15:00:41 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[amazon]]></category> <category><![CDATA[aws]]></category> <category><![CDATA[s3]]></category> <category><![CDATA[s3sync]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=972</guid> <description><![CDATA[s3sync is a great tool to synchronize local data with Amazon S3 for backups, or whatever other reasons you might want to put your data on S3. It is very simple to install (gem install s3sync) and use (s3sync -v -s -r &#8211;progress &#60;source_dir&#62; s3_bucket:&#60;dir&#62;); it runs very well and it can be easily scripted [...]]]></description> <content:encoded><![CDATA[<p><a
href="http://s3sync.net/" target="_blank"><strong>s3sync</strong></a> is a great tool to synchronize local data with <a
href="http://aws.amazon.com/s3" target="_blank"><strong>Amazon S3</strong></a> for backups, or whatever other reasons you might want to put your data on S3. It is very simple to install (<em>gem install s3sync</em>) and use (<em>s3sync -v -s -r &#8211;progress &lt;source_dir&gt; s3_bucket:&lt;dir&gt;</em>); it runs very well and it can be easily scripted to do regular backups or even synchronize live data with S3. The only problem I found while using s3sync was that it can be very <strong>slow</strong> when uploading a lot of data (millions of files) to S3; this because the process is slow but also because it runs a single file at a time, and it doesn&#8217;t do several uploads in <strong>parallel</strong>. I would have loved for s3sync to do this out of the box, but <strong>unfortunately it doesn&#8217;t</strong>, but for my particular need I was able to do this by <em>running more s3sync commands a the same time</em>. It will not apply to your data (unless it is structured the same way as here; very unlikely), but it might give an idea on how you could do this your own data if it is structured in a feasible way.</p><p><span
id="more-972"></span>Ok, for this particular upload I am sync&#8217;ing a few millions files in folders structured like this:<br
/> <em>000/000/files..<br
/> 000/001/files..<br
/> &#8230;<br
/> 999/999/file&#8230;</em><br
/> the process was taking days with a single s3sync running, so I just put up a small script to run several toplevel folder s3sync&#8217;s at the same time. This reduced the time a lot and was a good walkaround for our problem. Here is the script used, in case it might help others:</p><pre><code>#!/bin/bash

cd /source_top_folder

id=0
while [  $id -lt 999 ]; do
        sleep 10
        echo "."
        running=$(ps -ef | grep s3sync | grep ruby |wc -l)
        if [ $running -lt 20 ]; then
                lid=`printf "%03d" $id`
                echo "starting a new s3sync - $lid"
                /usr/bin/s3sync -p --no-md5 -v -s -r --progress --delete ./$lid/ my_bucket:$lid/ &amp;
                let id=id+1
        fi
done</code></pre><p>This will basically run 20 s3sync instances and start a new one everytime it is needed (if total running go bellow 20). I realize this is not perfect, but it has done its job for us for this particular project. Ideally s3sync would be able to run several parallel upload threads to be much faster, but until then you might use a similar solution if you have such a problem <img
src='http://www.ducea.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/08/12/running-s3sync-in-parallel/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Apache2 umask</title><link>http://www.ducea.com/2009/08/03/apache2-umask/</link> <comments>http://www.ducea.com/2009/08/03/apache2-umask/#comments</comments> <pubDate>Mon, 03 Aug 2009 23:21:49 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Centos]]></category> <category><![CDATA[Debian]]></category> <category><![CDATA[Fedora]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[RHEL]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[apache]]></category> <category><![CDATA[apache-tips-and-tricks]]></category> <category><![CDATA[tips]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=951</guid> <description><![CDATA[Many times you might want to fine tune the default permissions of the files created on a linux system. This is very simple and usually if you are using bash all you have to do is to define somewhere in the bash startup files (/etc/profile is a good place for this) a new value for [...]]]></description> <content:encoded><![CDATA[<p>Many times you might want to fine tune the <strong>default permissions</strong> of the files created on a linux system. This is very simple and usually if you are using <strong>bash</strong> all you have to do is to define somewhere in the bash startup files (<em>/etc/profile</em> is a good place for this) a new value for <strong>umask</strong> like this:<br
/> <code>umask 002</code><br
/> (this will allow by default group write permissions on the newly created files)</p><p>Normally on modern linux distributions this is by <em>default set to 022</em> and you can easily find out what it is on your system by running the umask command:<br
/> <code>umask</code></p><p>Contrary to what you might think, this is not enough to have this working for all applications and daemons on the system. This works fine for any files created from a shell session, but the files created by other processes, like the web server for example, will still use the default, unless otherwise configured. In order to have <strong>apache</strong> use a <strong>different umask</strong> we can define this inside <strong>/etc/apache2/envvars</strong> (debian, and ubuntu systems) or /etc/sysconfig/httpd (rhel,centos systems) like this:<br
/> <code>umask 002</code><br
/> and restart apache to enable it.</p><p>Other daemons will have different locations where you can define this to overwrite the default setting for umask (check their documentation if you are unsure).</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/08/03/apache2-umask/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Linux Tips: get the list of subdirectories with their owner &amp; permissions and full paths</title><link>http://www.ducea.com/2009/06/05/linux-tips-get-the-list-of-subdirectories-with-their-owner-permissions-and-full-paths/</link> <comments>http://www.ducea.com/2009/06/05/linux-tips-get-the-list-of-subdirectories-with-their-owner-permissions-and-full-paths/#comments</comments> <pubDate>Fri, 05 Jun 2009 12:39:01 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[tips]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=910</guid> <description><![CDATA[I needed to get a list of all the subdirectories that were owner by some other user than root under /var and their permissions/owner with full paths. My first thought was to use ls and something like this: ls -dlR */ drwxr-xr-x  2 root root  4096 2009-06-05 06:25 backups/ drwxr-xr-x  8 root root  4096 2009-05-11 [...]]]></description> <content:encoded><![CDATA[<p>I needed to get a list of <em>all the subdirectories that were owner by some other user than root</em> under /var and their permissions/owner with full paths. My first thought was to use <strong>ls</strong> and something like this:<br
/> <code><strong>ls -dlR */</strong><br
/> drwxr-xr-x  2 root root  4096 2009-06-05 06:25 backups/<br
/> drwxr-xr-x  8 root root  4096 2009-05-11 06:02 cache/<br
/> drwxr-xr-x  2 root root  4096 2009-05-06 04:49 ec2/<br
/> drwxr-xr-x 25 root root  4096 2009-05-25 14:55 lib/<br
/> ...</code><br
/> will show the subdirectories just as I needed but only at one level. Using */*/ would show the next level, etc. This obviously is not a solution and unfortunately I had found no other way to do this with ls. Using:<br
/> <code><strong>ls -alR | grep ^d</strong><br
/> drwxr-xr-x 15 root root  4096 2009-05-11 06:02 .<br
/> drwxr-xr-x 22 root root  4096 2009-06-03 15:02 ..<br
/> drwxr-xr-x  2 root root  4096 2009-06-05 06:25 backups<br
/> drwxr-xr-x  8 root root  4096 2009-05-11 06:02 cache<br
/> drwxr-xr-x  2 root root  4096 2009-05-06 04:49 ec2<br
/> drwxr-xr-x 25 root root  4096 2009-05-25 14:55 lib<br
/> ....</code><br
/> works somehow, but since I don&#8217;t have the full paths this is useless.</p><p><span
id="more-910"></span>Maybe this can be done with ls, but since I have not found a way to do this, I turned to <strong>find</strong>. Find allows me very simple to get the list of subdirectories with their full paths:<br
/> <code><strong>find /var -type d</strong><br
/> /var<br
/> /var/backups<br
/> /var/lib<br
/> /var/lib/ucf<br
/> /var/lib/ucf/cache<br
/> /var/lib/vim<br
/> /var/lib/vim/addons<br
/> /var/lib/php5<br
/> /var/lib/iptraf<br
/> /var/lib/mysql-cluster<br
/> /var/lib/collectd<br
/> ...</code><br
/> and to get also the owner/permissions we can get help from ls:<br
/> <code><strong>ls -dl `find /var -type d`</strong><br
/> drwxr-xr-x 15 root     root      4096 2009-05-11 06:02 /var<br
/> drwxr-xr-x  2 root     root      4096 2009-06-05 06:25 /var/backups<br
/> drwxr-xr-x  8 root     root      4096 2009-05-11 06:02 /var/cache<br
/> drwxr-xr-x  3 www-data www-data  4096 2009-05-11 06:02 /var/cache/apache2<br
/> drwxr-xr-x  2 www-data www-data  4096 2008-09-08 05:08 /var/cache/apache2/mod_disk_cache<br
/> drwxr-xr-x  3 root     root      4096 2009-06-03 09:32 /var/cache/apt<br
/> drwxr-xr-x  3 root     root      4096 2009-06-03 09:32 /var/cache/apt/archives<br
/> drwxr-xr-x  2 root     root      4096 2009-06-03 09:32 /var/cache/apt/archives/partial<br
/> drwxr-xr-x  2 root     root      4096 2009-06-05 06:25 /var/cache/apt-show-versions<br
/> drwxr-xr-x  2 root     root      4096 2009-06-03 09:32 /var/cache/debconf<br
/> drwxr-xr-x  2 root     root      4096 2009-06-05 06:25 /var/cache/locate<br
/> drwxr-sr-x 42 man      root      4096 2009-06-05 06:25 /var/cache/man<br
/> ...</code></p><p>And finally the oneliner that gives us all the folders that are owned by some other user as root:<br
/> <code><strong>ls -dl `find /var -type d` | grep -v root</strong><br
/> drwxr-xr-x  3 www-data www-data  4096 2009-05-11 06:02 /var/cache/apache2<br
/> drwxr-xr-x  2 www-data www-data  4096 2008-09-08 05:08 /var/cache/apache2/mod_disk_cache<br
/> drwxr-s---  2 mysql    adm       4096 2009-06-05 06:25 /var/log/mysql<br
/> drwxr-sr-x  2 news     news      4096 2009-05-06 04:49 /var/log/news<br
/> </code></p><p>time for <strong>awk </strong>now <img
src='http://www.ducea.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>Update: as per the comment bellow from <strong>chlovechek</strong> a much cleaner solution is:<br
/> <code><strong>find /var ! -user root -type d -ls</strong></code></p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/06/05/linux-tips-get-the-list-of-subdirectories-with-their-owner-permissions-and-full-paths/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>HowTo update DNS hostnames automatically for your Amazon EC2 instances</title><link>http://www.ducea.com/2009/06/01/howto-update-dns-hostnames-automatically-for-your-amazon-ec2-instances/</link> <comments>http://www.ducea.com/2009/06/01/howto-update-dns-hostnames-automatically-for-your-amazon-ec2-instances/#comments</comments> <pubDate>Mon, 01 Jun 2009 12:46:12 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Cloud Computing]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[amazon]]></category> <category><![CDATA[ami]]></category> <category><![CDATA[aws]]></category> <category><![CDATA[dns]]></category> <category><![CDATA[dnsutils]]></category> <category><![CDATA[ec2]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=887</guid> <description><![CDATA[A while ago one of the major problems people faced to use Amazon EC2 into production environments was the dynamic state of the instances IPs. Every time one instance was started it was getting a new, dynamic IP. This has been addressed with the introduction of Amazon Elastic IP Addresses, but even when using this, [...]]]></description> <content:encoded><![CDATA[<p>A while ago one of the major problems people faced to use <a
href="http://aws.amazon.com/ec2/" target="_blank"><strong>Amazon EC2</strong></a> into production environments was the dynamic state of the instances IPs. Every time one instance was started it was getting a new, <strong>dynamic IP</strong>. This has been addressed with the introduction of <em>Amazon Elastic IP Addresses</em>, but even when using this, the <strong>private IPs are still dynamic</strong> and most of the time people will want to communicate between several instances on the private allocated IPs and not on the public ones. This article will show <em>how you can easily automate the process to update DNS hostnames for your EC2 instances</em>, by adding to the AMI&#8217;s the logic for this. I will use for this a master DNS server running bind9, but this can be adapted to any other DNS server.<span
id="more-887"></span></p><h3>How to get the needed information (IPs, hostnames, etc.)</h3><p>Amazon api provides us all the needed information. Any EC2 instance can get a lot of information <em>about itself</em> just by querying a web server using a REST-like API. Here is how we can get all the available <strong>metadata </strong>items:<br
/> <code>curl http://169.254.169.254/latest/meta-data/<br
/> ami-id<br
/> ami-launch-index<br
/> ami-manifest-path<br
/> ancestor-ami-ids<br
/> block-device-mapping/<br
/> hostname<br
/> instance-action<br
/> instance-id<br
/> instance-type<br
/> kernel-id<br
/> local-hostname<br
/> local-ipv4<br
/> placement/<br
/> public-hostname<br
/> public-ipv4<br
/> public-keys/<br
/> ramdisk-id<br
/> reservation-id<br
/> security-groups</code></p><p>We have a direct interest in the <strong>public-ipv4</strong> and <strong>local-ipv4</strong> variables, but as we can see Amazon is providing many other useful information that can be used inside the instance for various purposes. For more details checkout the <a
href="http://docs.amazonwebservices.com/AWSEC2/2007-03-01/DeveloperGuide/AESDG-chapter-instancedata.html" target="_blank">amazon docs</a>.</p><h3>Public and private IPs</h3><p>This means that for getting the public and private ips we only have to make two calls like this:<br
/> <code>curl http://169.254.169.254/latest/meta-data/local-ipv4</code>and<br
/> <code>curl http://169.254.169.254/latest/meta-data/public-ipv4</code></p><h3>Hostnames</h3><p>We need a way to identify from inside the instance what <strong>hostname </strong>this should be configured. There are probably several ways to do this, but the most common is to specify this when the instance is started using the <strong>&#8211;user-data</strong> option of the <strong>ec2-run-instances</strong> command (or the short form -d). This will pass the custom data and make it available to the instance.</p><p>You will probably want to customize this based on your needs. Myself I assumed that I will use the same domain for all instances and I need to pass only the hostname. Since I don&#8217;t need any other parameters to the machine I can just do this:<br
/> <code>ec2-run-instances &lt;AMI&gt; <strong>-d "myhostname"</strong> ...other params...</code><br
/> If you use more user data, then you will probably use it like <em>&#8220;hostname=myhostname &lt;other_variables&gt;&#8221;</em>. (in this case you will need to update the script bellow like this: HOSTNAME=`echo $USER_DATA | cut -f 1 -d , | cut -f 2 -d =`)</p><p>Now making a http request like this will give us the hostname this instance should have:<br
/> <code>curl http://169.254.169.254/latest/user-data</code></p><h3>DNS Server configuration</h3><p>Now that we have available inside the EC2 instance all the needed information, we need a way to allow the instance to <strong>update the DNS server</strong>. As I mentioned before, I will use for this a <strong>bind9 </strong>server, but this can by any other DNS server that allows this action to be scripted somehow (either using some api calls, or any way to use dynamic DNS update). For bind9 we will use the <strong>nsupdate </strong>utility to update the DNS server securely using the <em>dnssec key</em> mechanism.</p><p>Personally I don&#8217;t use the full domain for this, but delegate two subdomains (to the same nameservers) like this:<br
/> <code>; ec2 zones:<br
/> ec2.domain.com.      NS      ns1.domain.com.<br
/> ec2.domain.com.      NS      ns2.domain.com.<br
/> ec2-int.domain.com.  NS      ns1.domain.com.<br
/> ec2-int.domain.com.  NS      ns2.domain.com.</code><br
/> and allow update access only to those zones. Of course if you prefer that you can give direct access to your full domain zone.</p><p>Generate a key using the dnssec-keygen utility like this:<br
/> <code>dnssec-keygen -a HMAC-MD5 -b 512 -n USER user.domain.com.</code><br
/> and this will create two files like this:<br
/> <code>Kuser.domain.com.+157+47950.key<br
/> Kuser.domain.com.+157+47950.private<br
/> </code><br
/> Using the information from the public key add to your dns server configuration the key:</p><pre><code>key user.domain.com. {
algorithm HMAC-MD5;
secret "xAw7F/axmVSxsZ+V4LAZnkeYObjOaJjbVKf21Zl4WhxtRHdlhqWSeCdd fIVR6MhC8LSQoim7NfkWD2j7WT5AHw==";
};</code></pre><p>where secret is the value from the public key, that in my example looks like this:</p><pre><code>cat Kuser.domain.com.+157+47950.key
user.domain.com. IN KEY 0 3 157 xAw7F/axmVSxsZ+V4LAZnkeYObjOaJjbVKf21Zl4WhxtRHdlhqWSeCdd fIVR6MhC8LSQoim7NfkWD2j7WT5AHw==</code></pre><p>Finally we need to allow update access for the key:</p><pre><code>zone "ec2.domain.com"
{
type master;
file "/etc/bind/zone/ec2.domain.com";
<strong>allow-update { key user.domain.com.; };
</strong>allow-query { any; };
};

zone "ec2-int.domain.com"
{
type master;
file "/etc/bind/zone/ec2-int.domain.com";
<strong>allow-update { key user.domain.com.; };</strong>
allow-query { any; };
};</code></pre><p>Bind will need to be <em>restarted </em>after making these changes.</p><h3>Using nsupdate to update the hostname</h3><p>Next we will need to upload the key we created on the EC2 image (later we will save it inside the AMI once all runs well) and test to see if it is working properly.<br
/> <code>cat&lt;&lt;EOF | /usr/bin/nsupdate -k Kuser.domain.com.+157+47950.private -v<br
/> server ns1.domain.com<br
/> zone ec2.domain.com<br
/> update delete test.ec2.domain.com A<br
/> update add test.ec2.domain.com 60 A &lt;some_IP&gt;<br
/> show<br
/> send<br
/> EOF</code></p><p>If this is working properly we can move on and put all this toghether in a script that will be running at the instance start time. If not, go back and see in your dns server logs if there are any issues why this is not working.</p><h3>Finally automation <img
src='http://www.ducea.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></h3><p>Now we just have to put all the pieces together and using a simple script like this will do the job:<br
/> ec2-hostname.sh:<pre><code>#!/bin/bash

#you will need to have the key available in the instance in the same dir as this script
DNS_KEY=Kuser.domain.com.+157+47950.private
DOMAIN=domain.com

USER_DATA=`/usr/bin/curl -s http://169.254.169.254/latest/user-data`
HOSTNAME=`echo $USER_DATA`
#set also the hostname to the running instance
hostname $HOSTNAME.$DOMAIN

PUBIP=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4`
cat&lt;&lt;EOF | /usr/bin/nsupdate -k $DNS_KEY -v
server ns1.$DOMAIN
zone ec2.$DOMAIN
update delete $HOSTNAME.ec2.$DOMAIN A
update add $HOSTNAME.ec2.$DOMAIN 60 A $PUBIP
send
EOF

LOCIP=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/local-ipv4`
cat&lt;&lt;EOF | /usr/bin/nsupdate -k $DNS_KEY -v
server ns1.$DOMAIN
zone ec2-int.$DOMAIN
update delete $HOSTNAME.ec2-int.$DOMAIN A
update add $HOSTNAME.ec2-int.$DOMAIN 60 A $LOCIP
send
EOF</code></pre><p>You will probably want to run this at boot time either from <em>rc.local</em> or creating an initscript for it. I put all these EC2 related stuff under /usr/local/ec2 and in this case I just call it from rc.local with a line like this:<br
/> <code>/usr/local/ec2/ ; sh ec2-hostname.sh</code></p><p>If all runs as you wanted you will probably want to <strong>save your AMI</strong> to include the script that will automatically update the dns hostname at instance boot time.</p><p>Hopefully you found this article interesting and it will be a starting point to create your own dns update script based on your needs.</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/06/01/howto-update-dns-hostnames-automatically-for-your-amazon-ec2-instances/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>iptables geoip match on debian lenny</title><link>http://www.ducea.com/2009/03/18/iptables-geoip-match-on-debian-lenny/</link> <comments>http://www.ducea.com/2009/03/18/iptables-geoip-match-on-debian-lenny/#comments</comments> <pubDate>Wed, 18 Mar 2009 13:56:31 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Debian]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Security]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[Tools]]></category> <category><![CDATA[Ubuntu]]></category> <category><![CDATA[geoip]]></category> <category><![CDATA[iptables]]></category> <category><![CDATA[kernel_modules]]></category> <category><![CDATA[lenny]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=818</guid> <description><![CDATA[The geoip iptables extension allows you to filter, nat or mangle packets based on the country&#8217;s source or destination. This does exactly what the geoip apache module does, or the regular geoip binary, but at the iptables level. I would not go into the details why you would want to use that, but there are [...]]]></description> <content:encoded><![CDATA[<p>The <strong>geoip iptables extension</strong> allows you to filter, nat or mangle packets based on the country&#8217;s source or destination. This does exactly what the geoip apache module does, or the regular geoip binary, but at the iptables level. I would not go into the details <strong>why </strong>you would want to use that, but there are many &#8216;positive&#8217; ways it can be useful&#8230; For example myself I use it in a project where we want to<em> serve customized content for different countries</em>. Since this is a high traffic site running on many web servers behind a loadbalanced setup, we prefer to split this at the <em>loadbalancer level</em> and not at apache level, to simplify our setup. We serve customized content to the US based visitors, while for the other countries we serve another international site.</p><p>Now this has been working fine for a long time now, using the <a
href="http://people.netfilter.org/peejix/geoip/" target="_blank">original geoip module</a> and <strong>patch-o-matic-ng</strong> method of installation (similar to what is very well <a
href="http://www.debian-administration.org/articles/518" target="_blank">described here</a>). Still, this is unmaintained, and starting with <strong>kernel 2.6.22</strong> it is no longer working. There is a <a
href="http://bjerkeset.com/patches/geoip-match-2.6.22.patch.gz" target="_blank">patch</a> that will make it work with a newer kernel, but if you run <strong>iptables 1.4.x</strong> this will again fail and even if there are some manual walkarounds this is still not the best solution.</p><p>The solution is called <strong>Xtables-addons</strong>. <a
href="http://xtables-addons.sourceforge.net/" target="_blank">Xtables-addons</a> is the successor to patch-o-matic-ng. Likewise, <em>it contains extensions that were not, or are not yet, accepted in the main kernel/iptables packages. Xtables-addons is different from patch-o-matic in that you do not have to patch or recompile the kernel, sometimes recompiling iptables is also not needed.</em><br
/> The latest version <strong>1.12</strong> <a
href="http://xtables-addons.git.sourceforge.net/git/gitweb.cgi?p=xtables-addons;a=blob;hb=master;f=INSTALL" target="_blank">supports</a>: iptables &gt;= 1.4.1 and kernel-source &gt;= 2.6.17.</p><p><span
id="more-818"></span>The installation is very simple and requires only the following steps exemplified on a <strong>debian lenny</strong> machine (kernel 2.6.26 and iptables 1.4.2):</p><p><strong>1.</strong> Install the needed <strong>dependencies</strong>: kernel headers and iptables dev:<br
/> <code>aptitude install linux-headers-2.6.26-1-amd64 iptables-dev</code><br
/> <em>libtext-csv-xs-perl</em> will be also needed if you plan to update the database (normally you will want this to be able to update the db from time to time):<br
/> <code>aptitude install libtext-csv-xs-perl</code></p><p><strong>2.</strong> <strong>Download </strong>the xtables-addons package and the supplied <a
href="http://jengelh.medozas.de/files/geoip/" target="_blank">database</a> (or the <a
href="http://jengelh.medozas.de/files/geoip/geoip_src.tar.bz2" target="_blank">sources</a> to build your own):<br
/> <code>wget http://switch.dl.sourceforge.net/sourceforge/xtables-addons/xtables-addons-1.12.tar.bz2<br
/> wget http://jengelh.medozas.de/files/geoip/geoip_iv0_database-20090201.tar.bz2<br
/> </code><br
/> <strong>3.</strong> Configure and <strong>compile </strong>the package. There are several iptables modules included; you can leave them all enabled or choose to compile and install only the ones needed. For this edit the <strong>mconfig </strong>file and leave only the ones you want:<br
/> <code>build_CHAOS=m<br
/> build_DELUDE=m<br
/> build_DHCPADDR=m<br
/> build_ECHO=<br
/> build_IPMARK=m<br
/> build_LOGMARK=m<br
/> build_SYSRQ=m<br
/> build_TARPIT=m<br
/> build_TEE=m<br
/> build_condition=m<br
/> build_fuzzy=m<br
/> <strong>build_geoip=m</strong><br
/> build_ipp2p=m<br
/> build_ipset=m<br
/> build_length2=m<br
/> build_lscan=m<br
/> build_quota2=m</code></p><p>Compile and install:<br
/> <code>./configure --with-xtlibdir=/lib/xtables<br
/> make<br
/> make install</code></p><p>this will add the iptables extension <strong>/lib/xtables/libxt_geoip.so</strong> and the kernel module in /lib/modules/&lt;kernel&gt;<strong>/extra/xt_geoip.ko</strong></p><p><strong>4.</strong> Now we have to put the <strong>geoip database files</strong> under the expected location (<strong>/var/geoip</strong>); this is hardcoded in the code, but you can change it if really needed and recompile. I would like to add that even if this uses the same geoip source (the <a
href="http://www.maxmind.com/app/geolitecountry" target="_blank">free GeoLite Country database</a>) as the original geoip iptables module, but the format has changed. You can either get the database from the source, or build your own with the supplied script. Once you have that copy the files to /var/geoip</p><p><code>wget http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip<br
/> unzip GeoIPCountryCSV.zip<br
/> ./runme.sh<br
/> cp -R var/geoip/ /var/</code></p><p>That&#8217;s it! All you have to do is use the module based on your needs. The syntax is the same as the original geoip iptables module:<br
/> <em> [!] &#8211;src-cc, &#8211;source-country country[,country...] = Match packet coming from (one of) the specified country(ies)<br
/> [!] &#8211;dst-cc, &#8211;destination-country country[,country...] = Match packet going to (one of) the specified country(ies)<br
/> NOTE:  The country is inputed by its ISO3166 code.</em></p><p>We use something like this to mark and send each type of traffic to its own destination:<br
/> <code>iptables -t mangle -A PREROUTING -p tcp -m geoip --src-cc US -d &lt;IP&gt; --dport 80 -j MARK --set-mark 1<br
/> iptables -t mangle -A PREROUTING -p tcp -m geoip ! --src-cc US -d &lt;IP&gt; --dport 80 -j MARK --set-mark 2</code></p><p>I hope you found this article useful, and as me, are grateful that <strong>Xtables-addons</strong> project took over the patch-o-matic-ng broken modules and made them available on current distributions. Xtables-addons was also accepted in debian repository (in <a
href="http://packages.debian.org/squeeze/xtables-addons-source" target="_blank">testing</a>) and this will make it even simpler to install and use in the future.</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/03/18/iptables-geoip-match-on-debian-lenny/feed/</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Lenny domU Xencons</title><link>http://www.ducea.com/2009/03/18/lenny-domu-xencons/</link> <comments>http://www.ducea.com/2009/03/18/lenny-domu-xencons/#comments</comments> <pubDate>Wed, 18 Mar 2009 11:09:39 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Debian]]></category> <category><![CDATA[Linux]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[Virtualization]]></category> <category><![CDATA[debian-lenny]]></category> <category><![CDATA[lenny]]></category> <category><![CDATA[xen]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=812</guid> <description><![CDATA[Even though at some point it looked like debian lenny will not have full xen support (for the 2.6.26 amd64 kernel) in the end this was fixed and lenny supports fully Xen ever on amd64. Upgrading from 2.6.18 to 2.6.26 is very straightforward (though we were using xen-hypervisor 3.2-1 already) and the only problem noticed [...]]]></description> <content:encoded><![CDATA[<p>Even though at some point it looked like <strong>debian lenny</strong> will not have full xen support (for the 2.6.26 amd64 kernel) in the end this was fixed and lenny supports fully <strong>Xen </strong>ever on amd64. Upgrading from 2.6.18 to 2.6.26 is very straightforward (though we were using xen-hypervisor <strong>3.2-1</strong> already) and the only problem noticed was that the <strong>console on the domU machines was no longer working</strong>: it was showing the output correctly, but you could not enter anything on the console.</p><p>This is caused by the &#8216;new Xen console&#8217; (<em>xen now uses hvc0 for its console</em>) and to fix it you have to add to your virtual machine xen configuration file one line: <strong>extra = &#8220;console=hvc0 xencons=tty&#8221;</strong>, restart the vm and it should be fine.  In /etc/xen/&lt;myvm&gt;.cfg add this line:<br
/> <code>extra = "console=hvc0 xencons=tty"</code></p><p><span
id="more-812"></span>If you are using <strong>pygrub </strong>then in grub.conf on the domU add: <strong>console=hvc0 xencons=tty</strong> to the kernel line, like:<br
/> <code>kernel /boot/vmlinuz-2.6.26-1-xen-amd64 ro root=/dev/sda1 console=hvc0 xencons=tty</code></p><p>Here are the xen packages lenny uses:</p><pre><code>ii  libxenstore3.0                          3.2.1-2                    Xenstore communications library for Xen
ii  linux-image-2.6.26-1-xen-amd64          2.6.26-13                  Linux 2.6.26 image on AMD64, oldstyle Xen su
ii  linux-modules-2.6.26-1-xen-amd64        2.6.26-13                  Linux 2.6.26 modules on AMD64
ii  xen-hypervisor-3.2-1-amd64              3.2.1-2                    The Xen Hypervisor on AMD64
ii  xen-tools                               3.9-4                      Tools to manage Debian XEN virtual servers
ii  xen-utils-3.2-1                         3.2.1-2                    XEN administrative tools
ii  xen-utils-common                        3.2.0-2                    XEN administrative tools - common files
ii  xenstore-utils                          3.2.1-2                    Xenstore utilities for Xen
</code></pre>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/03/18/lenny-domu-xencons/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>HowTo get a small sample dataset from a mysql database using mysqldump</title><link>http://www.ducea.com/2009/03/17/howto-get-a-small-sample-dataset-from-a-mysql-database-using-mysqldump/</link> <comments>http://www.ducea.com/2009/03/17/howto-get-a-small-sample-dataset-from-a-mysql-database-using-mysqldump/#comments</comments> <pubDate>Tue, 17 Mar 2009 16:42:49 +0000</pubDate> <dc:creator>- Marius -</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[Tips & Tricks]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[mysqldump]]></category> <category><![CDATA[tips]]></category><guid
isPermaLink="false">http://www.ducea.com/?p=793</guid> <description><![CDATA[Here is a quick tip that will show how you can get a small sample dataset from a mysql database using mysqldump. We frequently need to get a small snapshot from a very big production database to import it into a development or staging database that will not need all the original data; let&#8217;s say [...]]]></description> <content:encoded><![CDATA[<p>Here is a quick tip that will show how you can get a <strong>small sample dataset</strong> from a mysql database using <strong>mysqldump</strong>. We frequently need to get a small snapshot from a very big production database to import it into a development or staging database that will not need all the original data; let&#8217;s say we need 1,000,000 records from all the tables in the database; we will just use the option <strong>&#8211;where=&#8221;true LIMIT X&#8221;</strong>, with X the number of records we want mysqldump to stop after.</p><p>Simply we will run something like (add whatever other options you need to mysqldump):</p><p><code>mysqldump --opt --where="true LIMIT 1000000" mydb &gt; mydb1M.sql</code><br
/> <span
id="more-793"></span>and this will get 1M records from each of the tables in the database. If you want this for a single table you would use something like this:</p><pre><code>mysqldump --opt --where="true LIMIT 1000000" mydb mytable &gt; mydb_mytable_1M.sql</code></pre><p>To restore this, you would use the same as on a regular dump:</p><p><code>mysql -p mydb_stage &lt; mydb1M.sql</code></p><p>This will give you a small number of records that you can use for development, testing, etc. whatever you would need.</p>]]></content:encoded> <wfw:commentRss>http://www.ducea.com/2009/03/17/howto-get-a-small-sample-dataset-from-a-mysql-database-using-mysqldump/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Served from: www.ducea.com @ 2010-09-09 15:51:06 by W3 Total Cache -->