Under the data retention law, companies will be required to keep data on the timing and duration of telephone calls, as well as online traffic through IP addresses. Location data from mobile phones may only be stored for four weeks.
Telecom and Internet companies will be forbidden from storing the content of communications, while email traffic is excluded from the new law. Data retention centres will also be located in Germany, Justice Minister Heiko Maas told parliament.
Privacy is a particularly sensitive issue in Germany because of the surveillance by the Gestapo in the Nazi era and by communist East Germany's Stasi secret police.
Disclosures by former NSA contractor Edward Snowden about widespread espionage in Germany by the United States caused outrage in Germany.
The debate over the new law set Chancellor Angela Merkel's conservatives (CDU) at loggerheads with her Social Democrat (SPD) junior coalition partner, while opposition parties and critics argued the law violated human rights and would put millions of citizens under general suspicion.
Last year, the European Union's highest court overthrew a rule that required telecoms companies to store the communications data of EU citizens for up to two years on the grounds it infringed human rights.
In 2010, Germany's Constitutional Court blocked a law to store all data for six months.
Maas saw the new legislation as a compromise that would give police an "additional tool" to help fight the most serious crimes.
"It is proportionate because less data will be stored, we will save data for a much shorter period and because access to the data has been made significantly harder," Mass said.
© Thomson Reuters 2015
For the latest tech news and reviews, follow Gadgets 360 on X, Facebook, WhatsApp, Threads and Google News. For the latest videos on gadgets and tech, subscribe to our YouTube channel. If you want to know everything about top influencers, follow our in-house Who'sThat360 on Instagram and YouTube.