Contents |
Unix operating systems provide the foundation of the internet. For 30 years, unix systems have been the standard for multi-user, networked computing.Unix is strong, powerful, flexible, robust, and fully multi-tasking. It is supremely suited for running Internet servers. When a server does go down, it is usually due to a hardware failure. Unlike other operating systems, Unix itself almost never fails.Occasionally, the services running under Unix may crash. This is almost always due to a configuration problem. For example, Apache is extremely strict in its requirement that your configurations be 100% correct. If you make Apache configuration changes which are only 99.99% correct, Apache will crash. Your website will then be down until your configurations are fixed.Therefore, it is critical that you follow these two guidelines when making changes to your server:
1. ALWAYS make a backup copy before editing a file! Everyone makes mistakes, but a true expert limits the potential impact of a misconfiguration by having a backup copy of the original available. 2. Do not change or delete anything unless you are absolutely certain that what you are doing is correct. When in doubt, don't guess – find a reference and look it up. You will be able to avoid most disasters by following these suggestions.
From this point on, all instructions are given in the context of the command-line interface, unless otherwise specified. To us humans, the command-line interface (or CLI) takes the form of a prompt (such as the percent or greater-than sign) and a blinking cursor.The command-line interface allows you to "speak" directly to your server, without the interference of several GUI (Graphical User Interface) layers between you and the actual operating system. This communication goes both ways: your server responds to your commands, and occasionally replies to you directly.Most of the time, your server will only reply if it needs clarification, or if it did not understand your command. If your server performs your command, then it will usually just return you to the prompt.
To get to the command prompt, you will need a connection program. Although most Windows installations include a telnet program, your server will not allow telnet connections by default. Telnet is not encrypted, which means that the details of your session (including your user name and password) are sent across the internet in plain-text form. Thus anyone can easily read your login information.You will want to connect to your server with the SSH protocol, which sets up a secure connection. There are several SSH programs available for Windows users, including Putty (which is free), and SecureCRT (which is not). If you are a Macintosh user, we recommend using F-Secure.Once you have downloaded and installed an SSH program, connect to your server using your domain name, admin user name, and password.Note: For security reasons, you will not be able to connect to your server as root. Instead, you will have to connect to your server as your admin user, then su to root.
Once you have connected to your server and logged in, you will see a prompt. When you connect to your server, you will automatically be located in your home directory. To verify where you are, type the command:
pwd
Then hit "Enter". Your server will reply with the file path to your current location.(As a rule, Unix commands are abbreviations of a word or phrase. The command "pwd" is short for "Path to Working Directory.")To list all the files and directories at your current location, give the command:
ls -l
This will return a List Long of all the files and directories, as well as their permissions settings, and their last-modified date. For a List Short, use the command:
ls
This command simply gives you the names of the files, without any extra detail. If we were to put this in standard Unix terms, we would say that "the ls command returns the names of files in your current working directory."Both of the list commands will also list any directory which you care to specify. For example, to list the contents of your /www/vhosts directory from anywhere on your server, use the command:
ls /www/vhosts
The Change Directory command is the most basic way to move to a different directory. You can use it to specify which directory you want to move to. For example, to go to your /www/vhosts directory, you would use the command:
cd /www/vhosts
In the example above, "cd" is the command, and "/www/vhosts" is the argument. (Note: The command and the argument must always be separated by a space.) To translate this into Unix-speak, "the cd command takes a file path as an argument."The cd command takes several other items as an argument (including nothing). To go up one level (i.e. from /www/vhosts to /www) use two periods as the argument:
cd ..
When used alone (i.e. without an argument) the cd command will return you to your home directory. Use the command above to go to your /www/vhosts directory, then return to your home directory by simply giving the command:
cd
Now use the pwd command to verify that you are indeed in your home directory.
Return to your home directory (if you're not there already). For the next exercise, we must first create a new, empty file named "myfile" using the "touch" command:
touch myfile
If you list the files in your directory, you will see "myfile" listed there. If you use the ll command, you'll see that the file size is 0 bytes.Because we want to follow best practices, let's begin by making a backup copy of this empty file. Use the cp command (short for CoPy) to make a copy of myfile called myfile.bak:
cp myfile myfile.bak
Note: Unix itself does not care about file extensions (although other programs such as Apache are more picky). However, it has become standard practice to use the extension .bak or .old to designate a backup copy.Now that we have a backup copy of "myfile" in place, let's move (MoVe) the original file to your /www/vhosts/yourdomain.com/htdocs directory:
mv myfile /www/vhosts/yourdomain.com/htdocs
Next, we will follow it to that directory. The cd command also takes !$ as an argument. !$ is very useful. It essentially translates to "the last argument I just gave you."
cd !$
Note: In this context, your server will not only expand !$ to /www/vhosts/yourdomain.com/htdocs and change your directory there. It will also reply with this information, so that you can confirm that it expanded !$ correctly. It will then return you to the command prompt.Like the cd command, the mv command will take two periods as an argument meaning "up one directory." Let's move myfile from /www/vhosts/yourdomain.com/htdocs up one level, to the /www/vhosts/yourdomain.com directory:
mv myfile ..
Change directory up one level, then verify that the file is here by listing the contents of the directory.Finally, move "myfile" back to your home directory, using the command:
mv myfile /usr/home/username
Once you and "myfile" have both returned to your home directory, let's exercise one other function of the mv command. This command can not only move files; it can rename them while doing so.The mechanics behind this are simple. When your server receives your mv command, it checks to see if the destination is an existing file or directory. Based on the results, it does one of the following: 1. If the destination IS an existing directory, then your server moves the file into that directory. For example, the following command would move the file "myfile" into your /www/vhosts/yourdomain.com/htdocs directory:
mv myfile /www/vhosts/yourdomain.com/htdocs
2. If the destination IS NOT an existing file or directory, then your server moves the file to that location, and gives it the name you have specified. For example, the following command would move the file "myfile" to your /www/vhosts/yourdomain.com/htdocs directory AND changes the name of "myfile" to "my-new-file":
mv myfile /www/vhosts/yourdomain.com/htdocs/my-new-file
3. If the destination IS an existing file in the specified directory, then your server moves your file into that directory, overwrites the existing file, and gives your file the name of the existing file. For example, the following command would move "myfile" into your /www/vhosts/yourdomain.com/htdocs directory, rename it to "mywebfile.html", and overwrite your existing "mywebfile.html" file with "myfile". If this is what you want to do, then it has instantly been accomplished. However, if you did NOT want to overwrite the existing file, then the following command could be a disaster.
mv myfile /www/vhosts/yourdomain.com/htdocs/mywebfile.html
As you can see, the mv command is (like all Unix commands) a powerful tool. It can also be dangerous, if used sloppily or incorrectly. As a rule, your server will not prompt you with "Are you sure?" There is no "undo" command. For better or worse, Unix generally assumes that you know exactly what you are doing.The simplest way to change the name of a file is to use the third example to your advantage. Ever flexible, Unix will follow the third protocol (listed above) either with or without a directory path. To change the name of "myfile" to "my-new-file" without moving it to a different directory, simply give the command:
mv myfile my-new-file