Difference between revisions of "Common and useful commands"

From UNPM.org Wiki
Jump to navigation Jump to search
Line 1: Line 1:
=== Common and useful commands ===
+
== Common 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 repitition 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!
 
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 repitition 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!
Line 5: Line 5:
 
Note that nearly everything in Linux is case-sensitive, and most commands are entered as lower-case, including all of the commands below.
 
Note that nearly everything in Linux is case-sensitive, and most commands are entered as lower-case, including all of the commands below.
  
==== cd ====
+
=== /bin/bash ===
  
Used to change directory of the command prompt and is discussed above.
+
Though not exactly a command, entering <code>sudo /bin/bash</code> will open a <code>root</code> session. Note that there are several different ways to enter a <code>root</code> session, each with subtle yet distinct differences.
  
==== ll ====
+
=== cd ===
  
The <code>ll</code> command is used to list the contents of a directory. While there are other commands, such as <code>ls</code>, to accomplish the same thing, <code>ll</code> 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 <code>|less</code> 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 <code>q</code>.
+
Used to change directory of the command prompt and is discussed in the [[Intro_to_command_line#Navigation|Intro to command line]] article.
 
 
==== cp ====
 
 
 
The <code>cp</code> 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, <code>cp /path/to/fileordirectory /new/path/newfileordirectoryname</code>. 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.
 
 
 
==== mv ====
 
 
 
The <code>mv</code> command is used to move files or directories. For example, <code>mv /path/to/fileordirectory /new/path/neworsamefileordirectoryname</code>. As with the <code>cp</code> command, the moved file or directory may be in the same directory. This is usually the easiest way to rename a directory or file.
 
  
==== mkdir ====
+
=== chown ===
 
 
The <code>mkdir</code> command is used to create a new directory. Example: <code>mkdir /path/to/directory/newdirectoryname</code> or <code>mkdir newdirectoryname</code> to make a new sub-directory of the current directory.
 
 
 
==== rmdir ====
 
 
 
The <code>rmdir</code> command removes the specified empty directory.
 
 
 
==== rm ====
 
 
 
The <code>rm</code> command removes the targted file.
 
 
 
===== rm -rf =====
 
 
 
Running the <code>rm</code> command with options <code>-rf</code> 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!
 
 
 
==== nano ====
 
 
 
The <code>nano</code> text editor has been discussed already in this article.
 
 
 
==== chown ====
 
  
 
The <code>chown</code> command is used to change the owner or group a file or directory belongs to. For example, <code>chown username directoryorfilename</code> will assign <code>username</code> as the owner of the directory or file specified. Using <code>chown username:groupname directoryorfilename</code> will assign <code>username</code> as the owner and <code>groupname</code> as the group of the directory or file specified. Using the recursive option by entering <code>chown -R</code> when changing user or group ownership on a directory will apply the changes to all files and subdirectories in the targeted directory.
 
The <code>chown</code> command is used to change the owner or group a file or directory belongs to. For example, <code>chown username directoryorfilename</code> will assign <code>username</code> as the owner of the directory or file specified. Using <code>chown username:groupname directoryorfilename</code> will assign <code>username</code> as the owner and <code>groupname</code> as the group of the directory or file specified. Using the recursive option by entering <code>chown -R</code> when changing user or group ownership on a directory will apply the changes to all files and subdirectories in the targeted directory.
Line 48: Line 19:
 
Note that, as with all Linux commands, using <code>chown</code> will depend on file and directory permissions, and typically will require root privileges to execute.
 
Note that, as with all Linux commands, using <code>chown</code> will depend on file and directory permissions, and typically will require root privileges to execute.
  
==== chmod ====
+
=== chmod ===
  
 
The <code>chmod</code> 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 <code>chmod {permission annotation} filenameordirectoryname</code>, example <code>chmod 750 /home/username/test</code> sets the permissions on the <code>test</code> file to <code>drwxr-x---</code>. The recursive option can used by entering <code>-R</code> after <code>chmod</code> (before permission annotation).
 
The <code>chmod</code> 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 <code>chmod {permission annotation} filenameordirectoryname</code>, example <code>chmod 750 /home/username/test</code> sets the permissions on the <code>test</code> file to <code>drwxr-x---</code>. The recursive option can used by entering <code>-R</code> after <code>chmod</code> (before permission annotation).
Line 54: Line 25:
 
Note that <code>chmod</code> often requires root privileges to execute.
 
Note that <code>chmod</code> often requires root privileges to execute.
  
===== setgid =====
+
==== 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 <code>g+s</code> instead of the permission annotation, for example, <code>chmod g+s /targetdirectory/</code>. When a directory has an <code>s</code> in the group write permissions, this means that <code>setgid</code> has been enabled. (<code>.</code> is always the current directory and <code>..</code> is the parent directory) Example:
 
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 <code>g+s</code> instead of the permission annotation, for example, <code>chmod g+s /targetdirectory/</code>. When a directory has an <code>s</code> in the group write permissions, this means that <code>setgid</code> has been enabled. (<code>.</code> is always the current directory and <code>..</code> is the parent directory) Example:
Line 70: Line 41:
 
</code>
 
</code>
  
==== Restarting services ====
+
==== setuid ====
 +
 
 +
Note that despite what some sites on the Internet may state, although <coded>setuid</code> can be flagged in Linux, the OS will ignore the flag and new files will always be owned by their creators.
 +
 
 +
=== cp ===
 +
 
 +
The <code>cp</code> 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, <code>cp /path/to/fileordirectory /new/path/newfileordirectoryname</code>. 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.
  
It is often necessary to restart services after making changes to configuration files. This can be accomplished with <code>service servicename restart</code>, and nearly always requires root privileges.
+
=== exit ===
 +
 
 +
The <code>exit</code> command is used to exit the current shell session. For example, if a <code>root</code> session was entered, <code>exit</code> would return to the session to user session.
  
==== reboot ====
+
=== ll ===
  
The <code>reboot</code> command reboots the server and requires root privileges.
+
The <code>ll</code> command is used to list the contents of a directory. While there are other commands, such as <code>ls</code>, to accomplish the same thing, <code>ll</code> 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 <code>|less</code> 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 <code>q</code>.
  
==== logout ====
+
=== logout ===
  
 
The logout command will log the current user out of the session.
 
The logout command will log the current user out of the session.
  
==== poweroff ====
+
=== mkdir ===
 +
 
 +
The <code>mkdir</code> command is used to create a new directory. Example: <code>mkdir /path/to/directory/newdirectoryname</code> or <code>mkdir newdirectoryname</code> to make a new sub-directory of the current directory.
 +
 
 +
=== mv ===
 +
 
 +
The <code>mv</code> command is used to move files or directories. For example, <code>mv /path/to/fileordirectory /new/path/neworsamefileordirectoryname</code>. As with the <code>cp</code> 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 ===
  
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 cloud server will only shut down Ubuntu while the hosting company will continue to charge as if the server were running.
+
The <code>nano</code> text editor is discussed in the [[Intro_to_command_line#Linux_command_line|Intro to command line]] article.
  
===== shutdown =====
+
=== 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 <code>shutdown</code> 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.
 
The <code>shutdown</code> 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 <code>reboot</code> command reboots the server and requires root privileges.
 +
 +
=== rm ===
 +
 +
The <code>rm</code> command removes the targted file.
 +
 +
==== rm -rf ====
 +
 +
Running the <code>rm</code> command with options <code>-rf</code> 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 <code>rmdir</code> 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 <code>service servicename restart</code>, and nearly always requires root privileges.
 +
 +
=== su ===
 +
 +
The <code>su</code> command is use to change user sessions. A common usage is to change to a root session using <code>sudo su -</code>, but it can also be to change to a different user, for example, <code>sudo su www-data</code>.
 +
 +
=== sudo ===
 +
 +
The <code>sudo</code> command is discussed in the [[Intro_to_command_line#sudo|Intro to command line]] article.
  
 
== Useful commands ==
 
== Useful commands ==

Revision as of 23:34, 20 May 2014

Common 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 repitition 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 sudo /bin/bash will open a root session. Note that there are several different ways to enter a root 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 chown command is used to change the owner or group a file or directory belongs to. For example, chown username directoryorfilename will assign username as the owner of the directory or file specified. Using chown username:groupname directoryorfilename will assign username as the owner and groupname as the group of the directory or file specified. Using the recursive option by entering chown -R 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 chown will depend on file and directory permissions, and typically will require root privileges to execute.

chmod

The chmod 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 chmod {permission annotation} filenameordirectoryname, example chmod 750 /home/username/test sets the permissions on the test file to drwxr-x---. The recursive option can used by entering -R after chmod (before permission annotation).

Note that chmod 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 g+s instead of the permission annotation, for example, chmod g+s /targetdirectory/. When a directory has an s in the group write permissions, this means that setgid has been enabled. (. is always the current directory and .. is the parent directory) Example:

username@servername:~$ mkdir testdirectory
username@servername:~$ sudo chown username:adm testdirectory/
username@servername:~$ chmod g+s testdirectory/
username@servername:~$ touch testdirectory/test
username@servername:~$ ll testdirectory/
drwxrwsr-x 7 username adm      4096 May 12 0915 .
drwxr-x--- 3 username username 4096 May 10 1548 ..
-rw-rw-r-- 1 username adm         0 May 12 0916 test
username@servername:~$ rm -rf testdirectory/

setuid

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

cp

The cp 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, cp /path/to/fileordirectory /new/path/newfileordirectoryname. 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 exit command is used to exit the current shell session. For example, if a root session was entered, exit would return to the session to user session.

ll

The ll command is used to list the contents of a directory. While there are other commands, such as ls, to accomplish the same thing, ll 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 |less 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 q.

logout

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

mkdir

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

mv

The mv command is used to move files or directories. For example, mv /path/to/fileordirectory /new/path/neworsamefileordirectoryname. As with the cp 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 nano 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 shutdown 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 reboot command reboots the server and requires root privileges.

rm

The rm command removes the targted file.

rm -rf

Running the rm command with options -rf 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 rmdir 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 service servicename restart, and nearly always requires root privileges.

su

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

sudo

The sudo 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:

dig (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 +short modifier can be used to provide a terse answer instead of the default verbose answer. Below are some examples for viewing specific records (note that 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 <IPv6 or IPv4 address> +short

SPF records

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

External links

DiG HOWTO

External links

DiG HOWTO