Some rifles, such as the Steyr AUG, are already manufactured with built-in scopes,
Steyr AUG Assault rifle. Click on image to enlarge.
Image licensed from Steyr Mannlicher GmbH & Co. under the Creative Commons Attribution 2.0 License.
In many other rifles, they already come with some sort of mounting system built in (e.g. Weaver rail, Picatinny rail etc.). Examples of this would be the M4, H&K 416 etc. Even if there is no rail system built in, many modern rifle manufacturers usually have a couple of holes drilled into the receiver, so that the user can attach a scope mount base later. As for rifles that were built before scopes became popular (e.g. the Mosin Nagant rifle), a scope can still be attached to these, either by replacing the standard rear sight with a sight that allows a scope to be mounted, or by drilling the appropriate holes into the receiver and/or stock and attaching a scope base. Of course, the last couple of procedures are probably best done by a qualified gunsmith.
Assuming this is a relatively modern rifle, the receiver should already be pre-drilled or grooved to attach a scope base. The first step is to remove any filler screws and degrease the screw holes, then mount the scope base on to the rifle, applying a little bit of loctite or oil to the screws before screwing them in. '
The next step is to attach the scope rings to the base. These come in two halves, which can be separated.
The bottom of the scope rings are shaped with dovetails to attach to the scope base securely. The next step is to ensure that the scope rings are aligned correctly. To do this, the user uses a scope ring alignment tool. This consists of two precisely machined rods with pointed conical tips. The user slides one rod into each ring and tries to adjusts the alignment of the two scope rings until the two pointed ends of the two rods are almost touching.
Scope Ring Alignment Tool by Wheeler
Once the two scope rings are aligned properly, the user unscrews the top halves of the two scope rings, drops the scope in to the lower halves and then screws the top halves back on. Care is taken to mount the scope a little forward (i.e. give it eye relief), so that there is enough gap between the scope and the user's face to account for any recoil after the rifle is fired.
Once the scope is attached, the next step is to align the sights properly. One cheap way to do this is to remove the bolt from the rifle, then aim it at a target by looking through the barrel from the back, then ensure that the scope crosshairs are also pointing to the same point on the target. Of course, this only works for bolt-action rifles and doesn't work with lever action or semi-auto rifles. For these rifles, a laser bore sighting tool is used instead. These usually slide into the muzzle, or can be placed in the chamber. Either way, they project a laser beam, which can be pointed to a target and then the user can check if the scope crosshairs are also aligned to the same point or not.
In chamber bore sighting tool.
Bore sighting tool.
After this, the user needs to still take the rifle out to the range and align the sights properly by firing test-groups at a target.
The next two videos by Midway USA show how this process is done in detail: