Writehack Software development, system administration, and assorted hackery. http://www.writehack.com/ Modifying Sproutcore Copyright Headers http://www.writehack.com/2011/3/30/Modifying Sproutcore Copyright Headers I've been teaching myself Sproutcore over the past few days and ran into a minor annoyance. At the top of each generated template, there is a header with a copyright that reads "My Company, Inc.": <code>// Copyright: ©2011 My Company, Inc.</code> Fixing this requires modifying the templates for each generator (23 files, if you are wondering). These files are located in the gems/sproutcore-1.4.5/lib/gen folder. After navigating to that folder it is easy enough to change the company with grep, piping through cut and sed: <code>grep -r "My Company, Inc." . | cut -d: -f1 | xargs sed -i 's/My Company, Inc./Matthew Downey/'</code> Obviously, you want to replace your name with mine in the above example ;) http://www.writehack.com/2011/3/30/Modifying Sproutcore Copyright Headers Packaging a Ruby C Extension as a Gem http://www.writehack.com/2011/1/30/Packaging a Ruby C Extension as a Gem Recently, I created my first ruby gem. It also happened to be a C extension. While there is a fairly good deal of information available on the web on how to package a basic gem, there isn't very much information about how to package a gem that includes a C extension. Bundler has a nice feature that creates a project skeleton for a gem, so it is easiest to start there: <code>bundle gem hello_world</code> That command should produce the following output: <code> create hello_world/Gemfile create hello_world/Rakefile create hello_world/.gitignore create hello_world/hello_world.gemspec create hello_world/lib/hello_world.rb create hello_world/lib/hello_world/version.rb Initializating git repo in /home/matt/hello_world</code> Now we need to create a directory where the extension will live: <code>mkdir -p hello_world/ext/hello_world</code> Inside this directory, we need to create our C extension, in our case, hello_world.c, and also an extconf.rb that ruby uses to create a makefile. The first file, hello_world/ext/hello_world/hello_world.c: <code lang="c">#include "ruby.h" #include <stdio.h> static VALUE method_hello_world(VALUE self) { printf("Hello World!\n"); return Qnil; } VALUE HelloWorldModule; VALUE HelloWorldClass; void Init_hello_world() { HelloWorldModule = rb_define_module("HelloWorld"); HelloWorldClass = rb_define_class_under(HelloWorldModule, "HelloWorld", rb_cObject); rb_define_method(HelloWorldClass, "hello_world", method_hello_world, 0); } </code> This should be a fairly straightforward C extension, it's basically just a module with a class and a method that lives inside that class that prints "Hello World!". The next file, hello_world/ext/hello_world/extconf.rb: <code lang="ruby">require 'mkmf' create_makefile("hello_world") </code> Next, we need to load the extension from the hello_world.rb file located in the lib directory. Basically, all we need to do is add a require statement: hello_world/lib/hello_world.rb: <code lang="ruby">require 'hello_world/hello_world' module HelloWorld # Your code goes here... end </code> The require statement seems a little redundant, but the .so file that is built actually gets stored in hello_world/lib/hello_world. The next step is to modify the Rakefile in the root directory. Note that in order to build a gem that includes a C extension, we are going to need the rake extensiontask gem, so if you don't have that, now would be a good time to install it. Modify your hello_world/Rakefile as follows: <code lang="ruby">require 'rubygems' require 'rake' require 'rake/extensiontask' require 'bundler' Rake::ExtensionTask.new("hello_world") do |extension| extension.lib_dir = "lib/hello_world" end task :chmod do File.chmod(0775, 'lib/hello_world/hello_world.so') end task :build => [:clean, :compile, :chmod] Bundler::GemHelper.install_tasks </code> Next, we need to modify the hello_world.gemspec. Bundler creates a nice skeleton for us, the only thing we need to do is fill in the information for author, email, homepage, summary, and description, and add one line that points to the extconf.rb file, the s.extensions line. Modify the hello_world/hello_world.gemspec as follows, replacing your info with mine: <code lang="ruby"># -*- encoding: utf-8 -*- $:.push File.expand_path("../lib", __FILE__) require "hello_world/version" Gem::Specification.new do |s| s.name = "hello_world" s.version = HelloWorld::VERSION s.platform = Gem::Platform::RUBY s.authors = ["Matthew Downey"] s.email = ["mattddowney@gmail.NOSPAM.com"] s.homepage = "http://www.writehack.com" s.summary = %q{Hello World!} s.description = %q{Gem that prints Hello World!} s.rubyforge_project = "hello_world" s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] s.extensions = ["ext/hello_world/extconf.rb"] end </code> Next, build the extension, which should compile the .so file for us: <code>rake build</code> If you notice, the s.files line in the gemspec gets the files for the project from a git repository. It's probably a good idea to create a .gitignore file, but we won't cover that here. Just create an empty git repository, add everything, and make your initial commit: <code>git init git add . git commit -m 'initial commit'</code> Now, all thats left to do is install and test the gem! <code>rake install</code> If everything went well, we should no be able to fire up irb and test our gem: <code>ruby-1.9.2-p136 :001 > require 'rubygems' => true ruby-1.9.2-p136 :002 > require 'hello_world' => true ruby-1.9.2-p136 :003 > hello = HelloWorld::HelloWorld.new => #<HelloWorld::HelloWorld:0x000000027d7c40> ruby-1.9.2-p136 :004 > hello.hello_world Hello World! => nil ruby-1.9.2-p136 :005 > </code> Hopefully this helps someone that is trying to package a C extension as a gem. I wrestled with this for hours. http://www.writehack.com/2011/1/30/Packaging a Ruby C Extension as a Gem Scanning for Random DNS Servers http://www.writehack.com/2011/1/28/Scanning for Random DNS Servers Nmap is such a useful tool. One neat feature is the ability to scan random IP addresses. Someone recently asked me if there was a way to discover random DNS servers. <code>sudo nmap -Pn -sS -p 53 -iR 1000 --open | grep -B 4 "53/tcp open"</code> That will invoke nmap to scan 1000 random IP addresses for an open TCP port 53. It pipes through grep to filter out the garbage (the -B option grabs 4 lines of output before the line that was grepped for). http://www.writehack.com/2011/1/28/Scanning for Random DNS Servers Clearing Logs on Ubuntu System Shutdown http://www.writehack.com/2010/10/11/Clearing Logs on Ubuntu System Shutdown <p>I have a virtual machine of Ubuntu 10.10 that I use for development purposes. The other day, I got this message:</p> <p><img src="http://img812.imageshack.us/img812/170/ubuntulowdiskspace.jpg" alt='The volume "var" has only 0 bytes disk space remaining.' /></p> <p>Since the box is for development purposes, I have no real need to keep several days worth of logs. So I cooked up a script to delete the logs:</p> <code lang="bash">#!/bin/bash # delete archived logfiles for i in `find /var/log -type f \( -name \*.\[0-9\] -o -name \*.bz2 -o -name \*.gz -o -name \*.old \)` do rm $i done # clear remaining logfiles for i in `find /var/log -type f` do cat /dev/null > $i done </code> <p>I saved it as clearlogs.sh, in my /etc/init.d folder. Then, I made it executable, and created symbolic links in rc0.d and rc6.d to make it run on shutdown and restart:</p> <code lang="bash">chmod +x /etc/init.d/clearlogs.sh ln -s /etc/init.d/clearlogs.sh /etc/rc0.d/K10clearlogs.sh ln -s /etc/init.d/clearlogs.sh /etc/rc6.d/K10clearlogs.sh</code> <p>Now every time I load my system, I have clean logs. I would never do this on a production system or even my primary system, but for a dev box this works great.</p> http://www.writehack.com/2010/10/11/Clearing Logs on Ubuntu System Shutdown Virtual Linux Lab Series http://www.writehack.com/2010/10/9/Virtual Linux Lab Series <p>This post serves as a marker for my virtual linux lab series. It is a series of tutorials that shows how to install and configure two CentOS 5.5 guests on a Windows 7 host as a virtual lab with VirtualBox. It is geared towards absolute beginners; I designed it to help my fellow linux system administration students at the <a href="http://www.ccaurora.edu">Community College of Aurora</a>.</p> <p>Many of us don't have access to a proper lab, but thanks to virtualization technology, we can set one up on a single computer. Hopefully you will learn as much as I did reading these as I did creating them. :)</p> <ul> <li><a href="http://www.writehack.com/2010/10/1/Virtualbox%20Installation">Virtualbox Installation</a></li> <li><a href="http://www.writehack.com/2010/10/2/Installing%20CentOS%20as%20a%20Virtualbox%20Guest">Installing CentOS as a Virtualbox Guest</a></li> <li><a href="http://www.writehack.com/2010/10/3/Configuring%20Sudoers%20on%20CentOS">Configuring Sudoers on CentOS</a></li> <li><a href="http://www.writehack.com/2010/10/3/Modifying%20the%20Path%20To%20Include%20sbin">Modifying the Path to Include sbin</a></li> <li><a href="http://www.writehack.com/2010/10/4/Installing%20Virtualbox%20Guest%20Additions">Installing Virtualbox Guest Additions</a></li> <li><a href="http://www.writehack.com/2010/10/5/Virtualbox%20Shared%20Directories">Virtualbox Shared Directories</a></li> <li><a href="http://www.writehack.com/2010/10/6/Adding%20Virtualbox%20to%20the%20Windows%20Path">Adding Virtualbox to the Windows Path</a> <li><a href="http://www.writehack.com/2010/10/7/Cloning%20a%20VirtualBox%20Disk">Cloning a VirtualBox Disk</a></li> <li><a href="http://www.writehack.com/2010/10/9/VirtualBox%20Bridged%20Networking">VirtualBox Bridged Networking</a></li> </ul> <p>Check back often, as I will most likely be adding to this series in the future.</p> http://www.writehack.com/2010/10/9/Virtual Linux Lab Series VirtualBox Bridged Networking http://www.writehack.com/2010/10/8/VirtualBox Bridged Networking <p>If you've been using VirtualBox for a while, you've probably been scratching your head wondering how you can give two virtual machines the ability to communicate. The easiest way to do this is to configure bridged networking. In this tutorial, we are going to configure and test two CentOS 5.5 guests and a Windows 7 host. We also assume you already have <a href="http://www.writehack.com/2010/10/4/Installing%20Virtualbox%20Guest%20Additions">guest additions</a> installed.</p> <p>Make sure your virtual machines are shutdown, then click "Settings." Select "Network" from the list on the left. This will bring up the guest machine's network settings. As you can see, VirtualBox allows a lot of network flexibility, with support for up four adapters and four different networking modes. Click the dropdown box labeled "Attached to" and change it to "Bridged Adapter" like so:</p> <p><img src="http://img177.imageshack.us/img177/4737/bridged1.png" /></p> <p>Now we must make sure that we select the proper network adapter that our host is using. On my laptop, I have two adapters, a physical adapter and a wireless one. Since I am connected to my network wirelessly, I need to select my wireless adapter. Click the dropdown box labeled "Name" and select the appropriate adapter that you are connecting with:</p> <p><img src="http://img707.imageshack.us/img707/1708/bridged2.png" /></p> <p>After clicking "OK," modify the other virtual machines on the network in the same manner. Start them both.</p> <p>Bridged networking does a bit of voodoo to retrieve multiple IP addresses from your router with the same adapter card. If you login to your router after your guest OSes are connected, you should notice that there are multiple IP addresses for the same MAC address in your router.</p> <p>Let's test that everything is working. First, verify that your host can connect to the internet. If that is working properly, and it should be (you are reading this), the next step is to verify internet connectivity on each guest machine. Open up a terminal, and ping google. If everything went okay, you should get something like this:</p> <code>[matt@localhost ~]$ ping -c 3 google.com PING google.com (209.85.225.106) 56(84) bytes of data. 64 bytes from iy-in-f106.1e100.net (209.85.225.106): icmp_seq=1 ttl=52 time=83.4 ms 64 bytes from iy-in-f106.1e100.net (209.85.225.106): icmp_seq=2 ttl=52 time=82.3 ms 64 bytes from iy-in-f106.1e100.net (209.85.225.106): icmp_seq=3 ttl=52 time=82.9 ms --- google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2000ms rtt min/avg/max/mdev = 82.397/82.942/83.443/0.428 ms</code> <p>The important thing is at the bottom: all packets that were transmitted were recieved.</p> <p>Next, let's verify connectivity between the the host and each guest, and in between our two guests. To do this, we need to get the ip addresses for each machine. First, lets get the Windows host's ip address. Open up a command promt by going to "Start" and typing "cmd" into the search box. Now run ipconfig:</p> <code>C:\Users\Matt>ipconfig</code> <p>The should produce a fairly decent amount of output. The line we are interested in is near the top, the first IPv4 Address:</p> <code>IPv4 Address. . . . . . . . . . . : 192.168.1.60</code> <p>Write this number down.</p> <p>Now, let's move onto each guest. In linux, the command to get the computer's ip address is ifconfig, and it must be run as root. If you have <a href="http://www.writehack.com/2010/10/3/Configuring%20Sudoers%20on%20CentOS">sudoers configured</a>, and have <a href="http://www.writehack.com/2010/10/3/Modifying%20the%20Path%20To%20Include%20sbin">added /sbin to your path</a>, open up a terminal and type the following:</p> <code>sudo ifconfig</code> <p>The line we are looking for is also near the top, listed under our network adapter, which should be eth0. Specifically, we are looking for the second line:</p> <code>inet addr:192.168.1.114 Bcast:192.168.1.255 Mask:255.255.255.0</code> <p>The first number in this line is our ip address. Write this down, and do this for each guest. My ip addresses are the following:</p> <table border="1"> <tr> <th>Machine</th> <th>IP address</th> </tr> <tr> <td>Windows Host</td> <td>192.168.1.60</td> </tr> <tr> <td>CentOS Guest #1</td> <td>192.168.1.114</td> </tr> <tr> <td>CentOS Guest #2</td> <td>192.168.1.111</td> </tr> </table> <p>To verify that I can connect to each guest from my host, I just ping them from my Windows command prompt. The Windows and linux ping commands are different, yet similar. For Windows, there's no need to specify a number of packets to send. Let's ping both guests now:</p> <p><img src="http://img820.imageshack.us/img820/9407/pingvz.png" /></p> <p>As you can see, I can reach both guests from my Windows host just fine.</p> <p>Next, we'll ping the host and other guest from each guest:</p> <p> <a href="http://img99.imageshack.us/img99/8338/ping2b.png"> <img src="http://img99.imageshack.us/img99/8338/ping2b.png" width="75%" height="75%" /><br /> (Click for full size image) </a> </p> <p>Again, no errors-- everything appears to be working fine. Now we can move onto more interesting things, like using ssh and configuring various servers.</p> http://www.writehack.com/2010/10/8/VirtualBox Bridged Networking Cloning a VirtualBox Disk http://www.writehack.com/2010/10/7/Cloning a VirtualBox Disk <p>One nice thing about using virtual machines is that you can make copies of them. This way if you install and configure a machine to your liking, you can just clone it and get another machine without having to go through the hassle of installation and configuration again. You can't just copy the .vdi file, however, because each virtual disk has a unique id. You can however, clone a disk relatively easily with the VBoxManage command.</p> <p>In this tutorial we will be using a Windows 7 host, and cloning the hard disk for the CentOS 5.5 install that we performed in an earlier post. We will also assume that VBoxManage is in the Windows path. Open up a command prompt on the Windows host by clicking "Start" and typing "cmd" into the search box.</p> <p>Once we open up a command prompt, we need to navigate to the directory where our hard disks are stored. These are in the your user's directory in the ".Virtualbox\HardDisks" subfolder. When you first open the command prompt, you should already be in your user's directory. Change to the hard disks folder by typing the following:</p> <code lang="bash">cd .VirtualBox\HardDisks</code> <p>At this point, you can type "dir" to get a list of your .vdi files. If you followed along in the previous tutorial, you probably installed CentOS on a disk named "CentOS.vdi".</p> <p>The syntax for the command to clone the disk is simple, "VBoxManage clonehd [infile.vdi] [outfile.vdi]". To clone disk named "CentOS.vdi" and create a duplicate "CentOS2.vdi" we type:</p> <code lang="bash">VBoxManage clonehd CentOS.vdi CentOS2.vdi</code> <p>This process will take a few minutes.</p> <p><img src="http://img149.imageshack.us/img149/14/vboxmanageclonehd.png" /></p> <p>To make the disk available to VirtualBox, we need to add it in the "Virtual Media Manager." From the main VirtualBox window, go to "File > Virtual Media Manager." Click "Add" and select the new disk we just cloned. Click "OK" and it should now be available in the dropdown box when you create a new machine.</p> http://www.writehack.com/2010/10/7/Cloning a VirtualBox Disk Adding Virtualbox to the Windows Path http://www.writehack.com/2010/10/6/Adding Virtualbox to the Windows Path <p>VirtualBox comes with a useful command line utility called VBoxManage that allows you to do anything that can be done with the GUI tool and more. The only problem is it isn't added to the Windows path. Adding it to the path is relatively easy.</p> <p>First, bring up the System properties. The easiest way to access this is through a keyboard shortcut, "Windows-Break." Alternatively, you can get there by clicking "Start," right-clicking on "Computer," and then selecting "Properties."</p> <p><img src="http://img339.imageshack.us/img339/1648/path1.png" /></p> <p>Now click "Advanced system settings" in the list on the left hand side.</p> <p><img src="http://img230.imageshack.us/img230/304/path2.png" /></p> <p>Click the "Environment Variables..." button towards the bottom of this dialog.</p> <p><img src="http://img340.imageshack.us/img340/8531/path3.png" /></p> <p>This will open up an "Environment Variables" dialog. In the section labeled "System variables" scroll down to the one that says "Path" and click "Edit..."</p> <p><img src="http://img708.imageshack.us/img708/1399/path4.png" /></p> <p>Append the following to the end of the box labeled "Variable value" and click "OK." If you installed VirtualBox to another directory, use that instead:</p> <code lang="bash">;C:\Program Files\Oracle\VirtualBox</code> <p>Click "OK" on the "Environment Variables" and "System Properties" dialogs. Open a Windows command prompt by clicking "Start" and then typing "cmd" into the search box. If everything went well, you should be able to type the following to get a list of your virtual machines:</p> <code lang="bash">VBoxManage list vms</code> <p><img src="http://img176.imageshack.us/img176/6822/path5.png" /></p> http://www.writehack.com/2010/10/6/Adding Virtualbox to the Windows Path Virtualbox Shared Directories http://www.writehack.com/2010/10/5/Virtualbox Shared Directories <p>Virtualbox allows you to share files between the guest and host operating systems. For the purposes of this tutorial, the host OS is Windows 7 and the guest OS is CentOS 5.5, although we could use any flavor of Linux. This tutorial also assumes that you have <a href="http://www.writehack.com/2010/10/4/Installing%20Virtualbox%20Guest%20Additions">guest additions</a> installed. <p>To start with, create a folder on the host operating system. You can exist anywhere you like. For ease of access, we'll just place ours on the desktop. To do this, right-click on the Windows desktop and select "New > Folder." Give this folder the name "share."</p> <p><img src="http://img192.imageshack.us/img192/9862/share0.png" width="439" height="247" /></p> <p>Next, run Virtualbox and start the guest operating system. From Virtualbox's menu, select "Devices > Shared Folders."</p> <p><img src="http://img214.imageshack.us/img214/7991/share1.png" width="350" height="157" /></p> <p>This will open up a dialog box with the title "Shared Folders."</p> <p><img src="http://img201.imageshack.us/img201/4899/share2.png" width="466" height="338" /></p> <p>On the top right of the dialog, click on the folder icon with the green plus sign. This will open up a file browser.</p> <p><img src="http://img689.imageshack.us/img689/6513/share3.png" width="308" height="227" /></p> <p>Browse to the share folder that was created on the host desktop and make sure to check the box labeled "Make Permanent." If this box is unchecked, it will create a transient share folder that will disappear the next time the guest OS is started. Click OK. The share should now appear in the list under the heading labeled "Machine Folders."</p> <p><img src="http://img69.imageshack.us/img69/6266/share4.png" width="466" height="338" /></p> <p>After clicking OK, the configuration is complete on the host operating system. Now we need to configure the guest OS. Login to the guest machine, and open up a terminal. We need to create a directory for the shared folder to reside. This can be anywhere on the filesystem, but for our purposes, we will be creating the directory <span class="systemFont">/share</span>. Since we will be creating this directory in the root folder, we need be root. Either change to the root user using <span class="systemFont">su -</span>, or prefix the command with sudo. We will be using sudo. Issue the following command:</p> <code lang="bash">sudo mkdir /share</code> <p>Now we must mount the host's share folder to the <span class="systemFont">/share</span> directory we just created on the guest operating system. We could do this with the <span class="systemFont">mount</span> command, however that would require us to mount the folder every time we start the guest OS. Since we want this share to be permanent, it needs to be mounted on startup. To do this, we must add a line to the <span class="systemFont">/etc/fstab file</span>. We must be root to edit this file, so issue the following command:</p> <code lang="bash">sudo vi /etc/fstab</code> <p>This will open up the <span class="systemFont">/etc/fstab</span> in vi. Scroll down to the bottom of the file, and push <span class="systemFont">o</span> to switch to edit mode and insert the following line at the end of the file:</p> <code lang="bash">share /share vboxsf defaults 0 0</code> <p>The first column is the name of the share we are mounting, given to us from the name column of the shared folders dialog. The second column is the mountpoint, the name of the directory we created on the guest that we will be mounting the folder to. The third column is the filesystem type, and the remaining columns are various options, of which we will use the default setting. After adding the line, save and edit the editor using <span class="systemFont">:wq</span>.</p> <p>At this point, everything is setup, and all that is required is a reboot of the guest OS for the system to reload the /etc/fstab file. But before we do so, it would be nice to have a shortcut to our share on our linux desktop. To do this, we need to create a symbolic link, and store it in the ~/Desktop directory. Make sure you are logged in as your normal user, and type the following into the terminal:</p> <code lang="bash">ln -s /share ~/Desktop/share</code> <p>A link to the <span class="systemFont">/share</span> folder should now appear on your desktop.</p> <p><img src="http://img214.imageshack.us/img214/2636/share5.png" width="491" height="91" /></p> <p>Now, reboot the guest OS and test the shared folder by placing some files into it. If everything went well, the files should show up in the folder on both the host and guest operating systems.</p> http://www.writehack.com/2010/10/5/Virtualbox Shared Directories Installing Virtualbox Guest Additions http://www.writehack.com/2010/10/4/Installing Virtualbox Guest Additions <p>Virtualbox is nice because it comes with a set of drivers called guest additions, that allow you to be able to resize the guest window and provide seamless integration for the mouse and keyboard, without the need for using the host key. Installing guest additions also allows for more advanced networking modes, among other things. This tutorial assumes that your guest OS is CentOS or other Red Hat based, although the installation is similar for all distributions, you just have to substitute your distros package manager for yum.</p> <p>To get started we need to install a few packages as root. Type the following into a terminal:</p> <code lang="bash">sudo yum install gcc kernel kernel-devel</code> <p>Yum should ask you if you really want to install the packages, so tell it yes. After the packages have installed, reboot the virtual machine, and login to the desktop. Then, from the Virtualbox menu, select "Devices > Install Guest Additions."</p> <p><img src="http://img33.imageshack.us/img33/5125/installguestadditions.png" /></p> <p>This should automount the guest additions, which will show up as a CD on the desktop. Open another terminal, and switch to the root user:</p> <code lang="bash">su -</code> <p>Enter your password and change to the directory where the guest additions reside:</p> <code lang="bash">cd /media/VBOXADDITIONS_3.2.8_64453</code> <p>You might have a different version of guest additions, so substitute accordingly.</p> <p>Next, depending on your guest machine's architecture, run either VBoxLinuxAdditions-x86.run or VBoxLinuxAdditions-amd64.run using sh.</p> <code lang="bash">sh VBoxLinuxAdditions-amd64.run</code> <p>This will take a couple of minutes, just be patient. The system is recompiling the kernel with the new guest addition drivers. After it finishes, reboot. After your machine finishes booting, if everything went well you should be able to resize the window and the mouse will move seamlessly from guest to host without the need for using the host key.</p> http://www.writehack.com/2010/10/4/Installing Virtualbox Guest Additions Modifying the Path To Include sbin http://www.writehack.com/2010/10/3/Modifying the Path To Include sbin <p>If you are running Red Hat or CentOS and have added a user to /etc/sudoers, you may be perplexed by the fact that you cannot run ifconfig with sudo:</p> <code>[matt@localhost ~]$ sudo ifconfig sudo: ifconfig: command not found</code> <p>This is because /sbin is not in the current user's path. If we run the following command, it works:</p> <code>[matt@localhost ~]$ sudo /sbin/ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:6E:BC:67 inet addr:192.168.1.114 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe6e:bc67/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:576 errors:0 dropped:0 overruns:0 frame:0 TX packets:528 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:73739 (72.0 KiB) TX bytes:42611 (41.6 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:1148 errors:0 dropped:0 overruns:0 frame:0 TX packets:1148 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1945296 (1.8 MiB) TX bytes:1945296 (1.8 MiB)</code> <p>The solution is to modify the user's .bash_profile and add /sbin to the user's path. We might as well add /usr/sbin to the path, too, to avoid other surprises. Open up .bash_profile with your favorite text editor, and find the following line:</p> <code lang="bash">PATH=$PATH:$HOME/bin</code> <p>Modify it so it looks like this:</p> <code lang="bash">PATH=$PATH:$HOME/bin:/sbin:/usr/sbin</code> <p>Save the file, and logout of GNOME and then log back in. Now sudo ifconfig should behave as expected.<p> http://www.writehack.com/2010/10/3/Modifying the Path To Include sbin Configuring Sudoers http://www.writehack.com/2010/10/3/Configuring Sudoers <p>Adding a user to the sudoers file on Linux is a fairly simple process. All we need to do is edit the /etc/sudoers file as root, and add a line.</p> <p>First, open up a terminal and switch to the root user by typing the following:</p> <code lang="bash">su -</code> <p>Enter the root user's password, and then load the /etc/sudoers file in vi:</p> <code lang="bash">vi /etc/sudoers</code> <p>Notice that vi is saying that this is a readonly file, so when we go to save it we have to use :w! instead of the normal :w. Scroll to the bottom of the file by pushing 'G', and hit 'o' to enter insert mode and add the following line at the end of the file:</p> <code lang="bash">username ALL=(ALL) ALL</code> <p>Substitute your username where it says username, of coarse. Save the file by pressing escape to exit insert mode, and typing ':w!' without the quotes. Exit vi by typing ':q', again without the quotes. Now we can exit the root shell, and execute commands as root by prefixing them with sudo.</p> http://www.writehack.com/2010/10/3/Configuring Sudoers Configuring Sudoers on CentOS http://www.writehack.com/2010/10/3/Configuring Sudoers on CentOS <p>Adding a user to the sudoers file on Red Hat or CentOS is a fairly simple process. All we need to do is edit the /etc/sudoers file as root, and add a line.</p> <p>First, open up a terminal and switch to the root user by typing the following:</p> <code lang="bash">su -</code> <p>Enter the root user's password, and then load the /etc/sudoers file in vi using the visudo command:</p> <code lang="bash">visudo</code> <p>Scroll down until you find the following section:</p> <code lang="bash">## Allows people in group wheel to run all commands # %wheel ALL=(ALL) ALL</code> <p>Push 'i' to enter insert mode, and uncomment the second line so it looks like this:</p> <code lang="bash">## Allows people in group wheel to run all commands %wheel ALL=(ALL) ALL</code> <p>Uncommenting this line gives everyone in the group "wheel" the ability to run all commands. Exit insert mode by pressing escape, and then save the file and exit the editor by typing ':wq' without quotes.</p> <p>Now we need to add our user to the "wheel" group:</p> <code lang="bash">usermod -G wheel username</code> <p>Substitute your username where it says username, of coarse. Type "exit" to exit the root shell. You should now be able to execute all commands as root by prefixing them with sudo. You may notice that certain commands such as ifconfig don't work with sudo as expected, and this is because /sbin is not in the user's path. The solution to this problem is covered in this post.</p> http://www.writehack.com/2010/10/3/Configuring Sudoers on CentOS Virtualbox Shared Directories http://www.writehack.com/2010/10/2/Virtualbox Shared Directories <p>Virtualbox allows you to share files between the guest and host operating systems. For the purposes of this tutorial, the host OS is Windows 7 and the guest OS is CentOS 5.5, although we could use any flavor of Linux.</p> <p>To start with, create a folder on the host operating system. You can exist anywhere you like. For ease of access, we'll just place ours on the desktop. To do this, right-click on the Windows desktop and select “New &gt; Folder.” Give this folder the name “share.”</p> <p><img src="http://img192.imageshack.us/img192/9862/share0.png" width="439" height="247" /></p> <p>Next, run Virtualbox and start the guest operating system. From Virtualbox's menu, select “Devices &gt; Shared Folders.”</p> <p><img src="http://img214.imageshack.us/img214/7991/share1.png" width="350" height="157" /></p> <p>This will open up a dialog box with the title “Shared Folders.”</p> <p><img src="http://img201.imageshack.us/img201/4899/share2.png" width="466" height="338" /></p> <p>On the top right of the dialog, click on the folder icon with the green plus sign. This will open up a file browser.</p> <p><img src="http://img689.imageshack.us/img689/6513/share3.png" width="308" height="227" /></p> <p>Browse to the share folder that was created on the host desktop and make sure to check the box labeled “Make Permanent.” If this box is unchecked, it will create a transient share folder that will disappear the next time the guest OS is started. Click OK. The share should now appear in the list under the heading labeled “Machine Folders.”</p> <p><img src="http://img69.imageshack.us/img69/6266/share4.png" width="466" height="338" /></p> <p>After clicking OK, the configuration is complete on the host operating system. Now we need to configure the guest OS. Login to the guest machine, and open up a terminal. We need to create a directory for the shared folder to reside. This can be anywhere on the filesystem, but for our purposes, we will be creating the directory <span class="systemFont">/share</span>. Since we will be creating this directory in the root folder, we need be root. Either change to the root user using <span class="systemFont">su -</span>, or prefix the command with sudo. We will be using sudo. Issue the following command:</p> <code lang="bash">sudo mkdir /share</code> <p>Now we must mount the host's share folder to the <span class="systemFont">/share</span> directory we just created on the guest operating system. We could do this with the <span class="systemFont">mount</span> command, however that would require us to mount the folder every time we start the guest OS. Since we want this share to be permanent, it needs to be mounted on startup. To do this, we must add a line to the <span class="systemFont">/etc/fstab file</span>. We must be root to edit this file, so issue the following command:</p> <code lang="bash">sudo vi /etc/fstab</code> <p>This will open up the <span class="systemFont">/etc/fstab</span> in vi. Scroll down to the bottom of the file, and push <span class="systemFont">o</span> to switch to edit mode and insert the following line at the end of the file:</p> <code lang="bash">share /share vboxsf defaults 0 0</code> <p>The first column is the name of the share we are mounting, given to us from the name column of the shared folders dialog. The second column is the mountpoint, the name of the directory we created on the guest that we will be mounting the folder to. The third column is the filesystem type, and the remaining columns are various options, of which we will use the default setting. After adding the line, save and edit the editor using <span class="systemFont">:wq</span>.</p> <p>At this point, everything is setup, and all that is required is a reboot of the guest OS for the system to reload the /etc/fstab file. But before we do so, it would be nice to have a shortcut to our share on our linux desktop. To do this, we need to create a symbolic link, and store it in the ~/Desktop directory. Make sure you are logged in as your normal user, and type the following into the terminal:</p> <code lang="bash">ln -s /share ~/Desktop/share</code> <p>A link to the <span class="systemFont">/share</span> folder should now appear on your desktop.</p> <p><img src="http://img214.imageshack.us/img214/2636/share5.png" width="491" height="91" /></p> <p>Now, reboot the guest OS and test the shared folder by placing some files into it. If everything went well, the files should show up in the folder on both the host and guest operating systems.</p> http://www.writehack.com/2010/10/2/Virtualbox Shared Directories Installing CentOS as a Virtualbox Guest http://www.writehack.com/2010/10/2/Installing CentOS as a Virtualbox Guest <p>In this post we will install CentOS 5.5 as a guest operating system in Virtualbox. In order to do this, we need to have some kind of installation media handy. Virtualbox will allow us to install via DVD or, more conveniently, an ISO located on the host operating system's hard disk.</p> <p>If you don't have a CentOS DVD or ISO handy, you're going to need to download one; in order to download it, you're going to need a bittorrent client, a good one for Windows is <a href="http://www.utorrent.com">uTorrent</a>. Hop on over to the <a href="http://mirror.centos.org/centos/5/isos/">CentOS mirrors page</a> and grab a copy of the DVD torrent for your computer's architecture. If your computer is 64 bit, choose x86_64, otherwise if it is 32 bit or you are unsure, get the i386 version. After selecting an architecure, choose a mirror from the list to be taken to a download page. The file that you are interested in is either CentOS-5.5-i386-bin-DVD.torrent or CentOS-5.5-x86_64-bin-DVD.torrent, depending on your architecture. The torrent will download two DVDs, but we really only need the first one.</p> <p>After downloading a copy of the media to your hard disk, it's time to get started. Go ahead and fire up Virtualbox. You should be presented with a screen that looks something like this:</p> <p><img src="http://img42.imageshack.us/img42/3118/capturerk.png" /></p> <p>Click the button labeled "New" in the upper lefthand corner to create a new virtual machine. This will start a wizard to take you through the process of creating a new machine. After clicking "Next," the Wizard will ask you for the VM Name and OS Type, like so:</p> <p><img src="http://img709.imageshack.us/img709/2765/capture3rk.png" /></p> <p>Give your machine a name. If you give it the name CentOS, it will automatically change the operating system to Linux and version to Red Hat. Red Hat is the correct version, because CentOS is a clone of Red Hat. It is important that if you downloaded a 64 bit image to change the version to "Red Hat (64 bit)."</p> <p>After clicking next, the wizard takes us to a screen where we can select the amount of memory that the guest operating system will have available to it:</p> <p><img src="http://img269.imageshack.us/img269/6135/capture4pb.png" /></p> <p>Virtualbox recommends 512 megabytes and that is a good number to go with, since setting up a virtual lab is going to require us to run multiple guest OS at once, and each one is going to need it's own block of RAM. You can always change the amount of RAM the guest has available to it later in the virtual machine's settings.</p> <p>Click next and move on to the virtual hard disk setup:</p> <p><img src="http://img811.imageshack.us/img811/8100/capture5m.png" /></p> <p>Here, we basically have the option of creating a new hard disk, using an existing disk, or attaching disks later. We're going to go with the first option and create a new disk, so go ahead and click next.</p> <p>This will open up another wizard, where we create the disk for the machine. Go ahead and click next again, which should bring you to a window that looks like this:</p> <p><img src="http://img194.imageshack.us/img194/6971/capture7l.png" /></p> <p>This screen gives us the option to choose whether we want to use dynamically expanding or fixed-size storage. Since the guest operating system will live in a .vdi file on your hard disk, it's nice to just have the file increase in size as the guest operating system grows. Dynamic disks save space on the host and make backups easier because you end up with a smaller file. Click next and move on to the next screen, where we will define the size of the virtual disk.</p> <p><img src="http://img717.imageshack.us/img717/8658/capture8h.png" ></p> <p>When we first get to this screen, Virtualbox gives us a recommended default size of 8GB. That's fine if you don't want to use a custom partition layout or install all the packages, but for the purposes of this tutorial, we'll be using a 15GB disk, since it will allow us to install all of the packages if we choose to do so, and also give us enough room to play around with. Change the size of the disk to where you want it, and click next.</p> <p>Both wizards are at the end now, and are going to display a summary. Go ahead and click finish for both of them, and there should be a new virtual machine in your list:</p> <p><img src="http://img651.imageshack.us/img651/4749/capture11x.png" /></p> <p>Now that that's taken care of, click the green start arrow to fire up the virtual machine. At various points while running the machine, Virtualbox will pop up with some windows with various information about keyboard and mouse capture:</p> <p><img src="http://img251.imageshack.us/img251/6852/capture12p.png" /><img src="http://img837.imageshack.us/img837/5752/capture24.png" /></p> <p>These dialogs are confusing at first, but they are basically telling you that once you use your mouse or keyboard inside the guest OS, in order to make them available to the host you have to press the host key, which is defined as the right control key. The left control key functions as normal in the guest, but pushing the right one will release the mouse and keyboard back to the host operating system.</p> <p>The first time you run the machine, it will bring up the first run wizard. Go ahead and click next to get started.</p> <p><img src="http://img541.imageshack.us/img541/2503/capture14l.png" /></p> <p>Click the folder with the green arrow to select the ISO file we downloaded to install from. This will take us to the Virtual Media Manager, which should be empty. Click the "Add" button and browse to the folder where you downloaded the CentOS DVD ISO and select it. The Virtual Media Manager should now look something like this:</p> <p><img src="http://img139.imageshack.us/img139/227/capture17w.png" /></p> <p>Highlight the ISO you want to install from and click select. The ISO should now be available in the wizard's drop down box:</p> <p><img src="http://img809.imageshack.us/img809/1164/capture18.png" /></p> <p>Make sure it's selected and click next. Next a summary screen will be displayed. Click "Finish." This will boot into the CentOS installer.</p> <p><img src="http://img842.imageshack.us/img842/6769/capture20g.png" /></p> <p>The rest of the tutorial will focus on the actual CentOS install process. Hit enter to begin.</p> <p><img src="http://img97.imageshack.us/img97/6580/capture22o.png" /></p> <p>This screen will perform an optional check on our installation media. Since we are installing our system from an ISO we downloaded and not a scratched DVD, we'll skip this step. Push tab to highlight the "Skip" option and select it with the spacebar. This will load the graphical installer. Click "Next," which will take us to a screen where you can select the language you want to use for installation. Click "Next," and select your appropriate keyboard, which is most likely "U.S. English" if you are in the United States. Click "Next," and you should be presented with the following dialog:</p> <p><img src="http://img830.imageshack.us/img830/9320/capture27.png" /></p> <p>This screen is informing us that we are going to erase all of the data on our virtual hard disk. This is okay, it will not delete all of the files on your host operating system. Go ahead and click "Yes."</p> <p><img src="http://img268.imageshack.us/img268/883/capture28.png" /></p> <p>At this screen, the installer is going to ask us how we want to partition the disk. You could use the default layout, but we will be creating a custom layout, which is the more advanced option. Select "Create custom layout" from the dropdown box and click "Next" to define the partitions.</p> <p><img src="http://img811.imageshack.us/img811/2121/capture29h.png" /></p> <p>From here, click "New" to get started adding the first partition.</p> <p><img src="http://img52.imageshack.us/img52/3714/capture30.png" /></p> <p>Select "/" for the first mountpoint from the dropdown box, and give it a size of 1 GB (technically 1GB is equal to 1024MB, but we will just use multiples of 1000 for simplicity's sake). Click "OK." You should now see the new partition in the editor, which should now look something like this:</p> <p><img src="http://img706.imageshack.us/img706/5810/capture31.png" /></p> <p>Go ahead and click "New" again to create another partition. This time, we are going to create the swap partition:</p> <p><img src="http://img233.imageshack.us/img233/7666/capture32.png" /></p> <p>Typically, it is a good idea to set the swap size to twice the size of your RAM. Since we setup our virtual machine with 512MB of RAM, we want a swap size of 1024MB. Set the filesystem type to swap and enter the proper number of megabytes, then click "OK." Continue adding partitions just like before, with a layout as follows:</p> <table border="1"> <tr> <th>Mount Point</th> <th>Size</th> </tr> <tr> <td>/</td> <td>1 GB</td> </tr> <tr> <td>swap</td> <td>1 GB</td> </tr> <tr> <td>/usr</td> <td>8 GB</td> </tr> <td>/usr/local</td> <td>1 GB</td> </tr> <td>/home</td> <td>1 GB</td> </tr> <tr> <td>/opt</td> <td>1 GB</td> </tr> <tr> <td>/tmp</td> <td>1 GB</td> </tr> <tr> <td>/var</td> <td>1 GB</td> </tr> <tr> <td>/boot</td> <td>100 MB</td> </tr> </table> <p>After setting up your partitions, you should notice that there is a little bit of free space left at the end of the drive.</p> <p><img src="http://img42.imageshack.us/img42/3396/capture33.png" /></p> <p>Let's take care of that free space, by adding it to the /home partition. Highlight the /home partition in the list, and click "Edit."</p> <p><img src="http://img541.imageshack.us/img541/4281/capture34.png" /></p> <p>We want to leave everything the way we had it, only selecting "Fill to maximum allowable size" from the section labeled "Additional Size Options." This will add the rest of the free space to the partition.</p> <p><img src="http://img192.imageshack.us/img192/3994/capture35.png" /></p> <p>Finally, our partitions are set up, so click "Next." This will take us to the boot loader configuration, and we can just accept the default option and use GRUB, so click "Next." Next is the network configuration, and we want to accept the default here and use DCHP. Click "Next" again. You should now be at a map where you can select your timezone:</p> <p><img src="http://img834.imageshack.us/img834/8853/capture38.png" /></p> <p>Select your timezone, either from the dropdown menu or by clicking one of the locations on the map. Then click "Next" where you will be asked to setup the root password:</p> <p><img src="http://img811.imageshack.us/img811/2430/capture39.png" /></p> <p>The root user is the administrative user for the system that can change anything, so it is a good idea to set a strong password. Enter your password twice, then click "Next."</p> <p><img src="http://img829.imageshack.us/img829/4356/capture40.png" /></p> <p>Now it's time to select the packages we want to install on the system. I just checked the "Server" and "Server GUI" packages in addition the default GNOME package. If you think there is something else you want, feel free to add it, otherwise, don't worry, you can always add more packages later. If you choose the "Customize now" option, you will be taken to a page where you can do a more detailed package selection. For the sake of speeding things along, we are just going to leave the default, "Customize later."</p> <p>After selecting packages, click "Next." This will take us to a page that will tell us installation is about to begin, so go ahead and click "Next" to get things going.</p> <p><img src="http://img718.imageshack.us/img718/2073/capture42.png" /></p> <p>The installation process takes a bit of time, so now would be a good time to go do something else. After it is finished, the installer will tell you installation is complete, but that's not true. Click "Reboot," and after the system reboots, you will be taken to a configuration phase of the install process. Click "Next" through the "Welcome," "Firewall," "SELinux," "KDump," and "Date and Time" screens, accepting the defaults for each. Now you should be at the screen where you create your regular user that you will be logging in with to do most day to day tasks. It is good to have a user besides the root user to do most work on a system, to prevent accidentally misconfiguration. Go ahead and enter your credentials:</p> <p><img src="http://img838.imageshack.us/img838/5787/capture49.png" /></p> <p>After creating a user, you will be taken to a screen to configure the sound card. It should work, but feel free to test it. After testing, click "Next" and you will be taken to a screen asking for additional CDs. We don't have any additional CDs, so just click "Finish" to complete the installation.</p> <p><img src="http://img832.imageshack.us/img832/2181/capture52.png" /></p> <p>At long last, our system is now ready to use! Login with the user you created, and enjoy the benefits of using Linux as a virtual machine!</p> http://www.writehack.com/2010/10/2/Installing CentOS as a Virtualbox Guest Virtualbox Installation http://www.writehack.com/2010/10/1/Virtualbox Installation <p>The installation of Virtualbox is fairly straightforward. Oracle has done their best to make the installation process as painless as possible.</p> <p>First, point your browser to <a href="http://download.virtualbox.org/virtualbox/vboxdownload.html">http://download.virtualbox.org/virtualbox/vboxdownload.html</a> and grab the latest version. Navigate to where you downloaded the executable package, and run it.</p> <p><img src="http://img840.imageshack.us/img840/9414/vbox.jpg" width="499" height="388" /></p> <p>Click Next, and read and accept the license agreement. </p> <p><img src="http://img42.imageshack.us/img42/2323/vbox2.jpg" width="499" height="388" /></p> <p>This will bring you to a dialog which will list custom install options. For our purposes, it is best to use the default options, so just click Next.</p> <p><img src="http://img842.imageshack.us/img842/1719/vbox3q.jpg" width="499" height="388" /></p> <p>After clicking Next, you will be asked whether or not you would like to create a desktop shortcut and a quick launch icon. Select your preference, and click Next to move on. This will bring up a dialog warning you that installation will reset your network interface. It is just letting you know that during the installation process, you will lose internet connectivity for a few seconds. Click Yes to proceed.</p> <p><img src="http://img121.imageshack.us/img121/2411/vbox5g.jpg" width="499" height="388" /></p> <p>After clicking Yes at this dialog, we are finally ready to begin the actual installation. Click Install, and get on with it already!</p> <p>This will proceed with a very typical install process, with your standard progress bar.</p> <p><img src="http://img819.imageshack.us/img819/2898/vbox7.jpg" width="499" height="388" /></p> <p>During the middle of the installation, &quot;Windows Security&quot; dialogs will pop up asking you if you want to install various drivers. These are necessary for your guest virtual machines to have access to things like USB and network interfaces. Be sure to click Install for all of these.</p> <p><img src="http://img94.imageshack.us/img94/8246/vbox8.jpg" width="534" height="250" /></p> <p>After a few of these security dialogs, installation is finally complete, and we can now proceed to the more exciting process of guest operating system installation!</p> <p><img src="http://img714.imageshack.us/img714/6242/vbox12.jpg" width="499" height="388" /></p> http://www.writehack.com/2010/10/1/Virtualbox Installation Haml Ugly Mode and Other Templating Options In Sinatra http://www.writehack.com/2009/9/26/Haml Ugly Mode and Other Templating Options In Sinatra I was having a problem where haml was breaking the indentation of my code formatted with <a href="http://syntaxi.rubyforge.org/">Syntaxi</a> (a ruby syntax highlighting gem). The solution was to turn on ugly mode to disable haml's indenting. How to do this in Sinatra wasn't exactly clear, but after a little hacking, I figured it out: <code lang="ruby">haml(:index, { :ugly => true })</code> The options inside the curly brackets are haml's template options, of which there are several <a href="http://haml-lang.com/docs/yardoc/HAML_REFERENCE.md.html#options">listed here</a>. For example, if I wanted Sinatra to render a haml template in HTML5 ugly mode: <code lang="ruby">haml(:index, { :ugly => true, :format => html5 })</code> http://www.writehack.com/2009/9/26/Haml Ugly Mode and Other Templating Options In Sinatra