As others have said, the mechanism of the sniper rifle, whether autoloading or bolt action, should work. You would want to use a fine dry lubricant like molybdenum disulfide (Dri-Slide, SmoothKote) so the volatile compounds don’t boil off. However, the environmental problems of operating on the Moon are fairly formidable. The first and worst is dust; the fine, electrostatically-charged dust on the Moon sticks to and infiltrates everything, and is nearly impossible to remove. Even during the short duration of the lunar missions it caused substantial difficulty (see NASA/TM—2005-213610/REV1 “The Effects of Lunar Dust on EVA Systems During the Apollo Missions”, James Gaier, April 2007) and there is no clear engineering solution to dust intrusion, which would be enormously problematic for a close tolerance device like a piston-operated automatic rifle or a rotating bolt.
Another are the extreme thermal conditions on the Moon; about 250 °F in sunlight to to -385 °F in darkness, and there isn’t a lot of middle ground. Firearms are designed for operation at normal Earth surface temperatures and can sometimes have problems even then. The coefficient of thermal expansion (CTE) problems of taking a firearm designed for terrestrial use may well have binding problems with the high CTE. Compounding that is the fact that while on Earth most man-portable weapons are cooled by convection to the surrounding air, a rifle on the Moon would only be able to (slowly) radiate away the heat generated by the firing of a round. So, you can expect to have problems after a few shots on a close tolerance firearm like a sniper rifle.
The other thing to consider is that if your rifle is sighted in for terrestrial use it will shoot way high, both because of the significantly lower gravity and the lack of air resistance reducing the anticipated drag on the bullet. If the bullet is precisely designed for significant aerodynamic stabilization it may also be less stable in the vacuum conditions of the Moon, but this is most applicable to lightweight rounds; the heavier bullets typically used in sniper rifles are probably not as prone to this by design.
So the essential answer is that you could fire a sniper rifle on the Moon at least a few times as long as it was protected from dust and thermal extremes, but you couldn’t carry it around openly for any duration of time and expect reliable operation. Realistically, if you wanted to use a rifle on the Moon (or Mars, or another solid planet) you’d probably want to design it for that specific application, e.g. the temperature and atmosphere conditions, as well better protection from dust intrusion, a specific bullet and cartridge loading for that atmosphere and gravity, and provide some system for internal cooling, e.g. a liquid heat exchanger loop in critical areas like the chamber, piston, and along the barrel which would go to some kind of radiator or coolant tank.
You could not fire a rifle from the Moon at Earth unless the cartridge had sufficient muzzle velocity (~2400 m/s). This is about 2.0 to 2.5 times the velocity of the most powerful sniper rifles in existence, and probably more than you could hope to achieve from a single cartridge gas-fired gun. Even if you could fire a round at that speed, you wouldn’t aim directly at the Earth because it would have rotated well out of the way by the time your bullet reached that distance; you’d need to fire ahead of it and compensate for the affects of Lunar and Earth gravity on the bullet during its trajectory. This is a surprisingly difficult problem for a purely ballistic object; the Apollo missions typically performed a number of planned mid-course corrections and occasionally unplanned or modified corrections. A slight deviation early in the trajectory of the bullet from the planned course may have it miss the Earth completely.
Stranger