Telnet/SSH and the command line

From DD-WRT Wiki

Revision as of 12:57, 2 September 2006 by AlReece45 (Talk | contribs)
Jump to: navigation, search

Contents

Telnet

Telnet is an easy way to access the Command Line (Shell) of your WRT54G and access configuration not available in the web interface. Because Telnet passwords and commands are not encrypted, it is vulnerable to sniffing attacks. Secure Shell is a secure replacement for Telnet that uses strong encryption, and is recommended instead of Telnet over any public network. (See SSH section below.) Telnet operates on port 23 and uses an ASCII protocol.

Enabling

  1. Using the Web Interface, goto the Administration tab and the Services subtab
  2. Enable Telnet
  3. Save Settings

Using

  1. Open your favorite Telnet client
  2. connect to <Router_LAN_IP> e.g. 192.168.1.1
  3. When asked for the username, enter root (even if you changed username in web interface)
  4. When asked for the password, enter your routers password

On Windows, you can use Start > Run > telnet <Router_LAN_IP>

SSH

Overview

SSH, or Secure Shell, is an encrypted protocol and associated program intended to replace telnet. It can also be used for creating secure tunnels, somewhat akin to Virtual Private Networks. Unless changed, everything SSH operates on port 22.

SSH operates just as telnet with a user/password combination or on a Public/Private key infastructure. For the latter to work, a small public key is given to the server and the server gives your client its public key. Your client encrypts information to the server using the servers public key and the server encrypts information sent to you using your public key. Private keys are never exchanged, and are used to decrypt the information encrypted with the associated public key.

The DD-WRT firmware can use user/pass logon or only allows connections from clients whose public keys are manually entered via the web interface. Multiple keys can be entered by placing them on seperate lines.
If you want to use user/password to login using SSH use user "root" with the password you set in the webinterface

Actually you can manually set (via telnet or ssh) the sshd_authorized_keys nvram variable. ie nvram set sshd_authorized_keys=key1 key2 key3 etc

You can also manually edit /tmp/root/.ssh/authorized_keys and add keys (although these will dissapear on a reboot unless you have a startup script altering them).

It is worth pointing out ssh keys are quite long strings of characters so if you paste them in you have to be careful that you don't get any line breaks (ie it is one Long continuous line). or they will not work.

Setting Up

Public key method

First you should generate a Public/Private key pair on your desktop machine. This can be done through the "Puttygen" utility if you're using either Putty or WinSCP as clients. Copy the public key to the clipboard and save the private key somewhere on your computer. There is no need to save the public key. If you forget it, you can instruct Puttygen to open your private key file rather than generating a new key pair and it will tell you your public key. It is recommened that you don't secure your key pair with a password, as this will make things easier for you, although somewhat less secure.

  1. Using the [Web Interface] goto the Administration tab.
  2. Enable SSSHD If new options don't appear, Save Settings
  3. Paste your public key in the authorized key of the SSHD section that has now expanded. You will need to generate this on your desktop if you don't have one yet.
  4. Save Settings

Password Logon method

If you don't want the hassle of generating ssh keys, you may use the password logon method.

  1. Using the [Web Interface] goto the Administration tab.
  2. Enable SSSHD If new options don't appear, Save Settings
  3. Enable Password Login to enable the password login

After this you may login as user "root" with the password you set for the webinterface

SSH Shell Client

Provides a secure alternative to standard telnet.
A good Windows Client to use is Putty
Configure the client to use the Private Key you saved earlier.
Most Linux distros have telnet and SSH clients by default.

SCP

Secure Copy (SCP) allows one to copy files to and from the router and a remote host--usually a desktop machine.
A good Windows client to use is WinSCP
Configure the client to use the Private Key you saved earlier, or use "root" and the webinterface password
Remember: only the /tmp and /jffs partitions are writable!

Drop Bear

DropBear is an SSH client/server installed by default on the WRT54G. DropBear allows one to connect from the WRT54G to a remote SSH server for scp, etc. I don't believe SSHD needs to be enabled through the Web Interface in order to use the client portion of DropBear.

If you have an SSH server on your desktop machine (such as OpenSSH) you pull files from your desktop machine using the scp command. This can be used to copy files from your desktop machine in a Startup Script

The DD-WRT Command Line

aka the DD-WRT Linux shell

This is an 'ash' shell. Ash is a version of sh, literally 'A SHell' (A command Interpreter)

Basic Syntax

The Linux Command Shell (Ash) is not the same as the Windows/DOS command prompt.

/ (and not \) is used to seperate directories in a path, just like the interweb.

In order to execute a command, the path for that command must be provided. This may either be a full path or a relative path.

Relative Path Operators

There are two relative path operators.

.        The current path
..       One directory above the current path

Examples

1) If you are in the /jffs/usr/bin directory and wish to run the /jffs/usr/bin/noip command use:

/jffs/usr/bin # /jffs/usr/bin/noip

or

/jffs/usr/bin # ./noip


2) If you are in the /jffs/usr/bin directory and wish to run the /jffs/usr/kismet command use:

/jffs/usr/bin # /jffs/usr/kismet

or

/jffs/usr/bin # ../kismet

or

/jffs/usr/bin # cd ..
/jffs/usr # ./kismet


3) Relative paths can also be used as arguments. If you installed the noip package, you'd notice that the command is installed as /jffs/usr/bin/noip but its configuration file is installed as /jffs/etc/no-ip.conf When running noip, it is thus required to give it the path to its configuration file with the -c command. This can be done like:

/jffs/usr/bin # ./noip -c /jffs/etc/no-ip.conf

or

/jffs/usr/bin # ./noip -c ../../etc/noip.conf

notice that the first ../ brings us to /jffs/usr/. The second ../ brings us to /jffs/, and then the rest of the path can be appended.


4) While the other examples all showed how to save typing, you can also really screw around with relative paths. To launch the noip command in example 1, you could also use

/jffs/usr/bin # ../../../jffs/./usr/./bin/././../bin/././noip

Here we browse all the way back to the root / directory, then climb back up to /jffs/usr/bin, drop back down to /jffs/usr and then climb back up to /jffs/usr/bin.
Current path references of /./ are thrown in spuratically just to mix things up. See how /./ always references the then current path, not the origional path of the shell when the command was entered.

Pipes and Redirects

The output of commands can be piped through other commands or redirected to devices and files.

< and > are the redirect operators.   < Takes input from a device or file and routes it as input to the command given.   > Takes output from a command and redirects it as input for a device or file. Ex: If you don't want to see the output of a command, redirect it to the null device:

command > /dev/null

| is the pipe character, and pipes the output through another command (for formatting, etc) Ex: the most command use of the pipe is to limit the output of a command:

command | more

This is extremely useful for commands like nvram show which list some 800-1200 lines. nvram show | more will list the results 1 page at a time.

Background processes

It is possible to run programs in the background (returning you to the command prompt immediately) by terminating your command with the & character. ex:

command &

Make sure you add a space between your command and the ampersand or you will result with a File not found error.

Basic Commands

<command> -h                 The -h flag almost always provides help on a command. Use it!
ls                           List the contents of the current directory
cd <directory or full path>  Change to that directory or path
cp <source> <destination>    Copy the source file to the destination
mv <source> <destination>    Move the source file to the destination
mkdir <directory name>       Create a new directory
wget <URI>                   Download the file at the given URI to the current path
tar -xz -f <file>            un-gzip and un-tar the given *.tgz or *.tar.gz file
rm <file>                    Delete the file
rm -r <directory>            Delete the directory and all contents
killall <program name>       Kill all running processes of the program
ps                           Show running processes
top                          Show running processes in a graphical frontend

More Advanced Commands

These commands warrant their own wikis:

Scripting

For a good scripting howto, visit Linux Shell Scripting Tutorial - A Beginner's handbook

To have your scripts load on startup, see: Startup Scripts.

Useful Scripting Examples

Main Page: Script Examples

If you have an ipv6 tunnel/subnet use the IPv6 startup script to set it up

External Resources

Wikipedia's SSH article
Linux Shell Scripting Tutorial - A Beginner's handbook