Getting Started with Linux Commands in WHM

Getting Started with Linux Commands in WHM

Overview

While cPanel & WHM automates many server administration tasks, familiarity with the Linux command line can prove useful for both WHM and cPanel users. This documentation gives a brief overview of some basic Linux commands that you may wish to use as you manage your website or server.

Access the command line

To access your cPanel & WHM server from the command line, perform the following steps:

  1. Use the correct method for your computer’s operating system to open the command line interface (CLI):
    • Microsoft® Windows® — Open the Command Prompt program. To do this, click Start and enter Command Prompt in the Search text box. Double-click Command Prompt in the list that appears.
    • Mac OS — Open the Terminal program. To do this, click the Applications icon in the dock, click the Utilities icon, and then click the Terminal icon.
    • Ubuntu — Open the Terminal program. To do this, press the Ctrl+Alt+T keys.
  2. Your CLI program opens with access to the local computer. You must use SSH to access the cPanel & WHM server. To do this, enter the following command and press Enter:
    ssh username@hostname

    In this comment, username is your account’s username, and hostname is the server’s hostname. For example, to log in as the root user on the example.com server, run the following command:

    ssh root@example.com
  3. The CLI prompts you for the account’s password. Enter it and press Enter.
  4. The CLI’s display will appear similar to the following example.
    Last login: Fri Feb  6 12:50:38 on ttys000
    MyComputer:~ myname$ ssh username@example.com
    Password: 
    Last login: Fri Feb  6 12:52:33 2015 from 10.1.4.202
    username@example [~]#

    You can now navigate the server’s file system and run commands, within the limits of your account’s permissions on the server.

     

Note:

If you receive an error message that appears similar to the following example, you may not have SSH privileges enabled on the server.

ssh: connect to host example.com port 22: Connection refused

Contact your hosting provider or system administrator for more information. Because command line access (shell access) has certain security implications, many web hosting providers restrict this access.

Common Linux commands

Note:

To execute a command, enter the command with any options or arguments that it requires, and press Enter.

 

The following table lists some basic Linux commands and their functions:

Command
Description
ls Lists files and directories that are within your current directory. This command is similar to the dir command in Windows.

To view dotfiles (filenames that begin with a period) and additional file and directory details, add the -al options to the command:

ls -al
cd location Navigates between directories.

Replace location with the path to the directory that you wish to navigate to. For example, to navigate to the /usr/local/apache/ directory, run the following command:

cd /usr/local/apache/
cat filename Prints the contents of the specified file to the CLI.

Replace filename with the relative path to the file that you wish to view. For example, to print the contents of the filename.txt file, run the following command:

cat filename.txt
tail filename Print the last 20 lines of a file to the CLI.

Replace filename with the relative path to the file that you wish to view. For example, to print the last 20 lines of the filename.txt file, run the following command:

tail filename.txt

You can add an argument to change the number of lines that this command prints. For example, to print the last 100 lines of the filename.txt file, run the following command:

tail -100 filename.txt
more filename Print the contents of a file to the CLI, one screen at a time.

Replace filename with the relative path to the file that you wish to view. For example, to print the contents of the filename.txt file one screen at a time, run the following command:

more filename.txt
pico filename Opens the specified file in the pico text editor.

Replace filename with the relative path to the file that you wish to edit. For example, to open the filename.txt file in the pico editor, run the following command:

pico filename.txt
vi filename Opens the specified file in the vi text editor.

Replace filename with the relative path to the file that you wish to edit. For example, to open the filename.txt file in the vi editor, run the following command:

vi filename.txt
grep string filename Searches for a string in a specified file, and prints each line that contains a match to the CLI.

Replace string with a single word, or multiple words within single quotes (''). Replace filename with the relative path to the file that you wish to search. For example, to search for the string coffee filters in the grocerylist.txt file, run the following command:

grep 'coffee filters' grocerylist.txt
touch filename Creates an empty file in the specified location.

Replace filename with the relative path to the file that you wish to create. For example, to create an empty example.txt file, run the following command:

touch example.txt
ln -s file1 file2 Creates a symbolic link between the two specified files.

Replace file1 with the relative path to the existing file, and file2 with the relative path to the new symbolic link file. For example, to create the symlink-file.txt file and point it to the/pointtome/file.txt file, run the following command:

ln -s /pointtome/file.txt symlink-file.txt
rm filename Deletes the specified file. After you run this command, the system prompts you to confirm the file’s deletion.

Replace filename with the relative path to the file that you wish to delete. For example, to delete the trash.txt file, run the following command:

rm trash.txt
last Lists which users recently logged in, with the timestamp for each login.
w Lists the users that are currently logged in, and the location from which they logged in.
netstat Lists all of the server’s current network connections.
file filename Guesses a file’s type, based on the file’s contents.

Replace filename with the relative path to the file for which you want the system to guess the type. For example, to cause the system to guess the type for the example.txt file, run the following command:

file filename
du Shows the system’s current disk usage for each directory and subdirectory.
wc filename Displays the word count for a specific file.

Replace filename with the relative path to the file for which you wish to view a word count. For example, to display a word count for the example.txt file, run the following command:

wc example.txt
cp file1 file2 Copies a file into a new file.

Replace file1 with the relative path to the existing file, and file2 with the relative path to the new copy file that you wish to create. For example, to copy the contents of the original.txtfile to the /copies/duplicate.txt file, run the following command:

cp original.txt /copies/duplicate.txt
chmod permissions filename Changes a file’s octal permissions.

Replace permissions with the three-digit octal permissions that you wish to grant to the file, and replace filename with the relative path to the file for which you wish to alter the permissions. For example, to change the permissions of the myfile.txt file to 755, run the following command:

chmod 755 myfile.txt

 

chown user:group filename Changes a file’s user and group ownership.

Replace user with the user to whom you wish to grant ownership of the file, group with the group name, and filename with the relative path to the file. For example, to grant the user joe in the group joesgroup ownership of the joesfile.txt file, run the following command:

chown joe:joesgroup joesfile.txt

 

Run multiple commands on the same line

Various command-line tasks may require that you use different commands on the same line. Linux includes easy methods to perform these tasks.

  • Use the pipe character (|) to retrieve data from one program and “pipe” it to another program.
  • Use a single greater-than bracket (>) to create a new file if the file does not already exist, or to overwrite any existing content if the file does exist.
  • Use a double greater-than bracket (>>) to create a new file if the file does not already exist, or to append the new data to the file if the file does exist.
  • Use a single less-than bracket (<) to send input from a file to a command.

The table below lists examples of how to combine tasks into a single line:

Command
Description
grep User /usr/local/apache/conf/httpd.conf | more
This command searches for all of the lines in the httpd.conf file that match the user search term, and then prints the results to your terminal one screen at a time.
last -a > /root/lastlogins.tmp
This command prints all of the current login history to the /root/lastlogins.tmp file.
tail -10000 /var/log/exim_mainlog | grep 'example.com' | more
This command finds the last 10,000 lines from the /var/log/exim_mainlog file, searches those lines for all occurrences of the string example.com, and then prints the search results to your terminal one screen at a time.

Note:

The system treats periods (.) in a command as wildcard characters. Precede each period with a backslash () to instruct grep to interpret the period literally.

netstat -an | grep :80 | wc -l
This command shows the number of active connections to Apache (Apache’s httpd daemon runs on port 80).
mysql --skip-column-names --batch -e 'show processlist' | wc -l
This command lists the number of MySQL threads. If subselect expressions start new threads, the output of the show processlist command includes them.

Was this article helpful?

Related Articles

Leave A Comment?