It’s a problem foe everyone until you learn a few techniques. First, place your camera on manual focus. Auto focus just does not work in the dark.
First way: The easiest and most reliable technique is to focus on something far away during the day, simulating focusing on infinity, and tape the focus at that spot. It is not uncommon for the camera to get jostled at night and it can become unfocused without you knowing it. Tape that peals off without leaving a residue does a good job. I use gaffer’s tape. The problem with this technique is that you are always focused at infinity.
Second way: Go to Liveview and mag live view up to 10X or more. Pan around to Find a star in the field of view and manually focus on that star until it is sharp. Take a photo and view it and mag it up to make sure everything is on focus. If not then make tiny adjustments and check again. I use this most often. It takes a bit of practice to learn to quickly find stars, but it gets easier with practice.
Third way: At your chosen focal length and f stop, shine a bright light on something relatively far away (simulating a focus at infinity), focus on that structure, take a photo and check to make sure the stars and foreground are in focus. If not in focus then make tiny adjustments and recheck.
When checking your images, make sure you go to the trouble to mag them up all the way on the camera LCD to make sure the stars are in focus. Always recheck after adjusting your focus!
Focusing when foreground objects are very close:
Focal Length: The wider the focal length of the lens, the greater the Depth of Field. When up close to a foreground object use a wider focal length lens. If really close then use the widest lens you have. I occasionally even use a fisheye lens, which have great DOF. The increased DOF can save the photo.
Hyperfocal Distance: Using a hyperfocal distance maximizes the useful Depth of Field (DOF). When foreground structures are very close, and I want the foreground and stars to all be in focus, I focus using the hyperfocal distance. You can get a number of apps on your smartphone to help to calculate this. I use an app called TrueDoF. These apps calculate depth of field at varying focal lengths and apertures. Over time you start to remember the numbers you use most often.
The idea is that there is a zone around the focal point that is all in focus. This zone extends in front of and behind the focal point. If you focus on infinity you still have an “in focus” zone in front of infinity, but the in focus zone beyond infinity has been discarded. Instead, if you focus on a point in front of infinity, you can take advantage of the in focus zones in front of and behind the focal point, and still have good focus at infinity.
For example, If you are using a 16 mm lens at an aperture of 2.8, and you focus at infinity, then objects will be in focus from about 8.8 feet (2.6 m) to infinity. However if you focus at 9 feet (2.9 m), objects will be in focus from about 4.5 feet (1.5 m) to infinity. This allows you to get much closer to foreground subjects and still have everything in focus. In the app you just enter the focal length of the lens and the aperture, and you move the slider up and down to find the optimum focal point. In this case the best focal point is around 9 feet. You then find an object about 9 feet away, and shine a light on that object, and use the light to focus on that point. Your scene will then be in focus from 4.5 feet to infinity.