OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

3.2.2. Command Line Basics: Browsing the Directory Tree and Managing Files‌


This section only provides a brief overview of the covered commands, all of which have many options not described here, so please refer to the abundant documentation available in their re- spective manual pages. In penetration tests, you will most often receive shell access to a system after a successful exploit, rather than a graphical user interface. Proficiency with the command line is essential for your success as a security professional.

Once a session is open, the pwd command (which stands for print working directory) displays your current location in the filesystem. The current directory is changed with the cd directory com- mand (cd is for change directory). When you don’t specify the target directory, you are taken to your home directory. When you use cd -, you go back to the former working directory (the one in use before the last cd call). The parent directory is always called .. (two dots), whereas the

current directory is also known as . (one dot). The ls command allows listing the contents of a directory. If you don’t provide parameters, ls operates on the current directory.


$ pwd

/home/buxy

$ cd Desktop

$ pwd

/home/buxy/Desktop

$ cd .

$ pwd

/home/buxy/Desktop

$ cd ..

$ pwd

/home/buxy

$ ls

Desktop Downloads Pictures Templates Documents Music Public Videos

$ pwd

/home/buxy

$ cd Desktop

$ pwd

/home/buxy/Desktop

$ cd .

$ pwd

/home/buxy/Desktop

$ cd ..

$ pwd

/home/buxy

$ ls

Desktop Downloads Pictures Templates Documents Music Public Videos


You can create a new directory with mkdir directory, and remove an existing (empty) directory with rmdir directory. The mv command allows moving and renaming files and directories; re- moving a file is achieved with rm file, and copying a file is done with cp source-file target- file.


$ mkdir test

$ ls

Desktop Downloads

Pictures

Templates

Videos

Documents Music

Public

test

$ mv test new

$ ls

Desktop Downloads

new

Public

Videos

Documents Music

Pictures

Templates

$ rmdir new

$ ls

Desktop Downloads

Pictures

Templates

Videos

Documents Music

Public


The shell executes each command by running the first program of the given name that it finds in a directory listed in the PATH environment variable. Most often, these programs are in /bin,

/sbin, /usr/bin, or /usr/sbin. For example, the ls command is found in /bin/ls; the which command reports the location of a given executable. Sometimes the command is directly handled by the shell, in which case, it is called a shell built-in command (cd and pwd are among those); the type command lets you query the type of each command.


$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

$ which ls

/bin/ls

$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

$ which ls

/bin/ls


$ type rm

rm is /bin/rm

$ type cd

cd is a shell builtin

$ type rm

rm is /bin/rm

$ type cd

cd is a shell builtin


Note the usage of the echo command, which simply displays a string on the terminal. In this case, it is used to print the contents of an environment variable since the shell automatically substitutes variables with their values before executing the command line.


Environment Variables Environment variables allow storage of global settings for the shell or various other programs. They are contextual but inheritable. For example, each process has its own set of environment variables (they are contextual). Shells, like login shells, can declare variables, which will be passed down to other programs they execute (they are inheritable).

These variables can be defined system-wide in /etc/profile or per-user in ~/

.profile but variables that are not specific to command line interpreters are better put in /etc/environment, since those variables will be injected into all user sessions thanks to a Pluggable Authentication Module (PAM) – even when no shell is executed.

Environment Variables Environment variables allow storage of global settings for the shell or various other programs. They are contextual but inheritable. For example, each process has its own set of environment variables (they are contextual). Shells, like login shells, can declare variables, which will be passed down to other programs they execute (they are inheritable).

These variables can be defined system-wide in /etc/profile or per-user in ~/

.profile but variables that are not specific to command line interpreters are better put in /etc/environment, since those variables will be injected into all user sessions thanks to a Pluggable Authentication Module (PAM) – even when no shell is executed.


Top OS Cloud Computing at OnWorks: