You can do it by two steps:
First, lock the camera to the view. In Blender 4.1 there's a lock icon on the right side of the viewport (when in camera view). That locks the camera to the view:
In earlier versions you have to go in the view tab (right side of the viewport) and check "Lock Camera to View":
But now, if you accidentally rotate the view, the camera will rotate and move with the view. So to prevent that you have to select the camera object and lock its location aand rotation transforms by clicking on the lock icons next to the properties in the "Item" tab (right side of the viewport):
And there you go, now the camera is locked to the view and also locked in place so it won't move by accident!