Tuesday, September 25, 2012

Clearing partitions a USB stick using Windows 7

I've been trying to access the entire size of an 8GB USB stick.  At one point I must have created multiple partitions on the USB stick when using it with Linux machines.  I didn't know that Windows can only access the first partition on a stick.  The primary partition I had created was rather small, about 250MB.  I wanted to use my USB stick to copy a file that was several GB so this amount of space wasn't going to work.  I looked and looked for answers and finally found out a way to do it without installing 3rd party software.

WARNING: These steps will delete the entire contents of the USB stick!

Open a command line and run the following:
      (accept the request for privileges)
 DISKPART> list disk
      (determine which disk is your USB stick, for me it was Disk 1)
 DISKPART> select Disk 1
 DISKPART> clean
 DISKPART> create partition primary

This cleared the disk and gave me one large partition that can now be formatted.  Instead of trying to partition it from diskpart, I decided to just use the Disk Management gui using these steps:
  1. Start -> Run : diskmgmt.msc
  2. Right click on the usb disk in the gui and select Format
  3. Select the file system you want to use, I used FAT32 so it would also work in Linux
  4. Press OK
I now have a working USB stick that has all disk space available and no partitions hidden from Windows.

Tuesday, September 4, 2012

Remote Desktop to a machine behind a NAT

It is nice to be able to connect to someone else's Linux machine remotely so you can help them with issues.  The problem I run into frequently is that the remote user is not very technical.  So when a remote machine is behind a NAT stepping them through configuring their router to do port forwarding is not easy.  The difficulty is compounded when you are unfamiliar with their router.  Here is a solution I came up with that just requires the remote user to run a quick script that will reverse tunnel into my machine so I can connect via SSH or VNC into theirs.  The VNC is nice because then they can watch what you're doing or you can help them with something they're doing.

Setup Local Machine:

  1. You'll need to setup your local machine to be running an ssh server.  Then setup your router to port forward port 22 to your local machine.
  2. Create a guest user on your local machine for the remote machine to login to.  I created the guest so someone smart on the other end doesn't have too many permissions on my machine.  You could just use your login if you don't mind giving the remote user your password or them possibly using the password-less login to your machine.

Setup Remote Machine:

  1. Setup VNC server for remote desktop viewing.  Setup steps I used can be found here: http://ubuntuforums.org/showthread.php?p=10744047
  2. Create an executable shell script that can easily be run by novice users on the far side.  This shell script will do 2 reverse ssh tunnels into the local machine.  The first tunnel is for ssh access and the second is for VNC.  Script contents:
    • vi reverseSSH.sh
    • Paste in the following:
    #! /bin/bash
    echo "Setting up secure tunnels for Remote Desktop help."
    echo "Press Ctrl + C when you are finished to close connections."
    ssh -R 12323:localhost:22 -R 12324:localhost:5900 guest@$1 -N
    • Run chmod +x on the newly created script

Connecting the local and remote machines

  1. Determine the local machine's external IP address.  This can easily be done by visiting this site: http://www.whatismyip.com/
  2. Run the script created above from the remote machine.
    • ./reverseSSH.sh [ip address found in previous step]
  3. Now you should be able to ssh into the remote machine from the local machine using:
    • ssh username@localhost -p 12323
    • This will require you to know a username and password to login on the remote machine
  4. In addition you can connect via VNC to the machine with these: localhost: 12324
    • The way I have it setup, a remote user will need to be logged in and acknowledge the remote desktop request.  You can change this in the remote desktop settings if you prefer.
  5. (Optional) Setup password-less login from the remote machine to the local by running the following while ssh'd on the remote machine.
    # ssh-keygen -t rsa
        Press "Enter" for the next 3 prompts and accept the defaults
    # ssh-copy-id -i ~/.ssh/id_rsa.pub guest@local.server