Wednesday, December 29, 2010

The "Find" Command

The find command is not only useful for finding files by name, size, owner, etc, but it is also very powerful in limiting files to which you apply other commands either by using the pipe or arguments to find. For example, to change permissions on files only you can use:


find -maxdepth 1 -type f -exec chmod 644 {} \;


This command will change permissions in the present working directory (-maxdepth 1) to 644 on files only.


To change permissions on directories only use:


find -maxdepth 1 -type d -exec chmod 755 {} \;


This command will also be limited to the present working directory. It will change permissions to 755 on directories only. If you want to control recursion, you can use a different number with the -maxdepth option or leave it out altogether to recurse into all lower directories. Be careful, though, the find command is very powerful and can lead to unexpected and disastrous results if not used properly.

Sunday, December 19, 2010

Change Files on Droid 2

After rooting the Droid 2, there are sometimes files you want to change. You can pay for the Root Explorer app or use the free Android Terminal Emulator with linux commands on your phone. To change the system folder and below from read-only to read-write, do the following:


su
mount -o remount,rw -t yaffs2 /dev/block/mtdblk3 /system
rm /system/app/Gmail.apk
reboot

Saturday, December 18, 2010

Root The Droid 2

This is from other people: (there are other ways, like using adb from the Android SDK)

Visit the market and get the free Android Terminal Emulator installed.
Visit the market and make sure you have Astro installed.
Grab rageagainsthecage.zip with your phone
Use Astro to navigate to the /sdcard/download folder
Long press on the rageagainstthecage.zip file and "extract to this directory"
The 4 files, from the zip file should now be in the /sdcard/download folder (rageagainstthecage-arm5.bin, su, superuser.apk and busybox.
What are we going to do?
Steps 1- 5: Change to the /tmp folder on your phone, copy the file to the tmp directory, change its permissions. run it, and wait.

Step 6: make sure we can proceed

Steps 7 - 14: make system read-writable, copy the superuser. su and busybox files to where they need to go, use chmod to change their permissions, then make the system folder read-only, and exit Terminal Emulator.

If you want to know more about the commands being used here: cd, cp, chmod, mount, and exit are all Linux commands you can look up on your favorite search engine.

Okay, let's do it - type the blue parts in Terminal Emulator

cd /tmp
cp /sdcard/download/rage*.bin /tmp/
chmod 777 rage*.bin
./rage*.bin

This will take some time, just wait for the $ to show up so you know it is done.
Go to Settings > Applications > Manage Applications > Terminal Emulator > and Force Stop the application
Sanity check - Go into Terminal Emulator again and make sure you have a # symbol as a prompt. If so you are ready to proceed.

mount -o rw,remount -t ext3 /dev/block/mmcblk1p21 /system
cp /sdcard/download/Superuser.apk /system/app/Superuser.apk
cp /sdcard/download/su /system/bin/su
cp /sdcard/download/busybox /system/bin/busybox
chmod 4755 /system/bin/su
chmod 4755 /system/bin/busybox
mount -o ro,remount -t ext3 /dev/block/mmcblk1p21 /system
exit

Thursday, December 2, 2010

Setting Up a Separate Data Partition

Now that I went through the process of learning how to move /home to a separate partition, I learn that it isn't always the best practice. Actually, if one is running multiple versions of Linux, it can cause problems because configuration files are stored for each user in the /home folder and they will rarely be compatible from one installation to the next.

Still, I learned a lot and can apply what I learned to an even better alternative—keeping /home on the root partition but having a common, separate /data partition. This way, you can easily backup the configuration files in your small /home folder if you want (although this data won't typically be as important as user documents, etc.)—even back it up to the /data partition—and keep them separate to each installation (if you have multiple on one machine), while storing all of your documents, photos, videos, audio, etc. on the one, separate /data partition. Pretty smart idea I got from the Ubuntu Forums.

I plan to implement this soon, and many of the same principles and steps from moving /home to a separate partition will apply to setting up a separate /data partition.

Wednesday, December 1, 2010

Move Home Folder to Separate Partition

*NOTE* See "Setting Up a Separate Data Partition" before deciding to put your home folder on a separate partition.

There are several benefits to having the home folder (the one in which all user data is kept) stored on a separate partition, not the least of which is making re-installs or new installs cleaner and separate from user data. As with most things on this blog, I learned this from other sources and am consolidating the steps here for documentation's sake and to include small things that were left out of other tutorials.

First, boot from an Ubuntu CD so you are not booted from the drive you need to change. When in the Live CD environment, use GParted to repartition your hard disk drive and create an empty partition to be your home partition, formatted with the same filesystem (i.e. ext3 or ext4) as your root linux installation. I won't cover the details of this process.

Now, for this example, we'll assume your linux root partition is on the hard disk drive /dev/sda and found at /dev/sda5. We'll also assume the new, empty home partition is at /dev/sda7 and both are formatted with the ext4 fielsystem.

Since we booted from Live CD, we must mount the original linux partition and the new one where home will be. We do this by executing the following commands in a terminal:

sudo mkdir /original
sudo mount -t ext4 /dev/sda5 /original
sudo mkdir /new
sudo mount -t ext4 /dev/sda7 /new


Next, we copy the original home to the new home partition, move the original home to home_backup just in case we need a backup, then make a fresh and empty home folder on the root partition into which the new home partition will be mounted:

cd /original/home
find . -depth -print0 | sudo cpio –null –sparse -pvd /new/
sudo mv /original/home /original/home_backup
sudo mkdir /original/home


The above "find" command is one place I ran into problems. The guides I found did not have "sudo" after the pipe, which caused the copy operation to fail due to "permission denied" errors.

Now we need to backup the fstab file, then open it with an editor. (The following uses GEdit graphical editor. You may use your favorite editor as long as you have root privileges. For example, to use a terminal editor, replace the whole gedit line below with "sudo nano -w /original/etc/fstab"):

sudo cp /original/etc/fstab /original/etc/fstab_backup
gksudo gedit /original/etc/fstab


Now add a line to fstab that mounts the home partition into the /home folder on the root partition at boot time:

/dev/sda7 /home ext4 nodev,nosuid 0 2

Another problem I had related to file and folder ownership. I had two users in my home folder, one with user id (uid) 1000 (we'll call him bob). Some of the copied folders and files showed ownership of user "root" instead of uid 1000. To fix this, still booted from the Live CD, I used the following command after ensuring the new home partition was mounted and changing to the proper folder (i.e. "cd /new/bob":

find -user root -print0 | sudo xargs -0 chown 1000:1000

The "find" portion of the above searches the present working directory and below for files and folders owned by "root". The xargs feeds each line produced by the "find" command to chown (change ownership), which then changes the ownership to the uid 1000.

Now you can reboot normally.

Monday, November 22, 2010

Extract Files to Other Folder Using Tar

To extract files from a tarball to a folder other than the current one, simply add -C /path/you/desire/ to the end of the normal syntax. Examples follow.

Extract a zipped tarball:
tar -xvzf ./filename.tgz -C /home/user/jackrabbit/

Extract a bz2 tarball:
tar -xvjf ./filename.bz2 -C /my/desired/path/

Saturday, November 20, 2010

SSH Host Key Error

When trying to ssh into a server you may get the following error:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
5b:9c:15:52:a1:ce:19:80:3b:ad:4b:b2:31:ad:d5:4c.
Please contact your system administrator.
Add correct host key in /home/user/.ssh/known_hosts to get rid of this message.
Offending key in /home/user/.ssh/known_hosts:1
RSA host key for ras.mydomain.com has changed and you have requested strict checking.
Host key verification failed.

If you have reinstalled Linux with OpenSSH, you will get the above error. To confirm the fingerprint of the server, login to it directly if possible and execute

ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub

(To confirm fingerprint of other host keys use:

ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub
ssh-keygen -l -f /etc/ssh/ssh_host_key.pub


To get rid of this problem (if you are certain it it NOT due to an actual security breach):

Remove The Offending Key

Use the -R option to removes all keys belonging to hostname from a known_hosts file. This option is useful to delete hashed hosts. If your remote hostname is server.example.com, enter:

$ ssh-keygen -R {server.name.com}

$ ssh-keygen -R {ssh.server.ip.address}

$ ssh-keygen -R server.example.com


Sample output:
/home/robert/.ssh/known_hosts updated.
Original contents retained as /home/vivek/.ssh/known_hosts.old
Now, you can connect to the host without a problem.