User Tools

Site Tools


doc:howto:user.beginner.cli

Command-line interpreter

A command-line interpreter is a computer program that reads singular lines of text entered by a user and interprets them in the context of a given operating system or programming/scripting language. The interaction takes place by means of a command-line interface. Other common, but technically not quite correct, denominations are console or shell.

The OpenWrt standard unix shell is the Busybox-fork of the Debian implementation of the Almquist shell (see → http://www.in-ulm.de/~mascheck/various/ash/#busybox). In case you want to read about it.

Start

At the end of the boot up process, the init daemon is started, this can be init or systemd or upstart, etc. OpenWrt currently uses procd. Following the boot up scripts located in /etc/rd.d, init will then start all sorts of programs, amongst them the chosen shell. This listens to keyboard strokes and outputs a more or less colorful command-line interface to the connected display.

But most devices you run OpenWrt on, have neither a keyboard nor a display adapter. So we need to access it over the serial port (=local) or over the Ethernet port (= over the network).

Network

To gain access to a shell over the network, you obviously need some other programs to help you with that. And the whole data exchange (aka communication) has to involve some kind of network protocol.

Network protocols of choice are telnet and SSH. Both follow the server ↔ client scheme. On the device running OpenWrt we deploy telnetd for the telnet protocol and dropbear for for the SSH protocol. Try PuTTY for the real look-and-feel, but you should definitely also checkout WinSCP! The latter won't work quite correctly, however Konqueror with fish:// does! See FISH (Files transferred over shell protocol).

(OpenWrt does also include a SSH-client ssh and a telnet-client telnet, in case you want to login from it to somewhere else.)

Note: Before firstlogin only telnetd will run, and after only dropbear.

In case of a successful login dropbear will (generate a LOG and) spawn an instance of the specified shell (more shells can be installed simultaneously) with the users ID.

Configuration

In OpenWrt this is done in the file: /etc/profile by setting environment variables and aliases. It comes (of course) pre-configured and will work out-of-the-box, but you can alter and augment it's configuration:

Copy & Paste

When in PuTTY, you can mark text content with the mouse and, without pressing any key (like [Ctrl]+[c]), it is being automatically stored. You can then insert it the usual way (with [Ctrl]+[v]) in an other windows, e.g. an open firefox. The other way around, you copy text the usual way [Ctrl]+[c]) and then paste it in PuTTY by pressing the [right mouse button]!

Numpad in PuTTY while using Vi

In PuTTY goto "Terminal""Features" and check "Disable application keypad mode".

Issue commands

  • For some orientation with the file system and the whole directories, check flash.layout.

At login you will be in your $HOME directory, which is /root for user root and would be /home/user1 for user1, etc. Commands:

Command Memorize Description
pwd print working directory prints out the current directory you are in
cd change directory move through the file system directory tree: cd .., cd /, cd /etc/init.d, cd /tmp
ls list print the content of the current directory, ls -l /etc
cat concatenate printing the content of a file on screen: cat /etc/config/network, cat /tmp/dhcp.leases
cp copy creates a copy of the specified file, cp network network.bak
mv move creates a copy of the specified file and deletes the original, mv /tmp/opkg-lists/snapshots /mnt/sda1/opkg/packages
df disk free Shows you available space. Again, see flash.layout for understanding /rom, etc. And see df for help with the command and it's options. Try df -h.
free about free RAM
uptime time elapsed since last boot
dmesg print or control the kernel ring buffer
logread Shows the messages from syslogd (using circular buffer)
cat /proc/version
cat /proc/meminfo
more detailed data uppon RAM usage
cat /proc/cpuinfo
about your CPU
cat /proc/mtd
cat /proc/partitions
cat /proc/net/nf_conntrack
cat /proc/cmdline
cat /proc/modules

There is a ton of commands with a ton of options. On a full blown Linux distribution you would issue a man command to learn about the command and its options. However OpenWrt is minimalistic and thus does not contain this functionality. So either read the man-pages (manual pages) on another GNU/Linux machine or read them online: e.g. at http://man.cx/. Man pages are in the process of being translated.

Tip In firefox, you can use keywords to simplify the usage. Create a new bookmark, use http://man.cx/?page=%s as address and man as keyword.

editing files

To edit a file you need an editor, to edit a text file, you would use a text editor.

The standard text editor included is vi. Until you get used to it, vi is neither intuitive nor pretty.

  • vi has two modes: command mode and insert mode.
  • to enter command mode press [Esc] (escape key)
  • to enter insert mode press either [i] for insert or [a] for append
  • vi starts out in command mode

starting vi

Start with vi or vi /etc/config/network or vi firewall.user if you are already in the same directory.

editing

In order to edit the file, you have to be in insert mode. Press [i] or [a].

exiting vi

In order to get out of vi, you have to be in command mode. Press [Esc] (the escape key). Then issue one of the following commands:

  • :w to write the current file to disc, this will overwrite the old file
  • :q to quit without writing
  • :wq! to (forcefully) write to disk and then quit vi
  • :%s/string1/string2/g replace string1 with string2 in the whole file

configuring vi

Vi can be configured in command mode by setting certain variables:

  • :set ai use auto indentation (sometimes annoying default)
  • :set noai NO auto indentation

alternative text editors

If you do not like vi, try joe, mg, nano, vim, vim-full, vim-help, vim-runtime, zile

  • and there may be other text editors available in the OpenWrt repos ;-)
  • when logged in via WinSCP/Konqueror you can also use some Editor on your PC

Scripting language

OpenWrt uses busybox's ash shell by default, which is in most parts POSIX conform. Visit shell script for general Information about shell scripts.

Executing shell scripts

Shell scripts can be executed with:

sh /path/to/script.sh

After changing the executable bit its also possible to run it without the sh in front:

chmod +x /path/to/script.sh /path/to/script.sh

File Managers

You may also want to try mc or deco.

Use GUIs

Konqueror GVim WinSCP

Further Help

doc/howto/user.beginner.cli.txt · Last modified: 2013/11/15 10:26 by lorema