Enable shell history

I am using LuCI openwrt-19.07 on my Archer C7 and I see that there's no bash history functionality. Is there a way to enable it and possibly keep the .bash_history file in my mounted external storage?

Do you really use bash? LuCI is a web frontend.

The standard is

# echo $SHELL
/bin/ash

Almquist shell

From Wikipedia, the free encyclopedia

Almquist shell (also known as A Shell , ash and sh ) is a lightweight Unix shell originally written by Kenneth Almquist in the late 1980s. Initially a clone of the System V.4 variant of the Bourne shell, it replaced the original Bourne shell in the BSD versions of Unix released in the early 1990s.

and

Embedded Linux

Ash (mainly the dash fork) is also fairly popular in embedded Linux systems. dash version 0.3.8-5 was incorporated into BusyBox, the catch-all executable often employed in this area, and is used in distributions like DSLinux, Alpine Linux, Tiny Core Linux and Linux-based router firmware such as OpenWrt, Tomato and DD-WRT.

4 Likes

Ok yes is there any solution for ash?

https://openwrt.org/packages/pkgdata/bash

1 Like

ash is provided by busybox, which provides all basic shell & Linux default app features in OpenWrt.

You can enable the history feature in busybox and recompile a personal version of it for yourself, so that you can have ash that remembers history. (I do that for my own build.)

1 Like

Doesn't that wear out the flash fast? On my laptop .bash_history is around 20k, which means each time it's updated a new block of flash has to be erased, I think?

I only store it on ramdisk, so I lose the history if the router reboots.

But if it is stored to flash, you could also config it to save only when the ash shell exits, not after each command.

https://github.com/openwrt/openwrt/blob/0e8ddc953f844861b60debec4d7c65f9b4de7434/package/utils/busybox/config/libbb/Config.in#L180

config BUSYBOX_CONFIG_FEATURE_EDITING_SAVEHISTORY
	bool "History saving"
	default BUSYBOX_DEFAULT_FEATURE_EDITING_SAVEHISTORY
	depends on BUSYBOX_CONFIG_FEATURE_EDITING
	help
	Enable history saving in shells.

config BUSYBOX_CONFIG_FEATURE_EDITING_SAVE_ON_EXIT
	bool "Save history on shell exit, not after every command"
	default BUSYBOX_DEFAULT_FEATURE_EDITING_SAVE_ON_EXIT
	depends on BUSYBOX_CONFIG_FEATURE_EDITING_SAVEHISTORY
	help
	Save history on shell exit, not after every command.
2 Likes

Is there a cheaper-easier-faster solution to enable command history than recompiling?

Can I substitute ash with bash and do that?

Also as I said in my original question, I'd like to keep history file to my external storage to preserve flash memory

I patch it to be in /tmp, but you can change the location as you want.
(Alternatively, busybox looks up HISTFILE variable that you can set.)

You can substitute ash with bash as your shell.
But as bash is then your default shell but is not included in the firmware image, you will be locked out of the router next time you use sysupgrade (as the new image will not have bash embedded.)

4 Likes

a few alternatives: use sudo, cmds are logged to main logfile or try script

1 Like

You can avoid this problem without the effort of recompiling by using the imagebuilder to create a custom sysupgrade image that includes the bash package.

create 2 users; login one with the ash shell and su - w bash; this eliminates the chance being locked out after upgrade, just a hint

2 Likes

i install bash and keep .bashrc and .bash_history between updates via /etc/sysupgrade.conf, ie

~# grep root /etc/sysupgrade.conf
/root/.screenrc
/root/.bash_history
/root/.bashrc
/root/.ssh/config

# cat .bashrc
# enable basic tab completion in openwrt
bind "set show-all-if-ambiguous on"; bind "set completion-ignore-case on"

# runtime limit of commands in history. default is 500!
export HISTSIZE="5000"
# ignoredups to make the searching more efficient
export HISTCONTROL="ignorespace:ignoredups"
# append, not owerwrite history file (should be set by default, just to make sure)
shopt -s histappend

alias ll='ls -lah --color=tty'

From here, every time you login, run bash and you're good

3 Likes

I know this is an old thread but I want to ask something.
Is there a change to those busybox history saving options for version 23.05.
If I choose to save history (which is convenient between reboots) does it save the history to flash memory.
image
I couldn't find the file (location) where the history is kept.

default is to loose history in ash once the the SSH session ends.

My assumption would be, this will preserve the history on the RAM disk across multiple subsequent SSH sessions as long as the router is up, but loose history on reboot.

1 Like

Depends on how you configure the directory/file for storing the history:

I store it on RAMdisk via this profile extension:

root@router5:~# cat /etc/profile.d/busybox-history-file.sh
export HISTFILE=/tmp/.busybox_ash_history

Causing the HISTFILE shell variable to be set.

root@router5:~# env | grep ash
SHELL=/bin/ash
HISTFILE=/tmp/.busybox_ash_history
1 Like

Why is this not a standard feature built into the image?

It seems like a good feature to have by default

OpenWrt is traditionally really flash size-constraint focused, and the design goal has been routers with just a few megabytes of flash memory. So, most of the utilities are somewhat stripped down on features by default.

But you easily can build your own version of OpenWrt with the additional features enabled.

1 Like

crazy question, but with recent routers (5 years old) they seem to have more resources. Will Dev raise the min requirements for openwrt, such as 256mb Memory, 128mb NAND, Dual Core CPU?
Openwrt is great but I think the ideology of keeping openwrt on such low end devices is really limiting newer features.

1 Like

Such it has always been. Kernels never get smaller.

See this wiki article about 4/32 devices and what gets chopped next.

3 Likes