Friday, June 24, 2011

A Quick Overview of Linux Commands [Linux]

Before my posts really start, I thought a quick overview of commonly used Linux commands would be useful since this blog is for complete newbies and those starting off with almost no experience.

Below is a list of commonly used Linux commands (I will update this frequently, so it might be small at first):

  • NOTE: know the difference between "absolute" and "relative" path names? Absolute path names are used when you type a "/" (slash) before the pathway (E.G. typing ls /pentest/exploits/ will print out that from your main Linux directory), but using a relative pathname references your current directory's subdirectories (E.G. typing ls pentest/exploits/ would print out that directory if it exists in your current directory).
    I'll try to explain this more in depth with better examples, but a quick rundown can't necessarily hurt. You can think of absolute path names as never changing and relative referencing things that are relative to your current position! (quite simple once you get it)
  • man --- the manual command. Type "man [any command]" and it will bring up a manual to that command if there is one (which there usually is). This is probably the best command for your learning experience. Abuse it and use it to help learn each command and their "-" options. 
  • cd --- stands for "change directory. Navigates you to the specified directory OR moves you to your "home" directory if no directory is specified. E.G. "cd" alone would move you to /home/username/ or "cd etc" would move you to the "etc" directory that is located in your current directory, but "cd /etc" links to an "absolute" path name which is a standard Linux directory.
  • pwd --- stands for "print working directory" (thanks Anon in comments!) prints out your current directory, it's as simple as that; "makes it easier to remember for someone new."
  • ls --- lists out files and sub-directories in your current directory.
    • Using the sub-commands "-al" lists all (the "a") and in long format (the "l"); makes it easy to see hidden files and directories and also an easier to read format. Requires the dash "-" before the "al" and a space before it in between the "ls" and "-". E.G. "ls -al" in your home directory would print all files and folders (including hidden), then you can move to them or view/edit.
  • cat --- lists the file out on your screen in the terminal. Only works with files that are readable; if you try it with a directory, it will output an error.
    • I'll copy the helpful Anon from my comments, since he explained this quite well: "cat requires one or more filenames as parameters. The original purpose of cat was to concatenate files E.G. "cat file1.txt file2.txt > file3.txt" will create file3.txt that contains the contents of file1.txt followed by the contents of file2.txt."
    • This is very helpful information for new users trying to understand how to concatenate and manage files. The ">" operator takes the first two files and "pushes" them into the "file3.txt." Usually when operators such as ">" and "<" are in play, the direction they point is important; below I'll review the ">" and ">>" operators in more depth.
  • emacs --- a Linux text editor in your terminal; I will provide more help with this in a later post. You can edit a file with the command "emacs [file location or name if in same directory]" and play around with it.
  • vi --- another Linux text editor.
  • nano --- my favorite Linux text editor so far; easy to use and pretty self explanatory. I don't believe it is built in to classic Ubuntu though.
  • grep --- stands for general regular expression print; it searches through a file (or an output as I will show and explain) for a certain string or other options.
    You use this tool, for example, to search through a huge directory looking for a specific file, to see if it's located in there. Type "ls -al | grep [folder name]" to see how it works. The "|" is called "piping" and I will cover this next.
  • | --- this little tool is used when you want to "pipe" commands. Piping is, simply put, is running more than one command at once into one command. For example, eventually I'll show a quick BASH scripting guide and we might do something like "ping -c 1 192.168.0.1 | grep "bytes from" &" which I will explain in depth later on. All you need to know is if you want a general command (such as an nmap or ping command, but want to grep or ls or cut out certain things, just pipe in that command afterwards each with their own "|" after.
  • ping --- stands for Packet InterNet Groper... yes, groper. It basically "gropes" the specified internet source (whether it be by name on a local network, or a website name, or by IP address). It uses an "echo" system with acknowledgements that packets were sent and received to determine if hosts are "up" (able to be connected to).
    • Some options (like the one used above explaining piping) can be added on. For example, the ping command keeps pinging until you stop it (ctrl-z is the EOF or end of file command, and stops most running programs in Linux), so to only ping 3 times, you would add the "-c 3" command beforehand (in our example I used 1 just to ping it once).
    • Typing "ping --help" into the terminal brings up all the options you can use; try playing around with a bunch of them by pinging google.com or "localhost" which is yourself. Another way to ping yourself is use the "loopback" IP which is 127.0.0.1, or you can ping any other IP.
  • cut --- this command cuts out selected items from a file or output (it can be used with piping and a ping command which we will do in later posts). Typing cut --help brings up the help menu for this command, or try "man cut" to read more about it. It's a very useful tool to use when scripting in BASH.
    • I'll update this section with an explanation and more helpful information for newbies soon!
  • echo --- echo literally echos back what you type. For instance, if you type into a terminal "echo hello" it will print "hello" below. This is used in scripting a lot and you should understand how simple it really is.
    • Getting a bit more technical, you can echo certain "variables" that Linux has, such as the hostname, IP, and other things we will get into later. Try the command "echo $HOSTNAME" and see what comes back. It should be your username you have logged into. Cool, right? This is known as an environmental variable and is useful while creating scripts and user friendly interfaces later. I'll cover environmental variables and more helpful information on this in another post with BASH scripting. If you don't understand the whole "$HOSTNAME," it doesn't matter yet!
    • To get the help page for this, the command "echo --help" doesn't work. It will echo back "--help" which is annoying. Use the "man" command by typing "man echo" and read up on this useful Linux command.
  • arp --- check out my ARP post.
  • touch --- creates a file with the name you wish in your current directory. For example, typing "touch file.txt" would create the file with the name "file" and the extension ".txt"
    Now for some Linux maintenance, updating, and application downloading/installing:
    • apt-get --- [my] classic command for getting applications on Ubuntu. Each version of Linux has it's own patience package which at the moment I am not familiar with, but I will attempt to update as I learn them; below is some commands that are useful; append them after a space to this command. E.G. apt-get [commands].
      • install [application name] --- self explanatory; it installs (and prompts if necessary) the application you have entered.
      • upgrade --- Upgrades all your packages (or programs) that have available upgrades; definitely useful to run once in a while.
      • update --- Retrieves the list of packages that are available for your system to upgrade and install; also useful to run every once in a while.
    • apt-cache search [string (or keywords)] --- searches the application database with your string or keywords for applications. Very very useful if you want to find certain programs to install.
      Again, the "apt---" are for Ubuntu; each flavor of Linux has it's own maintenance packages, and each has multiple.
    Try using these commands by typing "apt-get install ssh" and looking at the output; ssh stands for "secure shell" and is a way for us to access other computers. There is also "ftp" which stands for "file transfer protocol" and is a way for us to transfer files from computer to computer through the terminal (and also user-friendly GUIs).

    Of course these are all simple, and I will be adding more and more to them as I remember/discover new ones, so don't be alarmed when there's only basics up at the moment.  As always, ask questions below and I'll get back to you!

    Last updated: September 24th, 2011.