[HOW TO] use the Linux command line

Introduce yourself to us and other members here, or share your own product reviews, suggestions, and tips and tricks of using QNAP products.
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

[HOW TO] use the Linux command line

Post by OneCD »

This is for new QNAP NAS users who may want to learn how to use the installed BASH shell, but don't know where to start. It will focus on the specific shell found in the standard firmware.

This post is an introduction only. After you've read it, please continue with your self-education. There's lots more to learn! :)


Accessing the NAS command-line-interface
QNAP NAS use a well-known shell called BASH (Bourne Again SHell).

SSH (Secure SHell) is a network service that is used to access a shell remotely. It's used to remote-access Unix, Linux and Mac computers (as well as Android, servers, routers, modems, etc...). It's also how you'll access your NAS command line. SSH requires an SSH server (daemon) to be running on the target device. Which is then accessed from one or more client shells.

A basic SSH server is already installed and configured in your NAS, but you'll need to check that it's enabled. Login to your NAS QTS desktop and navigate to Control Panel -> Network Services -> Telnet / SSH.

Ensure that the 'Allow SSH connection' box is ticked. Its default port is 22. Click the 'Apply' button if you made any changes. Done!


Login
Any terminal connection will require some information about the remote system that you want to connect to. It will need the IP address (or hostname) of your NAS and the username and password of a user who is permitted to access the NAS.

I'll skip locating your NAS IP address as this has been discussed extensively in the forums.

The SSH username will be 'admin' and the password will be the same password you use to login to the QTS desktop as the admin user.

Now, you just need a terminal program to remote-access the NAS. And that will depend on your operating-system:
  • Windows
    You'll need to install a terminal client to create an SSH connection. A common one for Windows is called PuTTY. After it's installed, you're ready to configure a connection to the NAS. Add your NAS 'admin' username and 'admin' password and NAS IP address.

    For Windows 10 users with a build later than 1709, try the new Microsoft OpenSSH client.
  • macOS
    You already have a terminal program installed! Read here for how to find and launch it.

    Once you've launched 'Terminal', you are now at the command line. But it's on your local machine only. To connect to the NAS SSH service, type:

    Code: Select all

    ssh admin@your-nas-ip
    ... and enter your NAS 'admin' password when requested
Once you've done all this and created a connection to the NAS, you should see the shell command prompt:

Image
Hey, congratulations! You made it!

Now type 'exit' and get out of there. ;)

Image


The command prompt
Generally in BASH, you'll see two different characters as the command prompt: $ and #

$ is used to indicate that you're logged in as a regular user. This type of user is quite common on Linux desktop distributions. This account type can perform basic tasks, but nothing too dangerous. It's designed to be a nice, safe way to use Linux.

# is used to indicate that you're logged in as a superuser, or root user. This is more common on server or embedded distributions. This type of user has almost unrestricted access to the system and any commands that they run can cause a lot of damage. :twisted:

The idea is that you use Linux as a regular user, only occasionally elevating your privileges to that of the superuser, through the use of sudo, or an equivalent, and only when needed.

The default SSH login for QNAP NAS is the 'admin' user. This is the root user. Which is why the command prompt is shown as #.

This means your commands can be quite dangerous. If anyone asks you to run a command as the root user, you should always ensure you understand what that command does, and consider the possibility that it can be destructive to your system. Google it if you have to. Over time, you'll get to know the good ones from the bad.

If you see a command-line example that shows the prompt as # or $, you don't need to type that character and the space that follows it. But everything after that should be entered as shown.

So, some example code might be shown as:

Code: Select all

[/etc/config] # uptime
But you only type:

Code: Select all

uptime

Linux files & the directory tree
Linux (unlike Windows) requires you to be case-specific when using commands and specifying filenames.

Which means myfile.txt is a completely different file to MyFile.txt, myFile.txt, myfile.TXT, and MYFILE.txt - even though they are all spelt the same. All of these files can exist in the same directory. The same applies to Linux commands and programs.

I tend to stick to using all lowercase names, as it means less keys to type. My own files are named like 'myfile.txt'

The Linux directory tree root is indicated with a forward slash: /

This means every absolute path starts with a forward slash.

If you ever see a path that does not start with a forward slash, it is a relative path. As in, it's relative to some other directory. This would usually be the directory you're currently in. It means that the path points to a sub-directory of your current directory.

On some Linux distributions, the command prompt is prefixed by your current path within the Linux directory tree and is shown surrounded by square brackets.

So, if your prompt looked like this:

Image

... then you would be 'sitting' in the /etc/config directory. This is a good place to be if you need to process several files or sub-directories here.


Navigating the directory tree
In the QNAP NAS, the shares directory is found at: /share

All the standard and user-created shares can be seen here.

The 'Public' share is found at: /share/Public

If you're already in a sub-directory, and want to change into another sub-directory of that sub then you won't need to type the forward slash.

Examples:
  • If I was already sitting in the /share directory (as shown by the start of the prompt between the square brackets), and then wanted to change into the Public sub-directory, I would use the cd command (change directory):

    Code: Select all

    [/share] # cd Public
    ...and my prompt becomes:

    Code: Select all

    [/share/Public] #
  • If I wanted to go back up a level, I'd type:

    Code: Select all

    [/share/Public] # cd ..
    ...and my prompt becomes:

    Code: Select all

    [/share] #
  • If I did this again:

    Code: Select all

    [/share] # cd ..
    ...my prompt becomes:

    Code: Select all

    [/] #
  • Or I could do this faster by specifying the full (or absolute) path as / :

    Code: Select all

    [/share/Public] # cd /
    ...and my prompt becomes:

    Code: Select all

    [/] #
  • If I was sitting really far down the directory tree and wanted to navigate back up to /share, I would specify the full (or absolute) path:

    Code: Select all

    [/lev1/lev2/lev3/lev4/lev5/lev6] # cd /share
    ...and my prompt becomes:

    Code: Select all

    [/share] #
Common commands
  • cat
    This is the concatenation command. It is used to show the contents of text files. Multiple files can be specified.

    Here, it's used to show the contents of the mdstat file located in the /proc/ directory:

    Code: Select all

    cat /proc/mdstat
    You'll encounter this particular command again-and-again on the forum. It's one of the first things to check when you have a RAID issue, or when you want to see the current state of your array.
  • ls
    This is the 'list' command. It's used to list the contents of a directory. If you don't specify one, it lists the current directory.

    There are some useful parameters that can be passed to it:

    Code: Select all

    ls -la
    This shows all files in a long directory list format. This is great for checking file and directory permissions and last-modified dates and times.

    But 'ls -la' has an alias. Type:

    Code: Select all

    ll
    ... and you'll get the same thing. ;)
  • rm
    This is the 'remove' command. It's used to delete files (and optionally - directories). Please note: if you delete a file with this, there is no 'undo'.
  • mkdir
    This is the 'make directory' command. It's used to create new directories.
  • rmdir
    This is the 'remove directory' command. It's used to remove empty directories.

Some other commands
  • df
    Displays usage & free space for each of your mounts.
  • hostname
    Displays the name of your NAS.
  • uptime
    Displays how long the NAS has been running since bootup. It also displays the system load averages for the last 1 minute, 5 minutes & 15 minutes.
  • history
    This shows the command-line history since the NAS was booted. The most recent command is shown at the bottom. You can re-run any of these by typing an exclamation-mark together with the line number of the command you want. E.g. typing '!765' then pushing 'enter' will re-run the command currently listed at line 765.

    Tip: if you push the cursor-up (and cursor-down) key, you can quickly return to a previous command without showing this list.
  • which
    Displays the path to the specified program - if it can be found in the current $PATH.

    For example, if you type 'python', the default Python interpreter will launch:

    Code: Select all

    [/share] # python
    Python 2.7.12 (default, Oct 31 2016, 15:57:24) 
    [GCC 5.4.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
    But if you want to know where the Python interpreter is run from, you would type 'which python':

    Code: Select all

    [/share] # which python
    /opt/bin/python
    
Last edited by OneCD on Sat Dec 16, 2017 1:59 pm, edited 11 times in total.

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
User avatar
Spider99
Experience counts
Posts: 1951
Joined: Fri Oct 21, 2011 11:14 pm
Location: UK

Re: [HOWTO] use the Linux command line

Post by Spider99 »

OneCD

For windows users a better interface would be WinSCP which can be used in conjunction with putty to access the shell - but gives a nice windows graphical interface for browsing around and editing files directly as needed - it does a lot more than that but thats the basics :)
Tim

TS-853A(16GB): - 4.3.4.0483 - Static volume - Raid5 - 8 x 4TB HGST Deskstar NAS
Windows Server + StableBit Drivepool and Scanner ~115 TB Backup Server
TS-412 & TS-459 Pro II: Retired
Clients: 3 x Windows 10 Pro(64bit)
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [HOWTO] use the Linux command line

Post by OneCD »

Thanks Tim. I'll have to dust-off one of my Win VMs and try these out. ;)

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
User avatar
schumaku
Guru
Posts: 43578
Joined: Mon Jan 21, 2008 4:41 pm
Location: Kloten (Zurich), Switzerland -- Skype: schumaku
Contact:

Re: [HOWTO] use the Linux command line

Post by schumaku »

No sure what WinSCP has to do with the subject "[HOWTO] use the Linux command line" :ashamed:

Happy New Year to everyone!
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [HOWTO] use the Linux command line

Post by OneCD »

Thanks Kurt. And to you guys as well! :)

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
User avatar
Spider99
Experience counts
Posts: 1951
Joined: Fri Oct 21, 2011 11:14 pm
Location: UK

Re: [HOWTO] use the Linux command line

Post by Spider99 »

schumaku wrote:No sure what WinSCP has to do with the subject "[HOWTO] use the Linux command line" :ashamed:

Happy New Year to everyone!
Well because you can use the command line while visually seeing the filesystem - something windows users are familiar with

And a lot of the command line stuff is not necessary with WinSCP as it has the gui to help you - edit files/change permissions/find etc

doesnt mean you dont need to use the command line in some cases but gives you an easier option for new users before they jump in with putty directly

Happy New year
Tim

TS-853A(16GB): - 4.3.4.0483 - Static volume - Raid5 - 8 x 4TB HGST Deskstar NAS
Windows Server + StableBit Drivepool and Scanner ~115 TB Backup Server
TS-412 & TS-459 Pro II: Retired
Clients: 3 x Windows 10 Pro(64bit)
User avatar
2louis
New here
Posts: 6
Joined: Wed Jul 20, 2016 8:58 am

Re: [HOWTO] use the Linux command line

Post by 2louis »

Spider99 wrote:OneCD

For windows users a better interface would be WinSCP which can be used in conjunction with putty to access the shell - but gives a nice windows graphical interface for browsing around and editing files directly as needed - it does a lot more than that but thats the basics :)
Thanks heaps Spider99, I was just looking for such a Windows-like friendly way to access certain files and commands
User avatar
razormoon
Easy as a breeze
Posts: 465
Joined: Fri Feb 13, 2015 5:05 am
Location: Denver, CO

Re: [HOWTO] use the Linux command line

Post by razormoon »

2louis wrote:
Spider99 wrote:OneCD

For windows users a better interface would be WinSCP which can be used in conjunction with putty to access the shell - but gives a nice windows graphical interface for browsing around and editing files directly as needed - it does a lot more than that but thats the basics :)
Thanks heaps Spider99, I was just looking for such a Windows-like friendly way to access certain files and commands
You see, schumaku...there was a reason for his interjection after all!

hehe Happy New Years to everyone! I swear I'd be bald by now if it weren't for you guys.
Hopefully this year will be better than last! :|

Oh and thanks, OneCD...very much needed info!
:!: TVS-871-i7-16G 5.0.0.1932 Build 20220129, KODI, WSE 2019
:?: 1 x KINGSTON SNV325S2 as 2mb block cache, WDC WD40EFRX as RAID5, 1 x WDC WD40EFRX as iSCSI
:idea: APC PRO 1500 S
:-0 WIKI SUPPORT

"Nothing is impossible. Only expensive, illegal or both."
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [HOWTO] use the Linux command line

Post by OneCD »

razormoon wrote:Oh and thanks, OneCD...very much needed info!
Thank you. :wink:

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
User avatar
Spider99
Experience counts
Posts: 1951
Joined: Fri Oct 21, 2011 11:14 pm
Location: UK

Re: [HOWTO] use the Linux command line

Post by Spider99 »

razormoon wrote:
2louis wrote:
Spider99 wrote:OneCD

For windows users a better interface would be WinSCP which can be used in conjunction with putty to access the shell - but gives a nice windows graphical interface for browsing around and editing files directly as needed - it does a lot more than that but thats the basics :)
Thanks heaps Spider99, I was just looking for such a Windows-like friendly way to access certain files and commands
You see, schumaku...there was a reason for his interjection after all!

hehe Happy New Years to everyone! I swear I'd be bald by now if it weren't for you guys.
Hopefully this year will be better than last! :|

Oh and thanks, OneCD...very much needed info!
Ha Thanks for the support :)
Tim

TS-853A(16GB): - 4.3.4.0483 - Static volume - Raid5 - 8 x 4TB HGST Deskstar NAS
Windows Server + StableBit Drivepool and Scanner ~115 TB Backup Server
TS-412 & TS-459 Pro II: Retired
Clients: 3 x Windows 10 Pro(64bit)
babuja
Know my way around
Posts: 224
Joined: Fri May 13, 2016 1:02 am

Re: RE: Re: [HOWTO] use the Linux command line

Post by babuja »

Spider99 wrote:
razormoon wrote:
2louis wrote:
Spider99 wrote:OneCD

For windows users a better interface would be WinSCP which can be used in conjunction with putty to access the shell - but gives a nice windows graphical interface for browsing around and editing files directly as needed - it does a lot more than that but thats the basics :)
Thanks heaps Spider99, I was just looking for such a Windows-like friendly way to access certain files and commands
You see, schumaku...there was a reason for his interjection after all!

hehe Happy New Years to everyone! I swear I'd be bald by now if it weren't for you guys.
Hopefully this year will be better than last! :|

Oh and thanks, OneCD...very much needed info!
Ha Thanks for the support :)
Thanks oneCD

Sent from my Nexus 6P using Tapatalk
babuja
Know my way around
Posts: 224
Joined: Fri May 13, 2016 1:02 am

Re: [HOWTO] use the Linux command line

Post by babuja »

Command lines for dummies...

Ive have installed putty on windows and playing with juicssh on nexus...

I login as user but cant find any folder/files except from recycle. How can i see all folders and files?

[César@QNAP-NAS ~]$ ls
@Recycle@

Tried tree without sucess to:
[César@QNAP-NAS ~]$ tree -a
-sh: tree: command not found


Sent from my Nexus 6P using Tapatalk
Last edited by babuja on Mon Nov 13, 2017 5:20 am, edited 1 time in total.
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: [HOWTO] use the Linux command line

Post by OneCD »

Try changing directory to root:

Code: Select all

cd /
Your command prompt is interesting: what environment have you logged into there? Are you running your own SSH daemon? Virtual Machine?

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
babuja
Know my way around
Posts: 224
Joined: Fri May 13, 2016 1:02 am

Re: RE: Re: [HOWTO] use the Linux command line

Post by babuja »

OneCD wrote:Try changing directory to root:

Code: Select all

cd /

[César@QNAP-NAS ~]$ cd/
-sh: cd/: No such file or directory

Ive wright after the dolar cd/ and get -sh: cd/: No such file or directory

Not sure what im i dojng wrong


OneCD wrote: Your command prompt is interesting: what environment have you logged into there? Are you running your own SSH daemon? Virtual Machine?
First time accessing my qnap from ssh...just trying to learn and test some commands but not having much sucess.





Sent from my Nexus 6P using Tapatalk
Last edited by babuja on Mon Nov 13, 2017 5:19 am, edited 1 time in total.
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: RE: Re: [HOWTO] use the Linux command line

Post by OneCD »

babuja wrote:
OneCD wrote:Try changing directory to root:

Code: Select all

cd /
[César@QNAP-NAS ~]$ cd/
-sh: cd/: No such file or directory
No, you missed the whitespace between "cd" and "/" ;)
babuja wrote:
OneCD wrote: Your command prompt is interesting: what environment have you logged into there? Are you running your own SSH daemon? Virtual Machine?
Not sure...first time accessing my qnap
Ah, OK, you would know if you were.

It used to be that only 'admin' could login via SSH. But I think the newer firmwares allow different users to do this now.

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
Locked

Return to “Users' Corner”