Booting linux from USB

If you want to try out linux on your own computer (without trying it in a VM), you can easily boot linux from a USB-stick. This also comes in handy if you want to retrieve info from a harddisk on a computer that you don’t have credentials for (if the disk is not encrypted), want to run boot-recovery (in case bootmanager stops working) etc.

Anyways, what you need to accomplish this is a USB-stick, internet and a computer.

We need some software that can write an iso (diskimage file) to a USB-stick, and for this we’re going to use unetbootin. Get unetbootin from https://unetbootin.github.io/ or use a package manager to install it from your repository of you’re on linux. Install unetbootin following the directions in the installer.

When unetbootin is installed you should find and download a distribusjon of linux. I would recommend starting with Linux MintUbuntu or Fedora. The easiest one to start with and the one most simular to Windows GUI-wise is probably Linux Mint, but all three distros can be downloaded with another desktopmanager than the default one.

When you have downloaded your distro of choice, insert your USB-stick and open unetbootin. It should look something like this:

unetbootin main windos

From this window, select diskimage (which is what you just downloaded) and click the three dots to the far right to browse for your diskimage. When you have found and selected your diskimage, make sure that Type is set as USB Drive, and not harddisk. Select your drive and click OK.

It probably takes a minute or two until this process is completed, and when it is done you can reboot your computer.

When your computer is booting up it has a list of devices it looks in to find a operating system. Normally for most computers it starts looking at the harddrive, and it usually finds an OS there, so no other devices are checked. What we want to do now is to make it look at the USB-stick before it looks at the harddrive. To do this you have to access the BIOS or UEFI, depending on your motherboard and how old your computer is. The steps are quite similar, but you might not be able to get to the UEFI config menu by pressing a key or key combo during startup. If this is true for you, follow these steps:

  1. Click the Start menu and select Settings.
  2. Select Update and Security.
  3. Click Recovery.
  4. Under Advanced startup, click Restart now. …
  5. Select Troubleshoot.
  6. Choose Advanced options.
  7. Select UEFI Firmware Settings.
  8. Click Restart to restart the system and enter UEFI (BIOS).
If you have BIOS, try pressing F2, F8, F12 or Ctrl+Alt+Delete during startup before the operating system logo shows. If your computer is showing a splash screen with the motherboard or manufacturer-logo, you can usually see a key combination somewhere on the screen as well.
When you have successfully entered BIOS/UEFI, look for a tab labeled BOOT. Your BIOS/UEFI should look somewhat similar to this:
bios
From the BIOS boot tab, change the order of the bootable devices so that the USB-stick is the first one to be looked for.
bios boot
Then Save and Exit, and your computer will restart. If your USB-stick is inserted while rebooting it will prompt you with a screen where you can select to try Linux without installing. Choose that option and press Enter, and your computer will boot Linux.

How to turn on developer options on Android Lollipop

I recently got back from Greece, and while I were there an old idea got back into my notebook. I will present it to you when the time is right (and the prototype is ready), but for now, I will present a short video about Android and how to turn on the developer options.

The reason to turn on the developer options is to be able to install APK’s from untrusted sources. This sound scary, but it only means that you will be able to install Android applications that are not downloaded from Google Play. So if you or your friend is developing apps for Android and want to test it on your own physical device, you want to turn on the developer options.

This time I will present the how to with a video, and I’m thinking of making more videos showing how to use Android Studio and basics of Android programming. Feel free to browse my channel on youtube as more content is on its way.

 

And that’s it! Please give me any thoughts or comments in the comment section below!

Getting public ip from home network

I’m using ssh on my raspberry pi, but to access it when I’m away from my home network I’ve got to have a public ip. My ISP doesn’t provide static ip for private users, so it may change whenever my ISP feels the need to do so. This of course means that I might not be able to access my raspberry pi over ssh if my ISP changes my ip address.

First of all, what is my public ip? You can find it at this address: http://www.whatsmyip.org/. Ok, but we obviously can’t check the IP from our home network when we’re not at home, right? So let’s automate it with python!

First of all, the link above gives a lot of information, but for this script we only need the ip. We have another address that provides just that: http://ip.42.pl/raw. As you can see, it only states the ip, nothing else.

To read the ip from a webpage we need to import urlopen from urllib2 like this.
from urllib2 import urlopen.
This enables us to do this:
public_ip = urlopen('http://ip.42.pl/raw').read()
the public_ip variable now contains our most recent public ip. Great!

What we’re interested in is if it has changed since the last time this script was called. To check that we need to store the ip somewhere. Since we’ll only store the last public ip, it is sufficient with a text-file for this. If you were to store historical data, like all ip addresses assigned to your network by your ISP and when it was changed I would suggest you use a database like mongodb.

Back to our script, we want to store the ip in a file called “publicip.txt”. On linux, the file ending is not necessary, but I like to include them for readability.
filename = 'publicip.txt'
We also need to import os with:
import os
This is because when we’re running the script, we want to check if the file already exists.

if os.path.isfile(filename):
  file = open(filename,'r+')
  old_ip = file.read()
  file.close()
else:
  file = open(filename,'w')
  file.write(public_ip)
  file.close()


What this snippet does is that it checks if the file exists. If it does, it opens it and tries to read it to get the stored ip and saves it in memory as the variable old_ip. If the file doesn’t exist it opens the file to write to it (this also creates the file). It then writes the ip we got from the website to the file and closes the file.

Ok so far, but we still have to make logic for if the file exist and contains an ip different from the one we got from the website.

if public_ip != old_ip:
  file = open(filename, 'w')
  file.write(public_ip)
  file.close()

This snippet basically says that if the public_ip is different from the old_ip, we open the file to write to it (the ‘w’ parameter truncates the file, meaning that the file is cleared of all contents. If you want to append, use ‘a’). We then write the new ip and close the file.

Awesome! But we also want to receive some sort of message that contains the new ip, right? Let’s use mail.

First, we need to import some libraries:
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib

Then we need to open a connection to a smtpserver. We’ll use gmail for this script.
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()

Next, we’ll provide some mail parameters:
username = 'themevik89@gmail.com'
password = 'somethingsupersecretaboutsocks'
fromaddr = 'themevik89@gmail.com'
toaddr = 'themevik89@gmail.com'
subject = 'Public ip changed!'

Username and password should be somewhat self explanatory. the fromaddr and toaddr are addresses, in this case from me to me. Subject is going to be used for the subject-line in the mail. Using the gmail smtp server might require you to do some additional steps when creating this script, namely creating what is known as a app-password. You can find information about it here: https://support.google.com/accounts/answer/185833. It is a password for your app that doesn’t need 2 way authentication or captchas. It is perfect for this kind of automation scripts, but might provide a security risk (since it is the only authentication needed to access your google account).

The next part is creating the mail, parsing it to text and sending it like this:
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
body = 'Your public ip has changed and is now: '+public_ip
msg.attach(MIMEText(body,'plain'))
server.login(username, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)

Here we construct a msg-object of the type MIMEMultipart. It contains values for sender (‘From’), receiver (‘To’) and subject (‘Subject’). We also create a body containing a short message and the new public_ip, convert it to MIMEText and attach it to the message. Then we convert the msg from MIMEMultipart to string and send it using the server-object we created earlier.

You can see the whole script on my GitHub-page: https://github.com/TM89/public-ip-finder.

When the script is called and the ip has changed, I receive an email looking like this.

Success! Public ip changed and message received from script

Email received from script

To run it on timed intervals, check out this post: Running script using crontab on Ubuntu

And that’s that! Let me know what you think in the comment section below!

Running script using crontab on Ubuntu

When playing around with my raspberry pi I sometimes want to automate scripts and make them run on timed intervals. This is a perfect job for crontab, so I will try to give a short explanation of how I use it here.

In a terminal window, run: crontab -e where -e is for edit. If no crontab exist, it will create one. If you haven’t used crontab before, you will be prompted with this:

Crontab editor prompt

Crontab editor prompt

If you are unfamiliar with these, I suggest using nano (choice 2). I recommend learning vim.
When you have chosen your editor, a file is opened in your chosen editor. I will assume you have chosen nano. It should look somewhat like this:

crontab file opened in nano

crontab file opened in nano

If you have a look at the last line in that file, that is the command running the script.
The first statement tells it to run every 15 minutes. The 4 trailing stars are for other timeattributes.

The order of the five stars is as follows:

  1. Minutes (0-59)
  2. Hour (0-23)
  3. Day of month (1-31)
  4. Month (1-12)
  5. Day of week (0-6, Sunday = 0)

The next part is the command to run. I’ve used full paths, but it is not necessary.
When you have added your job, you can save using Ctrl+O, then exit using Ctrl+X. Your crontab should now run in the interval you have specified.

You can read more about it here: http://www.adminschoice.com/crontab-quick-reference

And that’s all! Tell me what you think in the comment section below!

Accessing FTP on Raspberry Pi from Nautilus

If I’m making scripts on my laptop and I want them to run on my Raspberry Pi, I can either copy them word by word over ssh, or use a ftp-server!

Assuming you use Ubuntu and Nautilus (default file manager), you press file -> connect to server. This will open a dialogbox:

connect to server

As you can see, my pi is my only recently connected server. Hit “Connect” or “Enter”. This will prompt you for username and password:

usn pass

Enter the correct username and password for your device and hit “Connect” or “Enter”. Nautilus should now open the raspberry pi as a folder and you can click, drag and copy files as you please!

Tell me what you think in the comment section below!