Vagrant: sudo access and the hostsupdater plugin

Bringing up a vagrant machine is as easy as vagrant up.

If you’re a web developer, it would be nice if it were to add the private network IP address to /etc/hosts of the host machine. Thus giving you instant access to http://my-awesome-site.dev/

This doesn’t happen by default, but it is possible with the use of a plugin. The one I like to use is vagrant-hostsupdater.

Install thus:

When you bring up the vagrant machine, it will now automatically add the VM’s name to /etc/hosts.

As /etc/hosts is owned by root (and I hope you aren’t running everything as root), you have to provide sudo access to edit /etc/hosts.

Either you manually enter your sudo password every time you run vagrant up, or you can add some rules to sudoers.

This will work on Ubuntu type systems. Paths to sh and sed may be different on your own system.

Copy/paste the following into /etc/sudoers.d/vagrant and chmod the file to 0440

A similar system can be used if you want to make use of nfs for the file sharing with the VM.

Again, this works for Ubuntu systems, you mileage may vary.

Copy/paste the following into /etc/sudoers.d/vagrant and chmod the file to 0440

You will now be able to use nfs without having to enter your sudo password on each vagrant up and vagrant halt.
 

Basic set-up of a 3com 4500 managed network switch

I’m a PHP developer by trade with a strong Linux background. One thing that has been lacking from my skill set is how networking really works.

In an effort to rectify this, I bought myself a 2nd hand managed network switch from ebay. A 26 port (24 x 10/100mb + 2 x 1gb) “3com SuperStack 3 Switch 4500”.

Flashing lights and noisy fans, I feel like I’m headed in the right direction.

First things first – can I plug it into my router (with DHCP), have it get an IP address and log into the web interface?

No šŸ™

Using nmap to sweep the subnet, that the router manages, returned no results for the known MAC address of the switch.

Even checking the router for connected devices didn’t list the known MAC address of the switch.

If I wasn’t getting into the system via the network, I would have to use the console port instead.

As I didn’t have a null modem cable to hand, and I don’t have an active machine with a d9 serial port, I grabbed something from Amazon:Ā http://www.amazon.co.uk/gp/product/B00HUZ6OMQĀ (NB: does not work with this switch, keep reading)

As I’m a Linux user, I would be playing with /dev/ttyUSB0. And to use that, my user has to be in the dialout group:

Connecting to the serial console should be easy with:

Hooked up the cable, ran the command to bring up the serial interface and switched on the switch.

This, annoyingly vertical, video shows that something happens (watch the green block skit around in the black window) but no text appears:Ā https://www.youtube.com/watch?v=sXVYtClNDYU

EveryĀ different program I tried (screen, minicom, putty) to connect to the device all resulted in the same output.

Thanks to fellow 3com switch ownerĀ Intrbiz, I have been able to borrow a known working cable.

Hooked up between the PC and switch, ran the byobu-screen command and turned on the switch – It lives!

Now that I have a way of talking to the switch, I can configure it in a way so that I don’t need the console cable (as much).

 

Factory reset (this requires the console cable):

We need to factory reset for the following reasons:

  • remove any unknown users
  • restore the admin password to the factory default
  • remove any network configuration set-up by the previous owners
  • set-up our own network configuration

Start a console session and power on or reboot the switch.

  • Hit ctrl+b when prompted. Be quick, you don’t get long.
  • Now in the boot menu, tell the switch to ignore the saved configuration for the next reboot (option 7).
  • Reboot the switch (option 0).

Let it boot normally and wait until something like this appears:

  • Hitting enter will log you in as the admin user.

  • Hit enter again to put your cursor on a new line, not at the end of the debug output line.Ā Enter “save” to save the default configuration over the configuration that was written by the previous owner.

It’s now safe to reboot or power cycle the switch as much as you like and it’ll have the factory default settings.

 

Assign a static IP address to the network switch (this requires the console cable):

  • Ensure that the switch has booted and then connect to the console

  • Enter the system view

  • Switch to vlan 1

  • Set an IP address followed by netmask

  • Set the default route for the switch

  • Return to the user view

  • Save the configuration

 

Enable SSH login (this requires the console cable):

  • Ensure that the switch has booted and then connect to the console

  • Enter the system view

  • Create the public SSH key

  • Configure the authentication mode

  • Enable the SSH protocol for inbound connections

  • Exit the interface configuration and return to the system-view

  • Create a new user for our SSH connections

  • Set the user’s password

  • Give the user SSH access

  • Exit back to the system view

  • Allow the user to login via SSH using their password

  • Exit back to the user view

  • Save the configuration

  • Check that the SSH login works

 

Enable Web login (this can be done with the console cable or an SSH session to the switch):

  • Connect to the switch via the console
  • Change to the system view

  • Switch to the admin user

  • Configuration stuff

  • Return to the user view

  • Save the config changes

 

Reference

Firmware:Ā https://h10145.www1.hp.com/downloads/SoftwareReleases.aspx?ProductNumber=JE045A

Enabling SSH logins:Ā http://h30499.www3.hp.com/t5/Comware-Wireless-Unified-Series/How-To-Enable-SSH-In-3com-4500-Switch/td-p/2318357#.VXLVtd9jPRY

Fixing the web login:Ā http://brittadams.com/blog/2014/08/25/unable-to-log-into-web-interface-3com-4500-switch/

Vagrant hostsupdater plugin without having to enter your sudo password

Vagrant – Great

Hosts Updater plugin – very handy

Having to enter your sudo password when the plugin edits your hosts file – really annoying.

I finally got around to addressing this on my dev system. Looking at the plugin source there are two sudo commands that we need to cater for. One to add hosts and one to remove.

Adding the following to my sudoers file worked a treat:

There are a couple of things to address when you add this to your own sudoers file:
1. Change ‘iain’ to your own user name
2. ‘sed’ might be running from a different path. You can find out by running: which sed

Multi-screen with Ubuntu Unity

This tweet has to have been the most popular thing I have ever said. At time of posting it has gained 80 retweets, 25 favouritesĀ and many replies/questions.

Work recently bought me a new workstation, so the 1st thing I always do is to dual boot with Ubuntu.

Some might consider me an edge case user. Though as a developer, I like a ratherĀ particularĀ set-up. That is, 3 wide screen monitors with the central one rotated 90 degrees for my IDE.

This is something that Windows gets right without having to dig about installing things. While Linux distros have always struggled (in my experience).

Because my tweet gained quite a few questions, I thought it best to reply to them here for everybody to see.

@ankitvad asks what specs. I use for Ubuntu.
Titanium Rimless Glasses from Spex4Less.com
Couldn’t resist, sorry šŸ™‚

Dell Alienware X51
CPU: i7
Memory: 8GB
Graphics card: nVidia GeForce GTX 660
Storage: 1TB HDD (Windows)Ā 120GB SSD (Linux)
Mouse: Logictech M570 trackball
OSs: Window 7 SP1, Ubuntu 12.10 64bit
Monitors: 2 x 22″ Dell, 1 x 22″ LG

All 3 monitors are connected to the one graphics card. Two by DVI and one by HDMI.

As I said, this is a working system from a fresh install without updates being applied or any 3rd party packages installed. So the default graphics driver is doing quite well these days šŸ™‚

The only downside to this is that the default graphics driver is dog slow and won’t let me play games on Steam šŸ˜‰ The next step will be to get the nVidia binary driver working.

Chroot SFTP home dir

Example user ‘iain’

Adding extra fields to FOSUserBundle / SonataUserBundle

Sadly, this isn’t really documented (at time of writing).

Adding new protected variables to your User.php entity will not actually create database entries when you try to do a doctrine:schema:update.

While one still needs to have the protected variables in this entity class, along with getters and setters, the actual creation work is within UserBundle/Resources/config/doctrine/User.orm.xml

Here is an example for adding a foreign key:

Ubuntu 12.04 LTS on Bytemark VM

If, like me, you can’t wait for Ubuntu 12.04.1 to be released, you can force an upgrade of your favouriteĀ OS OS.

If you are going from the previous LTS 10.04, then the -d is important. Otherwise there would be no upgrade to offer.

The upgrade process should go smoothly enough. Except for when it comes to the kernel.

Bytemark VM’s make use of kernels that sit outside of the VM itself.

I didn’t realise this until I tried to fix syslog doing this:

Even if you update grub, it’ll be ignored. Instead, follow the instructions here:Ā http://www.bytemark.co.uk/support/technical_documents/kernelchange?tags=VirtualMachine

At the time of writing, I choseĀ 3.2.0-kvm-i386-20110111 as it’s fairly close to the kernel being used by other Ubuntu 12.04 machines I have.

Your mileage may vary, but I hope this helps somebody else with the same trouble.

Updating a WordPress database with new domain details

Further to my original quick db update queries, I found the need for a moreĀ in-depthĀ approach.

A WordPress site my father works on has weekly backups, but he wanted to see that they actually worked.
To do so, I set up a subdomain vhost on my bytemark server and set about getting the back to work with it.

One of the most annoying/lazy/strange things about WordPress is that it uses serialised arrays within database fields. This makes a simple search and replace fail if you need to do anything more than update the two fields here.

This script works for the database set-up for my father. It may require further tweaks if additional plugins store absolute paths or URLs.

It also could do with a heavy dose of refactoring, but it works for my needs at this point in time šŸ™‚