Specifying the bash shell as the default android terminal shell

If you like me, prefer the bash shell over the sh shell, you will have compiled or copied the bash binary to your android system and started running it manually by running bash.

You may have noticed that your .bashrc doesnt get imported on running the shell. This is because by default .bashrc doesnt get imported.

To import .bashrc, you need to add the following line to /data/.bash_profile:

source /data/.bashrc

And add the relevant lines to /data/.bashrc


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.
Midnight commander (mc) shows garbled text characters instead of graphics

Midnight commander (mc) shows garbled text characters instead of graphics

On a Debian ssh shell running via Putty on Windows 8, I started getting this weird display on midnight commander:

MWSnap009 2013-05-06, 21_29_08

 

To fix this,

In Putty>Load Session

Putty>Window>Translation>Remote character set>UTF8

MWSnap010 2013-05-06, 21_29_55

Also export the following variable in your .bashrc file:

NCURSES_NO_UTF8_ACS=1

Save the session. Exit and restart Putty. mc is fixed now.

MWSnap011 2013-05-06, 21_31_41

Credits: 
http://www.andremiller.net/content/getting-midnight-commander-line-drawing-work-putty

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Cannot sftp, can ssh and scp

I’d noticed an issue on my server where it allows me to login with ssh, or transfer files with scp or rsync, but would not allow me to use Filezilla to transfer files using sftp protocol

My /var/log/auth.log showed the following:

Mar  7 05:31:03 gambit sshd[28323]: Server listening on 0.0.0.0 port 22.
Mar  7 05:31:03 gambit sshd[28323]: Server listening on :: port 22.
Mar  7 05:31:09 gambit sshd[28310]: pam_unix(sshd:session): session closed for user root
Mar  7 05:31:14 gambit sshd[28329]: Accepted password for root from 59.93.41.155 port 55173 ssh2
Mar  7 05:31:14 gambit sshd[28329]: pam_unix(sshd:session): session opened for user root by (uid=0)
Mar  7 05:31:15 gambit sshd[28329]: subsystem request for sftp
Mar  7 05:32:52 gambit sshd[28323]: Received signal 15; terminating.
Mar  7 05:32:52 gambit sshd[28352]: Server listening on 0.0.0.0 port 22.
Mar  7 05:32:52 gambit sshd[28352]: Server listening on :: port 22.
Mar  7 05:32:55 gambit sshd[28329]: pam_unix(sshd:session): session closed for user root
Mar  7 05:33:00 gambit sshd[28358]: Accepted password for root from 59.93.41.155 port 55198 ssh2
Mar  7 05:33:00 gambit sshd[28358]: subsystem request for sftp

I checked my sshd_config at /etc/ssh/sshd_config. The line I was looking for was:

PermitRootLogin yes

However that was already set.

So I was in a delimma. I couldnt find out at all, what the issue was, until I read this comment by mdpc on Server Fault, where he says:

Another issue that I have noticed, is that if your login startup files generate any sort of output, scp and sftp fail. Be sure you bracket things that output to the terminal in your startup scripting using a test on the variable $?prompt if it is non-zero you talking with a terminal.

This made me remember that I wasnt getting this issue immediately after installed a new OS, however only after I’ve worked on it for a while. So I understood that it was because I had .bashrc set to output a few things. So if .bashrc output anything to the terminal, an sftp would fail.

Moving .bashrc fixed the issue.

In fact, once I became aware of the issue, it was easy to find that this situation was rather well documented, in no less than the openssh

http://www.openssh.org/faq.htmlFAQ

2.9 – sftp/scp fails at connection, but ssh is OK.

sftp and/or scp may fail at connection time if you have shell initialization (.profile, .bashrc, .cshrc, etc) which produces output for non-interactive sessions. This output confuses the sftp/scp client. You can verify if your shell is doing this by executing:

ssh yourhost /usr/bin/true

If the above command produces any output, then you need to modify your shell initialization.

There are many solutions you can use, and all of them involve checking what kind of shell you’re running. If it’s a non-interactive shell like xterm, you execute .bashrc. If it’s filezilla, you dont.

So you first rename .bashrc to .bashc_real

Then you insert one of the following code into a new file .bashrc:

if [ "$TERM" = "xterm" ]
then
   source .bashc_real
fi

OR

if [ "$TERM" != "dumb" ]
then
   source .bashc_real
fi

OR

if [ "$SSH_TTY" ]
then
   source .bashc_real
fi

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Fix for “ssh-add: Could not open a connection to your authentication agent.”

You might have tried adding a key with:

ssh-add .ssh/id_rsa

only to get this error:

ssh-add .ssh/id_rsa
Could not open a connection to your authentication agent.[/code]
If this happens, it is because your ssh agent is not running in the terminal.

Run this:
exec ssh-agent bash

which will cause it to resource your .bashrc files and start the agent.

Now try readding the key again:

ssh-add .ssh/id_rsa
Identity added: .ssh/id_rsa (.ssh/id_rsa)

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

crontab – Specify default editor

To change the default vim editor to emacs:

In your ~/.bashrc file, set the following:

export VISUAL=emacs
export EDITOR=emacs

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Alert by email when root user logins by shell

The following command added to ~/.bash_profile will alert a specified address when a root user signs in.

echo 'ALERT - Root Shell Access () on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" [email protected]
source .bashrc

.bashrc is sourced because, at least on Debian squeeze, .bashrc isnt executed if .bash_profile exists.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

.bashrc file

My customized .bashrc file follows:

alias l='ls -lh --color'
alias la='ls -lah --color'
alias download='curl -c - -O'
#PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;37m\]'

#Blue prompt
PS1='\[\e[1;34m\][\u@\h \W]\$\[\e[0m\]'

#Green prompt
#PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
PATH=$PATH:~/bash-advanced-scripts:~/toolchains/linux-x86/bin:/home/droidzone/android/adb

#Path to SDK
PATH=$PATH:~/android/envt/SoftwareDevelopmentKit/sdk/tools:~/android/envt/SoftwareDevelopmentKit/sdk/platform-tools

#Path to javac and java
PATH=$PATH:~/android/java/jdk1.7.0_09/bin

#apktool
PATH=$PATH:~/android/Apktool

#Java envt
JAVA_HOME=~/android/java/jdk1.7.0_09
export GREP_OPTIONS="--color -i -n"

d=.dircolors
test -r $d && eval "$(dircolors $d)"

My root bashrc /root/.bashrc is:

alias l='ls -l --color'                                                                                                                
alias download='curl -c - -O'                                                                                                          
#PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;37m\]'                                               
PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\]'                                                                                                
#'\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '                                                                                                  
PATH=$PATH:~/bash-advanced-scripts:~/toolchains/linux-x86/bin:/home/droidzone/android/adb                                              

#Path to SDK                                                                                                                           
PATH=$PATH:~/android/envt/SoftwareDevelopmentKit/sdk/tools:~/android/envt/SoftwareDevelopmentKit/sdk/platform-tools                    

#Path to javac and java                                                                                                                
PATH=$PATH:~/android/java/jdk1.7.0_09/bin                                                                                              

#apktool                                                                                                                               
PATH=$PATH:~/android/Apktool                                                                                                           

#Java envt                                                                                                                             
JAVA_HOME=~/android/java/jdk1.7.0_09

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Set .bashrc for Android ARM cross compiled bash

Set .bashrc and copy to / (root folder) and, /home ($HOME)

Use a default shell with adb

adb shell /system/bin/bash

I tried changing the default shell with the console process

[email protected]:~/kernel/bootimg/ramdisk$ grep -n 'console' init.rc
168:    copy /proc/apanic_console /data/dontpanic/apanic_console
169:    chown root log /data/dontpanic/apanic_console
170:    chmod 0640 /data/dontpanic/apanic_console
176:    write /proc/apanic_console 1
636:service console /system/bin/sh
638:    console
642:service console_user /system/bin/sh
644:    console
650:    start console
653:    start console_user

To:

[email protected]:~/kernel/bootimg/ramdisk$ grep -n 'console' init.rc
168:    copy /proc/apanic_console /data/dontpanic/apanic_console
169:    chown root log /data/dontpanic/apanic_console
170:    chmod 0640 /data/dontpanic/apanic_console
176:    write /proc/apanic_console 1
636:service console /system/bin/bash
638:    console
642:service console_user /system/bin/bash
644:    console
650:    start console
653:    start console_user

Create a boot.img and flash the new kernel..


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Fixing the error “warning: setlocale: LC_CTYPE: cannot change locale”

Recently I was granted build server access on Goo. While trying Bash completion, I encountered this error. Every time I tried to use Tab key for completion, this error message would get added to my typed command. It didnt interfere with the actual command execution. Bash seemed to be totally oblivious to the command line getting hijacked in this fashion. But for the user, me, it was an eyesore.

The fix was too simple. I simply needed to add the following to my ~/.bashrc file:

export LC_CTYPE="en_US.UTF-8"

Thanks to bergspot


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Setting up adb on Linux for Android

If you install adb somewhere and try to run it, you get the an error stating “Insufficent permissions”. So we need to setup a couple of steps to get adb ready to work on Linux with our device.

1. Install Android SDK (Instructions found elsewhere)
First install Android SDK to someplace and set its path in ~/,bashrc

2. Setup path to adb
Edit bashrc and add the following line after the normal PATH statement:

PATH=$PATH:/pathto_adb

Set paths like this:

kate ~/.bashrc

Eg:

PATH=$PATH:~/android/sdk:~/android/android-ndk-r6b:~/android/sdk/tools:~/android/sdk/platform-tools:~/android/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin:~/android/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/arm-linux-androideabi/bin[/code]
3. set up permissions

On Android Lollipop, you need to enable adb from Developer options:
First enable developer mode, by clicking on build version in About menu multiple times.

Next, under Debugging>Android debugging, toggle it on.
[email protected]:~$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 003: ID 0424:2512 Standard Microsystems Corp. USB 2.0 Hub
Bus 004 Device 002: ID 046d:c03d Logitech, Inc. M-BT96a Pilot Optical Mouse
Bus 001 Device 004: ID 0bb4:0c87 High Tech Computer Corp. Desire (debug)
Bus 001 Device 005: ID 0781:5406 SanDisk Corp. Cruzer Micro U3

The important part is this: Bus 001 Device 004: ID 0bb4:0c87 High Tech Computer Corp. Desire (debug)

Note: ID 0bb4:0c87

Or it might be:

[[email protected] ~]$ lsusb | grep -i 'desire'
Bus 001 Device 007: ID 0bb4:0ff9 High Tech Computer Corp. Desire / Desire HD / Hero (Charge Mode)

Here, note

0bb4:0ff9[/code]
N.B: The ID 0bb4:0c87 (or 0bb4:0ff9 depending on the PC) is unique for the Desire. So you can just as well copy and paste into the permissions file.

For a Samsung Galaxy Note 2, it is:
[[email protected] ~]$ lsusb
Bus 001 Device 009: ID 04e8:6860 Samsung Electronics Co., Ltd 

For one plus one, it is:
Bus 002 Device 008: ID 05c6:6764 Qualcomm, Inc. A0001 Phone [OnePlus One]

For Oneplus 3, locating it was slightly more difficult:
First you have to enable Developer mode (Clicking on Build version 5 times), and then toggle USB debugging to On.

lsusb showed:

[email protected]:~$ lsusb
Bus 002 Device 014: ID 2a70:9011
Bus 002 Device 004: ID 1058:0820 Western Digital Technologies, Inc. My Passport Ultra (WDBMWV, WDBZFP)
Bus 002 Device 003: ID 046d:c534 Logitech, Inc. Unifying Receiver
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

I couldnt get any clue as to which one was OnePlus 3.
So I disconnected and reconnected the device, and took a look at dmesg logs:
dmesg:

[email protected]:~$ dmesg
[22373.685139] usb 2-1.4: USB disconnect, device number 13
[22377.722778] usb 2-1.4: new high-speed USB device number 14 using ehci-pci
[22377.816588] usb 2-1.4: New USB device found, idVendor=2a70, idProduct=9011
[22377.816598] usb 2-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[22377.816603] usb 2-1.4: Product: Android
[22377.816607] usb 2-1.4: Manufacturer: Android
[22377.816611] usb 2-1.4: SerialNumber: 87a9854a
[22377.820830] usb-storage 2-1.4:1.1: USB Mass Storage device detected
[22377.821088] scsi host15: usb-storage 2-1.4:1.1
[22378.819918] scsi 15:0:0:0: CD-ROM            OnePlus  Device Driver    0318 PQ: 0 ANSI: 2
[22378.829944] sr 15:0:0:0: [sr1] scsi-1 drive
[22378.830294] sr 15:0:0:0: Attached scsi CD-ROM sr1
[22378.830507] sr 15:0:0:0: Attached scsi generic sg4 type 5

Thus from idVendor=2a70, idProduct=9011, I found out that the line ID 2a70:9011 referred to Oneplus 3

Create/Edit /etc/udev/rules.d/99-android.rules

gksu gedit /etc/udev/rules.d/99-android.rules (Gnome)
kdesudo kate /etc/udev/rules.d/99-android.rules (KDE)

 

Or use your favorite editor.

SUBSYSTEM=="usb", ATTRS{idVendor}=="0bb4:0c87", SYMLINK+="android_adb", MODE="0666" GROUP="plugdev"
TEST=="/var/run/ConsoleKit/database", 
RUN+="udev-acl --action=$env{action} --device=$env{DEVNAME}"

For OnePlus one:

ATTR{idVendor}=="05c6", ATTR{idProduct}=="6764", SYMLINK+="libmtp-%k", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"
sudo service udev restart

Now unplug and then reinsert the Device to the usb connection.

Now, adb devices shows my device

On Ubuntu 16.04, and my Oneplus One all of this is no longer necessary. The steps are:
sudo apt install android-tools-adb
OPO:
Connect phone in MTP mode.
Developer options (enable it by multi-tapping About:Android version) > Android debugging: On
Accept the “Trust computer” dialog
Test with

adb devices

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.