Linux

This tutorial requires your phone to be rooted!

This testdrive was executed on the following device:

Phone: Samsung J5 Prime SM-G570M

System: Nougat 7.0

Internal memory path is: /sdcard

Better Android Shell

Accessing the Android's shell for the first time can be disappointing. One is expecting a fully featured shell or at least some history functionality. But thats not the case. It is a pretty standard busybox shell with no color, no nice prompts, no history, just a raw shell! More importantly no nano, dammit!

I'll help you setup a nicer shell, with nano editor, some colors, aliases, custom commands and a history.

But we'll will not cover shell connectivity. To access the Android's shell you can use any one of the many sshd server apps in the Play Store or adb. I recommend the Server Ultimate Pro app. You can buy it here:

https://play.google.com/store/apps/details?id=com.icecoldapps.serversultimatepro

It is a paid app but well worth it. Clean design, easy to use, reliable and it has servers for services you might not even head about it.

In fact, at the writing of this tutorial it supported the creation of servers with the following protocols:

Caddy, CVS, DC Hub, DHCP, UPnP, DNS, DDNS, eDonkey, Email (POP3 / SMTP), FTP Proxy, FTP, FTPS, FTPES, Flash Policy, Git, Gopher, HTTP Snoop, ICAP, IRC Bot, IRC, ISCSI, Icecast, Lighttpd, LPD, Load Balancer, MQTT, Memcached, MongoDB, MySQL, NFS, Node.js, NTP, NZB Client, Napster, Nginx, PHP, PXE, Port Forwarder, RTMP, Remote Control, Rsync, SIP, SMB/CIFS, SMPP, SMS, Socks, SFTP, SSH, Server Monitor, Styx, Syslog, TFTP, Telnet, Time, Torrent Client, Torrent Tracker, Trigger, USB/IP, Unison, UPnP Port Mapper, VNC, VPN, Wake On Lan, Web, WebDAV, WebSocket, X11, XMPP

You can download a 7 day trial of Servers Ultimate here:

https://play.google.com/store/apps/details?id=com.icecoldapps.serversultimate&hl=en

Download Nano

I did not build this pre-compiled version of nano, but works just the same! It will suffice.

You can download it here: nano 2.2.6 arm.zip

Setup nano and it's support files

Here we'll copy nano's compressed zip file to the devices's internal storage, access the shell, unzip it and setup nano.

So copy the nano226arm.zip file to your phone's internal storage via your favorite method and connect to the shell. I am assuming here the path for the internal storage is /sdcard.

We need to su before we issue these commands to allow the proper permissions to be applied to the extracted nano files and also to remount the system partition. So su first!

su
cd /sdcard
unzip nano226arm.zip

This will create a directory called system in the sdcard directory, the nano files will be inside of it.

Now we need to remount the system partition as read write so we can copy stuff there:

mount -o rw,remount /system

Move nano's binary and support files to their proper paths:

mv /sdcard/system/xbin/nano /system/xbin
mv /sdcard/system/etc/terminfo /system/etc

Clean-up! Delete the rest of the system folder that was created during the extraction process. Be careful not to delete the /system folder instead!

rm -rf /sdcard/system

Make nano's binary executable:

chmod +x /system/xbin/nano

Everything is in place for nano to work, but since we have no terminfo variable set in our shell nano won't start. We'll set these variables in a ash.rc file later on.

For now execute the exports manually to test nano:

export TERMINFO=/system/etc/terminfo
export TERM=linux
nano

It should work. To exit nano press CTRL+X

Unless you have a absurdly small terminal window on your PC you'll notice that nano's screen size is really small, smaller than your terminal window in fact.

We can fix that setting up stty's column and row sizes to match our pc's default terminal size. Mine is 32 rows and 105 columns. So set the stty size like this:

stty rows 32 cols 105

Try opening nano again. Now the size of it should fit the size of your terminal.

Of course as soon as you exit the shell the stty size and the exported environment variables will be lost. To prevent this from happening at each login we'll make the system load a profile file with all the variables and aliases we need, and we'll do that using the ash shell, not the sh one.

Setup ash shell and it's rc file

The default shell when you login is sh. The simplest way to use the ash shell instead of sh is to call ash from within the default sh every time we login.

If you run the ash shell command with the -l flag it will load the /system/etc/profile rc file. By the way that file did not existed on my Galaxy J5 Prime (Nougat).

My approach to this is to keep all these bash support files in one place inside the internal sdcard of the device. This way I can access and edit them on my pc with easy. It is also easier to make backups of these files if they are right there in the sdcard directory.

So let's create a directory that will hold these files in the internal storage sdcard directory. I'll name it System:

mkdir /sdcard/System

Create an empty history file, so we can set the HISTFILE variable. I've added the lame .txt extension to the file in order to open it easily when double clicked.

touch /sdcard/System/history.txt

Now create a file in your PC called ash.rc and either paste the code below in it, or create your own aliases and shell stuff.

# BASH SCRIPT

alias ..='cd ..'
alias ls='ls -lsahF'
alias la='ls -ACk'
alias ns='netstat -alW | sed -n -e '1,/unix/p''
alias profile='nano -w /sdcard/System/ash.rc'
alias p='ping -c 3 $1'
alias rmd='rmdir $1'
alias reload='source /system/etc/ash.rc'
alias maclean='find . -name "._*" -exec rm -v {} \; && find . -name ".DS_Store" -exec rm -v {} \; && find . -name "__MACOSX" -exec rm -rv {} \;'
alias psg='ps -A |grep -i "$@"'
alias pause='kill -STOP "$@"'
alias unpause='kill -CONT "$@"'
alias nano='nano -w $1'

h () { history | grep "$*" | cut -c 8- ; }
gr () { grep -r "$*" ./ ; }
k9 () { for A in $(ps -A | grep $* | grep -v grep | awk '{print $1}') ; do kill -9 "$A" ; done ;}
mkd () { mkdir -p -- "$1" && cd "$1";}

export EDITOR='nano'
export TERMINFO=/system/etc/terminfo
export TERM=linux
stty rows 32 cols 105
HISTFILE=/sdcard/System/history.txt

PS1="\n Droid \[\e[34;1m\][\[\e[0;m\]\w\[\e[34;1m\]] \[\e[37;1m\]\[\e[0m\]$ "

Save the ash.rc file and copy to the /sdcard/System directory we've created earlier. This will be the rc file the ash shell will load and use as a profile.

Remember, by default ash loads the profile from /system/etc/profile. Since that file does not exist in this path we'll create a symlink in this path pointing to our newly created ash.rc that we've just copied to the /sdcard/System directory:

ln -s /sdcard/System/ash.rc /system/etc/profile

Almost set!

Now all we need to do is to call the ash shell at every logon. To do that edit the sh's shell rc file at /system/etc/mkshrc and add the ash command at the end:

nano -w /system/etc/mkshrc

Add this at the very bottom:

ash -l

Press CTRL+O to write the file and CTRL+X to exit nano.

Thats it! Logout from the android shell then log back in.

You should now see a better ash shell with colorful listings and a styled PS1, properly stuffed with your alises, commands and variables AND nano.

ash shell