Showing posts with label raspberry pi. Show all posts
Showing posts with label raspberry pi. Show all posts

September 4, 2014

How to set up a private Git server on a Raspberry Pi (or any Linux-based OS, really)

Paying for private Git space sucks. And sometimes some code is really not worth putting out onto the greater web, or really doesn't need to be up there. The solution is to privately host your repository!

A Raspberry Pi is a super cheap, small, power-effective computer, excellently re purposed as a Git server. Start by installing some dependencies - chances are, if you're running Raspbian, you may already have these.
sudo apt-get install wget git-core

It is good practice to set up a dedicated system user, in this case we are calling the user git with a home folder location at /home/git
sudo adduser --system --shell /bin/bash -gecos 'Raspberry Pi Git user' --group --home /home/git git

Enter the user's home folder, and initialize a new bare git repository:
su git
cd /home/git
mkdir newrepo.git
cd newrepo.git
git --bare init

And your Pi is now set up with a shiny new repository, ready to accept some files!

Return to your host machine, and go to the root of the new project that is about to get pushed to the new repository. At this point, it is a good idea to check your .gitignore file to make sure you aren't about to push stuff that shouldn't be version-controlled. Replace the IP address below as needed; or better yet, hostname if that is set up (mine is just running on my local network). We will now tell our host machine where to push to, then will commit the project, and actually push it:
git init
git remote add origin git@192.168.0.xx:/home/git/newrepo.git
git add .
git commit -m "First commit"
git push origin master

If, like me, you already had a git work tree set up, but the old repository is now defunct, you can instead just change the repo location like this:
git remote set-url origin git@192.168.0.xx:/home/git/newrepo.git
git push origin master

This will retain your entire git history, even though you may previously have been pushing to a different repository. If you had the entire history checked out locally before your switchover, that history will be uploaded to the new location as well.

Finally, as a last quality-of-life tweak, upload your public SSH key to your remote user's ~/.ssh/authorized_keys file. This stops the annoying password prompts during every push, but is just as secure.

Lastly, back on the host machine, it might be worth setting Git's case-sensitivity off via git config core.ignorecase false.

September 3, 2014

How to back up a SD card image, i.e. Raspberry Pi OS

Setting up a Raspberry Pi to finally run how you want it, can take a while. And it is super annoying when, for whatever reason, the SD card becomes corrupt, and you need to reinstall everything. Lame. Luckily, there is a tool with which a SD card can be imaged and stored away in waiting, until its triumphant return; it isn't the backup image you need right now, but it is the image you deserve...

Enter Win32 Disk Imager. It is likely something you've used before anyways when setting up your RasPi, because the official documentation speaks highly of its use. For our purposes, we just need a simple backup image:
  1. In the Image File textbox, select your save path and file name
  2. Make sure the right Device (i.e. SD card's drive letter) is selected.
  3. Hit the "Read" button.
That's it! If you ever need to reload this image onto a blank SD card, just load the backed up image and hit the "Write" button instead.

Another related good read here, with suggestions for Mac/Linux.

Installing Baikal CalDAV calendar server on Raspberry Pi

In this post we will set up a lightly prepared Raspberry Pi to serve a calDAV calendar. We have previously looked at doing this on a Synology NAS, and overall, Baikal requires very similar steps taken to work on a different platform such as a Raspberry Pi.

There are a few things we will need right off the bat:

PHP5:
sudo apt-get install php5 php5-xmlrpc php5-curl
Apache2:
sudo apt-get install apache2 libapache2-mod-php5
Sqlite:
sudo apt-get install php5-sqlite

In the /var/www folder, download and extract the "flat" package from Baikal's website:

sudo wget http://baikal-server.com/get/baikal-flat-0.2.7.zip
sudo unzip baikal-flat-0.2.7.zip
sudo mv baikal-flat/ baikal/
sudo chown -R www-data:www-data baikal/
sudo chmod 755 baikal
cd baikal/
sudo chmod 755 Specific/
sudo chmod 755 Specific/db
sudo chmod 755 Specific/db/db.sqlite
sudo touch Specific/ENABLE_INSTALL

Once that's done, load up your Pi's IP address, http://192.168.0.xx/baikal/admin/install, and follow the Baikal install. Refer to this post from the Initialization Wizard steps onward if things get confusing.

September 2, 2014

How to set up SSH, using a Raspberry Pi as an example

SSH is a versatile and secure way of connecting to other devices, regardless of host/client operating system. Here's how it's done. I'll just use my Rasberry Pi as an example of how to do this, but these steps can be applied to any UNIX environment.

  1. See this article if for some reason SSH isn't already enabled on your Pi.
  2. Create your keys on your host machine if you haven't yet (see RSA vs DSA for key type info):
  3. mkdir ~/.ssh
    ssh-keygen -t rsa
  4. It's OK to use blank password defaults, unless you want the extra layer of security.
  5. Copy the public key to your Pi (replace the IP below with whatever your Pi's address is; use ifconfig on your Pi if needed to figure it out):
    scp ~/.ssh/id_rsa.pub pi@192.168.0.xx:~
  6. Connect to your Pi, being prompted still for your password one final time before kicking in your authentication keys:
    
    // SSH in from the host:
    ssh pi@192.168.0.xx
    // Enter your password, for the last time
    // Finally, pipe the SCP'ed public key into the authorized_keys file:
    cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

Now you can log into your Pi from your host machine, securely, without being prompted for a password again.

Getting started with a Raspberry Pi: Some basics

These are some basic steps and resources for getting a Raspberry Pi up and running in very little time, while ironing out some annoyances.

You will need a SD/SDHC card (2-8 Gb is plenty of space) as well as a computer with a card reader, and a Raspberry Pi (duh).
  1. Prepare SD card: If running Windows or Mac, download the SD Association's SD Formatter tool. This tool is recommended by almost any RPi guide worth their salt, and is very lightweight and fast. Excerpts from the official RPi guide:
    • Windows
      1. Install and run the [SD Formatter] on your machine
      2. Set "FORMAT SIZE ADJUSTMENT" option to "On" in the "Options" menu
      3. Check that the SD card you inserted matches the one selected by the Tool
      4. Click the "Format" button
    • Mac
      1. Install and run the [SD Formatter] on your machine
      2. Select "Overwrite Format"
      3. Check that the SD card you inserted matches the one selected by the Tool
      4. Click the "Format" button
    • Linux
      1. We recommend using gparted (or the command line version parted)
      2. Format the entire disk as FAT
  2. Download the latest NOOBS image, or pick your preferred distro.
  3. Unzip the Raspberry Pi image, and copy those files to the formatted SD card.
  4. Insert the SD card into the Pi and connect networking, USB keyboard, HDMI TV/monitor, power etc.
  5. NOOBS will boot up, so select your desired operating system, and hit I to install.
  6. Go grab a drink, because this takes like 10 mins.
  7. Change font size: When it is finally done, you'll need a magnifying glass to see anything on the screen, which sucks. Firstly, you'll be greeted by the console-setup tool. Slog your way to the update command with your magnifying glass; when this is done, we will have the option to change font size.
  8. You may need to log in to your Raspberry Pi at this point, the default username/password is pi/raspberry.
  9. Reconfigure the console-setup tool with:
    sudo dpkg-reconfigure console-setup

  10. Follow the steps, and select a larger font (I personally like 16x32 on my 42" TV), but let's face it, we're gonna be SSH'ing into this puppy most of the time, so this is really just a backup solution, and that we won't get annoyed when we DO need to connect it to a TV.
  11. sudo reboot
  12. Change the default user's password: As in Unix systems, done via the passwd command, or create a new user altogether for better security.



In a following article, I will detail setting up SSH access, and use a Raspberry Pi as an example of how to do this. A few final notes, once you can access your Pi via SSH:

Set up some useful aliases: I usually just uncomment the ll, la and l aliases in the ~/.bashrc file. Reload your terminal profile to pull those aliases in:
su - pi //where pi is my username; substitute if you changed yours.

Proper shutdown: Don't be a dumbass and systematically wreck your Pi by just pulling the plug to turn it off. Power it down properly, either while SSH'ed in, or directly via your plugged-in keyboard:
sudo shutdown -h now