Common and useful commands

The best way to learn commands is to use them. Staring at lists of commands and trying to memorize them is both boring and ineffective. Commands are usually memorized through repetition of use. For rarely used commands, a browser's bookmark feature is a great solution, although it should be noted that obscure blogs can go away, get rewritten, links changed, and the same can even happen for large forums, such as happened with ubuntuforums.org, rendering bookmarks to greatly useful posts worthless. Another option is to save the webpages to a directory, keep notes somewhere convenient, or contribute to this wiki!

Note that nearly everything in Linux is case-sensitive, and most commands are entered as lower-case, including all of the commands below.

/bin/bash
Though not exactly a command, entering  will open a   session. Note that there are several different ways to enter a  session, each with subtle yet distinct differences.

cd
Used to change directory of the command prompt and is discussed in the Intro to command line article.

chown
The  command is used to change the owner or group a file or directory belongs to. For example,  will assign   as the owner of the directory or file specified. Using  will assign   as the owner and   as the group of the directory or file specified. Using the recursive option by entering  when changing user or group ownership on a directory will apply the changes to all files and subdirectories in the targeted directory.

Note that, as with all Linux commands, using  will depend on file and directory permissions, and typically will require root privileges to execute.

chmod
The  command is used to change the permission set of a file or directory. The permissions can be entered as symbolic notation or octal notation, but octal notation is typically preferred for speed of entry. A change is made by entering, example   sets the permissions on the   file to. The recursive option can used by entering  after   (before permission annotation).

Note that  often requires root privileges to execute.

setgid
It is possible to force all new entries created in a directory to always belong to the same group the parent directory belongs to. This is enabled by entering  instead of the permission annotation, for example,. When a directory has an  in the group write permissions, this means that   has been enabled. ( is always the current directory and   is the parent directory) Example:

setuid
Note that despite what some sites on the Internet may state, although  can be flagged in Linux, the OS will ignore the flag and new files will always be owned by their creators.

cp
The  command is used to copy files or directories. When using the command, the new file or directory can also be given a new name. For example,. The new file or directory can be in the same directory as the one being copied. This is useful for backing up configuration files before editing or copying configuration files that are being applied to new items, such as virtual servers in nginx.

exit
The  command is used to exit the current shell session. For example, if a  session was entered,   would return to the session to user session.

ll
The  command is used to list the contents of a directory. While there are other commands, such as, to accomplish the same thing,   is typically more useful as it will list ownership and permissions for each file and directory as well as listing hidden files and directories. If the directory being queried is particularly large, using the  option after the directory location will allow for viewing the contents of the directory (navigated with page up/page down) more easily and can be exited by pressing.

logout
The logout command will log the current user out of the session.

mkdir
The  command is used to create a new directory. Example:  or   to make a new sub-directory of the current directory.

mv
The  command is used to move files or directories. For example,. As with the  command, the moved file or directory may be in the same directory. This is usually the easiest way to rename a directory or file.

nano
The  text editor is discussed in the Intro to command line article.

poweroff
This command will shutdown Ubuntu and turn the computer's power off. It is only used on physical machines, never virtual machines such as VPS and cloud servers. Powering off a VPS or cloud server will only shut down Ubuntu while the hosting company will continue to charge as if the server were running.

shutdown
The  command is not usually used as it only shuts down Ubuntu without powering off the enclosure, but is mentioned here as many new administrators mistakenly use it.

reboot
The  command reboots the server and requires root privileges.

rm
The  command removes the targted file.

rm -rf
Running the  command with options   can be used to delete a directory and its contents. Caution! Linux has few built-in safety nets and will execute this command as permissions allow, so using root privilege to delete a directory containing vital system files will destroy the system! Additionally, there is no 'trash', so all deletions are permanent. On top of that, the user will not be asked to verify whether the target should be deleted, so pressing enter is all that's required to permanently delete the target!

rmdir
The  command removes the specified empty directory.

service
It is often necessary to restart services after making changes to configuration files. This can be accomplished with, and nearly always requires root privileges.

su
The  command is use to change user sessions. A common usage is to change to a root session using, but it can also be to change to a different user, for example,.

sudo
The  command is discussed in the Intro to command line article.

Useful commands
The commands below are used somewhat infrequently, but are convenient when working towards certain goals.

dig
As its man page description states: (domain information groper) is a flexible tool for interrogating DNS name servers. It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried. Most DNS administrators use dig to troubleshoot DNS problems because of its flexibility, ease of use and clarity of output.

It can be particularly useful for seeing what other admins have done when setting up various different DNS records. The  modifier can be used to provide a terse output instead of the default verbose output. Below are some examples for viewing specific records (note that a subdomain should be used when appropriate):

A records
username@servername:~$ dig example.com +short

AAAA records
username@servername:~$ dig example.com aaaa +short

DKIM records
username@servername:~$ dig -t txt selector._domainkey.example.com +short

DMARC records
username@servername:~$ dig -t txt _dmarc.example.com +short

DMARC RUF/RUA reporting address different from organization domain
username@servername:~$ dig -t txt example.com._report._dmarc.example.org +short

MX records
username@servername:~$ dig -t mx example.com +short

PTR records
username@servername:~$ dig -x  +short

SPF records
username@servername:~$ dig -t txt example.com

tar
The  package is installed by default in Ubuntu. It is both a file format and a program. The program can be used to create and extract compressed  files, which may be further compressed with the    or    programs, which are also installed by default in Ubuntu. Files created with  are commonly referred to as tarballs.

The biggest advantage  has over   is that the archive can be created to retain information such as file ownership and permissions. The biggest disadvantage is that  files are generally only usable by *nix operating systems, so users of Windows will have to download some additional utility, such as 7-Zip, to extract the files.

Using tar
As popular as  is, remembering the myriad options can be difficult, and it pretty much can't be used without options. Below is an option set that will allow compression and extraction to retain file ownership and permissions. Note that the options and their usage may not function as stated below in non-Ubuntu Linux distributions and other *nix operating systems.

Options for compressing with tar and gzip
This command will create  from the files or directories specified, compress them using the   program and will preserve ownership and permission information.

username@servername:~$ tar -zcvf archivename.tar.gz fileordirectoryname1 fileordirectoryname2

Options for compressing with tar and bzip
To compress using, simply use the above commands, but add the   option:

username@servername:~$ tar -jcvf archivename.tar.bz2 fileordirectoryname1 fileordirectoryname2

Extracting tarballs
This command will extract tarballs to the current directory and retain ownership and file permissions. Executing without  will perform the command, but will assign the current user as owner while retaining file permissions. Note that it may be somewhat inconvenient to use  unless it is known that the archive was created with correct ownership and permissions.

username@servername:~$ sudo tar -xvf archivename.tar.gz

Note that this option set will extract both  and   archives.

Adding the  option will extract the archive to.

username@servername:~$ sudo tar -xvf archivename.tar.gz -C /directoryname/