Saturday, June 19

Ten Linux Commands .... Part 2

5. Finding World-writable Files

For security reasons you should always ensure that your website's public-facing files are not world-writable. Otherwise, you could leave your server vulnerable to defacement by an outside party. Hopefully, you configure your file ownership and permissions to disallow such a gaffe from the outset, but it doesn't hurt to regularly audit your server to confirm no such security holes exist. Rather than exhaustively browse the server documents directory, you can instead use the find command to scan a specific directory structure for files configured with certain permissions:





%>find /var/www/ -type f -perm -o+w -exec ls -l {} \;

In this example, I tell find that I'm looking for files (by
 setting the -type option to f). You can 
alternatively search for directories by setting -type to d.
 Finally, I use the -exec option in order to format any 
found files using the ls command.



6. Backing Up Your Web Directory

Your web hosting provider presumably has implemented a fairly routine backup service. However, I nonetheless encourage you institute your own backup procedure in order to be absolutely certain you'll be able to easily restore your site in the event of a catastrophic server failure. Quite a few solutions exist for backing up your data, among them Amanda, but you can create your own home-brewed solution using the tar command. For instance, to back up the web directory located at /var/www/www.wjgilmore.com while preserving file permissions and excluding the directory /var/www/www.wjgilmore.com/cache/ (as it contains cached files, which are not important for backup purposes), use the following command:

%>tar cpzf www.wjgilmore.com.backup.042710.tgz /var/www.wjgilmore.com

To restore the directory structure, you again use tar, 
but this time passing the x switch:


%>tar xvpfz www.wjgilmore.com.backup.042710.tgz -C /var/www/
Using tar in conjunction with scp and key-based authentication, you could completely automate the backup process and move the tar file to a remote server!

7. Viewing Your Command History

When getting acquainted with Linux's occasionally esoteric syntax, you'll regularly attempt to recall a particular syntax you executed while attempting to debug a server problem. Rather than continuously referring to bookmarks or other learning resources to recreate the command, you could review your command history using the history command. Executing history will produce a list of the commands you executed. A sample of the output looks like this:
119  more chapters/staging/chapter06.docbook
120  ./convert_program_listings.rb
121  ./convert-chunks.sh 
122  pwd
123  dir
124  more .gitignore 
125  vim .gitignore 
126  git init

Because each command is accompanied by its sequence number, you can 
easily execute it anew simply by prefacing the sequence number with an 
exclamation mark, like this:



%>!124
more .gitignore
betas/
cache/
chapters/staging



8. Creating Directory Trees

You'll often need to create a series of nested directories, particularly when starting new projects. Most novice users tediously create each directory by using the mkdir command and then enter each newly created directory only to create the next. You can perform this task in mere seconds using the -p option. For instance, the following example will create a new project directory named webapp, a directory named application inside it, and a directory named controllers inside application:
%>mkdir -p webapp/application/controllers

9. Creating Command Aliases

For whatever reason, I almost always prefer to list directory contents using a format that displays the permissions, owner and group names, size, modification date, and name, by passing the -al option to the ls command:
%>ls -al
Because I use this command so frequently, I've aliased it to something easier to type, namely dir, using the alias command:
%>alias dir='ls -al'
These aliases, however, are lost when you logout of the current session. To make them permanent, you can add them to an account configuration file such as .bashrc.

No comments:

Post a Comment