How to hosts file

The What

Thankfully this can be done by modifying the ‘hosts’ file on your local system.  The hosts file is the first place your system will look when trying to resolve a domain name to an IP address.  This lets us supersede the information in the world wide DNS network for a single computer.  The exact steps to modify the file do vary slightly based on operating system, but they do all share a few similarity.

1) As it’s a system file you’ll need to be a privileged user to modify the file.  On windows this means using the ‘open as administrator’ feature.  In linux or on a Mac you’ll need root or ‘super user’ privileges.

2) Each sub-domain you’d like to resolve to the specified IP address must be present in the hosts file (ie. domain.tld shop.domain.tld and www.domain.tld would each need to be explicitly specified).

3) Each line should start with an IP address followed by the domain or domains that should resolve.  For example:

321.456.987.789      domain.tld www.domain.tld shop.domain.tld otherdomain.tld

4) If you have multiple domains your adding you can put them all on a single line, or create a new line for each domain.  Just be sure that every line starts with an IP address.

The Why

Generally the DNS system of name servers does a great job translating domain names to IP addresses for us.  However there can come a time when you’d like http://domain.tld to resolve to xyz.zyx.yzx.zxy instead of abc.bca.cba.acb  This is often the case when testing a site in a new environment, but there are certainly other situations where you might want this behavior.

The How

The exact steps needed to modify the file based on operating system.

Windows 7 / Vista / XP / NT

1) Open Notepad as with elevated privilages.  To do this click Start and look for the notepad entry.  Right click on Notepad and select ‘open as administrator’

2)  Click onto File -> Open and navigate to the hosts file.  It should be in %systemroot%\system32\drivers\etc\ which generally works out to be C:\windows\system32\drivers\etc\

3) Once the file modifications have been made click Save

Windows 95 / 98 / ME

1) Open Notepad in the normal fashion, no privilege escalation is needed.

2) Open c:\windows\hosts  If the file does not exist you can create a new file of that name.

3) Once the additions to the file have been made simply save the file.

Linux

1) Open /etc/hosts in the text editor of your choice.  From command line the below should do the trick

sudo vim /etc/hosts

2) After the modifications have been made save the file.  In VIM this would be “:wq”

OSX -  10.2 and later

1) Open the Terminal App

2) Open the hosts file with escalated privilages.  The below should do the trick

sudo nano /private/etc/hosts

3) After modifying the file use ‘control+0′ to save the file (press enter when prompted for the file name) and then ‘control+x’ to quit

After you’ve saved the changes to the hosts file we’ll need to make those changes active.  This means we’ll need to clear out the systems DNS cache.  The simplest way to do this is to just restart your computer but this has always struck me as a bit heavy handed.

If you’ve got access to a FireFox browser you can use an addon like DNS Flusher which will clear your DNS cache with the click of a button and also displays the IP of a site as you view it.

If command line is more you’re style try one of the following:

Windows

From the command line run “ipconfig /flushdns”

OSX 10.2 and on

From a terminal run “lookupd -flushcache”

Linux

Restart your dns service.  Likely this is nscd, but bind, dnsmasq and others could be present on your system.  /etc/init.d/ and/or service restart will be your friend.

Once the DNS cache has been purged the domain name will resolve to the new IP address for your system.  When you no longer want this behavior simply remove or comment out the line (add a # to the start of a line to comment it out), save, and re-flush your DNS cache

Basic Linux Commands

About a year and a half ago I got a new job and had an opertunity to use linux for the first time on a day to day basis.  Prior to this I had toyed around with a couple of installations, but never left them in place for more than a day or two before I gave up and went back to windows.  This time that wasn’t an option.  The new job not only required that I use linux, but that the majority of the work was done at the command line.  I was surprised at how user friendly the linux CLI turned out to be.  I found that I was able to get by just fine with the assistance of the linux comand cheat sheet that I made for myself.   The layout is name of command — how I remember the command — description of the command.  These are the commands that I found essential to learning and using the linux command prompt.

  • man — Manual Page — This command is the key to all other commands.  Basically it brings up an in depth help file for any command.  Man shows a brief synopsis of what the command dose and an example of the formatting for the command, and a list and explanation of any options/flags/switches the command may accept.   Copyright, bug reporting, and authorship are also often included, but from a learning linux point of view aren’t really as useful. The format is “man command” for example “man ls” or “man tar”
  • ls — List Stuff — This is the linux exivelant of the DOS ‘dir’ command.  When entered as ‘ls’ it will ouput a listing of the contents of the current directory.  You can also pass either full or relitive paths to ls to get a listing of a directoy other than the one you’re currently in.  For example ‘ls /home/trippinglizard/public_html’ or ‘ls public_html’ would output the same thing if entered from the working directory of /home/trippinglizard.
  • cd — Change Directory –  This command lets you move your location in the filesystem from one directory to another.  Cd can be passed a full path to imidiatly jump to a specific location; ‘cd /home/trippinglizard/public_html’.  If the path you pass cd dosn’t begin with a ‘/’ then the path is relitive to your current directory.  So if I where in /home/trippinglizard I could enter the public_html directory by typing ‘cd public_html’.
  • pwd — Present Working Directory –  In linux the same command entered in different directories can in some cases have vastly diffrent results.  This makes knowing where you are currently ‘working’ in the filesystem very important.   This is the ‘where am I?’ command.  The output of this command is the full path of the current, or present, working directory.  When jumping around a command prompt that dose not list the current working directory in the prompt it can be easy to get a little lost.  Pwd will tell you exactly where you’ve ended up.  The format is just “pwd”
  • vim/nano/vi — Text Editor — In linux many settings are stored in ‘flat files’.  This means that if you’re going to do any work on a server you’ll need to be able to edit these files.  Vi, vim, and nano are 3 common editors.  Personally I very much so prefer vim, but it is a matter of personal prefference.  Each of these text editors have enough commands to make a seperate cheet sheet nessisary.  To open a file with vim it’s ‘vim /path/to/filename’.  If the file dose not already exist it will be created, if it dose exist you’ll see and be able to edit the contents of the file.
  • | — Pipe — This is used to send the output of one command as the input for the next command.  For example if you where looking for the details of all files with bob in there name in the current directory you could use ‘ls -lah | grep bob’
  • grep — Filter — This command is a serch utility of sorts.  My favorite use is parring this command with | to help limit the results.  Looking for all messages pertaining to /dev/sda in dmesg, you could use ‘dmesg | grep sda’  All of the contents of dmesg would be printed to the screen, but grep will only print the lines that actually contain sda.
  • chmod — Change/Modify — this command is used to change the permissions of a file or folder.  File permissions in linux can be represented as a 3 digit number, 777, 644, 755, etc.  The first number represents the permission for the owner, the second is for the group, and the 3rd is for everybody else.  The format would be ‘chmod XXX filename’
  • chown — Change Ownership — This is the command that you would use to change the user or group ownership of a file.  The syntax is ‘chmod user:group filename’

–force

One of the nice things that a cPanel server comes with is the /scripts folder.  It’s full of all sorts of usefull tools and is probably worthy of a post just going over the various tools there, but that isn’t this post :)   Sufice it to say there are scripts for everything from backup up or restoring accounts, to rebuilding configuration files, or re-installing all of the base cpanel files.

Certain scripts, such as the cPanel backup script, and the restore account script have conditions that they check for before running.  Is the system supposed to generate a backup today?  Dose the user you’re attempting to restore already exist?  When a script fails one of these checks the script will refuse to run.  Normally this is a helpful way to keep you from accidentally doing something dangerous.  However, sometimes what you want the server to do is to just run the command you issued it, yes we know we’re restoring over a user, we mean to.  The way you tell a cPanel script to go anyways is to pass the command the –force option, for example /scripts/cpbackup –force  will initiate a run of the cPanel backup script, even if it’s already backed up accounts for the day.

Backing up, and restoring a cPanel account

If you care about your website content it is absolutely imparitive that you keep backups.    I can’t count the number of times I’ve heard a client say “but I can’t loose that data, it’s my whole buisness” and when I ask if they have any backups I usually get the “Well no.  why would I need those?” GRAAAAAHHHHH So again I say, if you care about your content, keep backups, multiple backups, keep them off of the server that you’re hosted on.  If you have a backup of your website in your public_html folder, and the server dies, guess what, you just lost your backup too.

cpanel servers make it very easy to make a backup.  You just log into the cPanel interface, click onto backups, and then click generate a full account backup.  It’ll give you an option to save the file to the server, or you can download it.  This is great….most of the time.  For some accounts, generally accounts around 2 GB in size or larger, this method is just full of fail.  This is because the web backup processes just times out before the backup is complete and your not left with a usable backup.  The best way to get around this is to get to the command line of your server and run things the old fashioned way.

Backing up and account with root SSH access

  1. Log into the server via SSH.  If you’re on a windows computer you’ll need a program such as PuTTY to do so.
  2. Start a screen session with the command ‘screen -S pkgAcct’  This way if you’re connection is droped part way through the backup process the backup will continue to run
  3. Start the backup processes with ‘/scripts/pkgacct USER’ where USER is the cPanel username of the account you’re backing up.
  4. Once the backup finishes it will place a cpmove-USER.tar.gz file in /home which can then be downloaded via FTP.  This file will be owned by root, in a folder that is owned by root.  Move the file to an alternate location if you don’t have root level FTP access to download the file.

Backing up an account with user level SSH access is almost the same, but step 3 is a little diffrent.  Instead you’d want to use ‘/scripts/pkgacct USER /path/to/users/home/folder’  This way the restored pkg will be placed not in /home where a user level account cannot access it, but instead in the users home folder where you’ll be able to get access to download the file via FTP

Great, so now you’ve got a backup of your content.  Save it to your home computer, put it on a CD, basically just keep it safe for that day that will eventually come when you need to restore your account.

To restore a cPanel account via an ssh connection you’ll need to be able to log into the server as the root user.  Then it’s just a matter of following these steps:

  1. Copy the cpmove file you’d like to restore to the /home folder of the server
  2. Log into the server as root via ssh
  3. start a screen session, the same way as above
  4. start the restore with ‘/scripts/restorepkg USER’ where USER is the name of the user you’re restoring.

These same steps can easily be used to migrate an account from one server to another.