Dynamo effect: The earth gets its own magnetic field lines because of the presence of the metallic fluids that are present at the outer core as well as in the inner core.
Illustration of the dynamo mechanism that creates Earth's magnetic field: convection currents of fluid metal in Earth's outer core, driven by heat flow from the inner core, organized into rolls by the Coriolis force, create circulating electric currents, which generate the magnetic field.