NETGEAR is aware of a growing number of phone and online scams. To learn how to stay safe click here.

Forum Discussion

alouch47's avatar
Sep 06, 2010

SSH shell: He says "Let there be colors" Shell Beginner Guid

Modified : 2010-12-31
Hi everyone.

Not quite sure it has already been posted, so I thought I just share some tips.
It's just "basic" Linux knowledge, so it won't be of great interest for those who are already familiar with this, but others may find this useful. I won't just throw what to modify, but I'm going to explain some of the very basic Linux commands too.

As you may already know, you NAS is accessible via SSH, but the basic shell access is rather "austere" & first time users might be unwilling to use shell access due to that fact.
So this "howto" is just about putting some life in it.
Pre-requisites :

  • SSH addon

  • APT addon (may be already installed on your NAS)

  • A SSH access software. For Win users, use the great PuTTY

  • basic linux knowledge will help


:idea: Tip for the first time users.

  • To copy/paste text in the linux terminal/shell : Just select the text you want to copy, as in windows, and THAT'S ALL (selected text is automatically copied into memory). To paste, just right-click, and the text will be pasted at the cursor position (not the mouse cursor position, the terminal cursor position, so be careful).

  • To cancel an operation, input CTRL+C

  • To logout of a terminal, input CTRL+D (when you're at shell prompt, ie not editing a file or running an operation)

  • With Linux, directory structures are written with forward slashes "/" as opposed to windows where it is backslashes "\". So directories are accessed with cd /directory1/directory2 ...


Let's start with the beginning.

When you access you readynas via SSH, after login in successfully, you end up to the linux shell.
It's called "BASH" (acronym for "Bourne-Again Shell") (For those interested in reading, here is the Wikipedia link).
I'll assume you'll use PuTTY from now on. (Sorry Mac users, should be pretty the same for your ssh soft too)

Once logged in, you get something like this :

login as: root
Authenticating with public key "root@Hydra"
Passphrase for key "root@Hydra":
Last login: Fri Sep 3 09:45:07 2010 from 172.21.0.12
Last login: Fri Sep 3 10:12:10 2010 from 172.21.0.12 on pts/1
Linux Hydra 2.6.33.6.RNx86_64.2.1 #1 SMP Tue Jul 27 13:21:19 PDT 2010 x86_64 GNU/Linux
Hydra:~#

Here "Hydra" is the name of my ReadyNas Pro, so you should see a
YouNASName:~#

That's the prompt.
the "~" mean you're in your "home directory", so if you've logged in as "root" you should be in the "/root" folder.
You can check in what folder you're in by issuing the "pwd" command
:!: "/root" is the home folder for the user "root", not to be confused with "the root folder" which is the base directory ("/")

Ok, now go to the root folder "/", and input a "ls" ("list") command to see what's in it.

Hydra:~# cd /
Hydra:/# ls
addons-config dev home lost+found opt sbin usr
backup Documents initrd media proc sys var
bin etc lib mnt ramfs tmp webroot
c frontview lib64 Mx500 root USB
Hydra:/#

:?: Yeah, whatever, just plain grey text over a black background... Not really appealling :roll:
Don't worry, as we're gonna remedy to this !

Part 1 : Improve the shell
First of all, we need to edit "bash" profile file, so we can pass options to it.
The file's located under the /root folder (if you're logged in as "root") and it's called ".bashrc"
Hydra:/# cd /root/
Hydra:~# ls
Hydra:~#

:?: WTF ?? It's all empty ??
Ok so you won't find it if you input just a "ls" command, as files beginning with a dot "." are considered hidden by the system.
You have to input a "ls -a" ("a" as in "all") to see everything
Hydra:~# ls -a
. .bash_history .ncftp .ssh .viminfo .VirtualBox
.. .bashrc .profile .vim .vimrc
Hydra:~#


Let's edit the file, and put some lines in it.
We'll use the "vi" text editor, a basic editor available in almost (if not all) linux distro.
For the moment, "vi" we'll be basic too, but we'll enhance it in the next part of this howto
Hydra:~# vi .bashrc

The file will be pretty empty when you open it first time
So lets fill the following lines in, so you final file is as mine :
To input lines in "vi" you have to tap the "insert" key on your keyboard.
It'll then read as "I" in the bottom-left corner of the screen. If you tap "insert" a 2nd time, it'll read "R" instead which mean "Replace" so be careful. For numbers input DO NOT USE THE NUMPAD as the result may be unexpected. Use the keys above the keyboard instead
To exit "edit" mode, just press the "Esc" key on your keyboard.
Here is the file as it should look when you're done editing

# ~/.bashrc: executed by bash(1) for non-login shells.

# You may uncomment the following lines if you want `ls' to be colorized:
export LS_OPTIONS='--color=auto'
eval "`dircolors`"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

:!: Be careful with the line "eval "`dircolors`"" because "dircolors" is enclosed in single backquote "`" (= Grave accent) and not single quote " ' ". If you're unsure how to type it, just copy/paste the text above in you file

To do operations in "vi", you have to escape input mode (by tapping the "Esc" key on you keyboard) and then tap ":"
You'll see that ":" will appear on the bottom-left corner of the screen.
Then input "wq" (without the quotation mark) for "write & quit"
"Vi" will save the file and return you to the shell.
:!: If you've made a mistake and don't know how to go back, input ":q!" (without the quotation mark) to "force exit" without saving, and just try again

Some explanations. In the above file, we've passed options to bash, to tell it to render a color coded display.
Moreover, we have created "Alias". Alias are useful to shorten commands for example, as it's the case here.
For example, we have aliased the "ls -l" command (ls = show dir content, -l in ordered vertical list with file/directory details) to "ll"
So if you type "ll" at the shell prompt, it'll be as if you've typed "ls -l". You might not see the point in doing this right now, but when you'll be a little more used to the Linux environment, you'll see that's it's very useful to have full detailed view of a file/directory.

:?: When I type "ll", it says "command not found"
Yes, the modifications are not applied immediately, you'll see them when you'll logout/login to PuTTY.
But for now, stay logged in, as we have more to do.
You can try the "ls -l" command to see how the result looks like.

Part 2 : Improve "Vi"

This part is the simpler one now that you've learned a little bit more. We're going to replace our basic "Vi" with it's enhanced version called "Vim"
With APT ready to use, it's very simple to install new "packages" (equivalent of Win/Mac executables)
We're going to install the enhanced version of the "Vi" editor.
To do this, just type the following command at the prompt
Hydra:~# apt-get install vim vim-doc vim-scripts

This will install all the necessary packages plus some others (the doc, which can be very useful)

Once everything is installed, we have to do one more small modification.
Go to you home folder by issuing a "cd" without any char after
type a "ls -la" and see if you have a file called ".vimrc"
If it's not the case, simply make one, by issuing "touch .vimrc"
:!: At the left of each line what you see are permissions of each file/folder. Folders are indicated with a "d" as the first letter
Hydra:/# cd
Hydra:~# ls -la
total 52
drwxr-xr-x 6 root root 4096 2010-09-06 10:54 .
drwxr-xr-x 23 root root 4096 2010-09-02 12:27 ..
-rw------- 1 root root 10290 2010-09-03 16:05 .bash_history
-rw-r--r-- 1 root root 396 2010-09-02 17:26 .bashrc
drwxr-xr-x 2 root root 4096 2010-08-18 21:53 .ncftp
-rw-r--r-- 1 root root 110 2004-11-10 17:10 .profile
drwx------ 2 root root 4096 2010-08-18 21:48 .ssh
-rw------- 1 root root 3885 2010-09-06 10:54 .viminfo
-rw-r--r-- 1 root root 10 2010-09-02 17:29 .vimrc
drwxr-xr-x 2 root root 4096 2010-09-02 20:46 .VirtualBox

Hydra:~# touch .vimrc


This file will contain only one parameter for the moment, so it is necessary to open it and edit it, we can fill it with the following command:
Hydra:~# echo "syntax on" >> .vimrc

The "echo" command is used to sort of "write" the text inside the quotes somewhere. By default, it write it to the display.
By using the ">>" we're telling echo to write the text at a precise location. This location is in fact the ".vimrc" file.
So our command write the text in quotes into the file.
This command will tell "Vim" to use syntax highlighting which is very useful when reading trough a file.

One more thing and we're done.
We have to tell the system to now use the "vim" executable instead of the "vi" one.
Go to the "/bin" folder which contains some of the executable files of the system.
Type a "ls -l vi"
Hydra:~# cd /bin
Hydra:/bin# ls -l vi
lrwxrwxrwx 1 root root 12 2010-09-02 17:51 vi -> /bin/busybox
Hydra:/bin#

What you see here is called "Symbolic Link" (designated by the -> and the "l" at the beginning of the line )
It means that when you type "vi" at the command prompt, in fact the system execute a program called "busybox"
We have to modify this to call "vim" instead.
First we have to delete the existing link.
:!: Be VERY CAREFUL when deleting system files when you're logged in as root, as you might end up killing your system and restoring your NAS
Type "rm -i vi" and answer with a "y" to the question if you are sure what you're about to delete.
Hydra:/bin# rm -i vi
rm: remove symbolic link `vi'? y
Hydra:/bin#


Now we'll tell the system that "vi" should call "vim". For this we're using the "ln -s" command. The "ln" command is to create links and the "-s" parameter is for "symbolic" (as opposed to "hard links"). A symbolic link (also known as a soft link or symlink) consists of a special type of file that serves as a reference to another file or directory.
Hydra:/bin# ln -s /usr/bin/vim vi
Hydra:/bin# ls -l vi
lrwxrwxrwx 1 root root 12 2010-09-06 11:18 vi -> /usr/bin/vim
Hydra:/bin#

With this, you can continue to type simply "vi" to edit a file, and the system will automatically called "Vim"

"Et voila!" you're done! Now you have to logout and back in to SSH for all the modifications we have done to be applied to our profile.
Alternatively, you can also source (aka, re-read the file and apply modification) the .bashrc file by typing the following code into your terminal :
cd ~ ; . .bashrc

The "~" means you home-dir. So basically you just cd to your homedir, and source (reload) the .bashrc file. Be carefull, there is a space between the dots. The first is equivalent to the command "source", the second one is part of the filename you want to source.

To see how things change, you can for example go to the "/bin" folder and issue a "ll" command.
You can also try to edit your .bashrc file and see how it looks like now.


Hope you guys will find this little contribution helpful. Don't hesitate to ask questions & make criticism.

21 Replies

Replies have been turned off for this discussion