
\begin{DoxyItemize}
\item infos = Information about hosts plugin is in keys below
\item infos/author = Markus Raab \href{mailto:elektra@libelektra.org}{\tt elektra@libelektra.\+org}
\item infos/licence = B\+SD
\item infos/provides = storage/hosts
\item infos/needs =
\item infos/recommends = glob error network
\item infos/placements = getstorage setstorage
\item infos/status = maintained unittest nodep libc limited
\item infos/metadata = order comment/\# comment/\#/start comment/\#/space
\item infos/description = This plugin reads and writes /etc/hosts files.
\end{DoxyItemize}

The {\ttfamily /etc/hosts} file is a simple text file that associates IP addresses with hostnames, one line per IP address. The format is described in {\ttfamily hosts(5)}.

The {\ttfamily hosts} plugins transforms the information of this file to the following structure. The keys directly below {\ttfamily ipv4} or {\ttfamily ipv6} are host names of I\+Pv4 or I\+Pv6 addresses, respectively. The keys directly below these keys are aliases. The IP addresses themselves are stored as values.

Canonical hostnames are stored as key base names with their IP addresses as value.

Aliases are stored as keys directly below canonical hostnames with a read-\/only duplicate of the associated IP address as value.

Comments are stored according to the comment metadata specification (see \href{/home/jenkins/workspace/libelektra_master/doc/METADATA.ini}{\tt /doc/\+M\+E\+T\+A\+D\+A\+TA.ini} for more information).

The ordering of the hosts is stored in metakeys of type {\ttfamily order}. The value is an ascending number. Ordering of aliases is {\itshape not} preserved.

Mount the plugin\+:


\begin{DoxyCode}
sudo kdb mount --with-recommends /etc/hosts system/hosts hosts
\end{DoxyCode}


Print out all known hosts and their aliases\+:


\begin{DoxyCode}
kdb ls system/hosts
\end{DoxyCode}


Get IP address of ipv4 host \char`\"{}localhost\char`\"{}\+:


\begin{DoxyCode}
kdb get system/hosts/ipv4/localhost
\end{DoxyCode}


Check if a comment is belonging to host \char`\"{}localhost\char`\"{}\+:


\begin{DoxyCode}
kdb meta-ls system/hosts/ipv4/localhost
\end{DoxyCode}


Try to change the host \char`\"{}localhost\char`\"{}, should fail because it is not an I\+Pv4 address\+:


\begin{DoxyCode}
sudo kdb set system/hosts/ipv4/localhost ::1
\end{DoxyCode}


``` \hypertarget{autotoc_md264_autotoc_md271}{}\section{Backup-\/and-\/\+Restore\+:/tests/hosts}\label{autotoc_md264_autotoc_md271}
sudo kdb mount --with-\/recommends hosts /tests/hosts hosts\hypertarget{autotoc_md264_autotoc_md272}{}\section{Create hosts file for testing}\label{autotoc_md264_autotoc_md272}
echo \textquotesingle{}127.\+0.\+0.\+1 localhost\textquotesingle{} $>$ {\ttfamily kdb file /tests/hosts} echo \textquotesingle{}\+:\+:1 localhost\textquotesingle{} $>$$>$ {\ttfamily kdb file /tests/hosts}\hypertarget{autotoc_md264_autotoc_md273}{}\section{Check the file}\label{autotoc_md264_autotoc_md273}
cat {\ttfamily kdb file /tests/hosts} \#$>$ 127.\+0.\+0.\+1 localhost \#$>$ \+:\+:1 localhost\hypertarget{autotoc_md264_autotoc_md274}{}\section{Check if the values are read correctly}\label{autotoc_md264_autotoc_md274}
kdb get /tests/hosts/ipv4/localhost \#$>$ 127.\+0.\+0.\+1 kdb get /tests/hosts/ipv6/localhost \#$>$ \+:\+:1\hypertarget{autotoc_md264_autotoc_md275}{}\section{Should both fail with error 04200 and return 5}\label{autotoc_md264_autotoc_md275}
kdb set /tests/hosts/ipv4/localhost \+:\+:1 \hypertarget{autotoc_md264_autotoc_md276}{}\section{R\+E\+T\+:5}\label{autotoc_md264_autotoc_md276}
\hypertarget{autotoc_md264_autotoc_md277}{}\section{E\+R\+R\+O\+R\+:\+C03200}\label{autotoc_md264_autotoc_md277}
kdb set /tests/hosts/ipv6/localhost 127.\+0.\+0.\+1 \hypertarget{autotoc_md264_autotoc_md278}{}\section{R\+E\+T\+:5}\label{autotoc_md264_autotoc_md278}
\hypertarget{autotoc_md264_autotoc_md279}{}\section{E\+R\+R\+O\+R\+:\+C03200}\label{autotoc_md264_autotoc_md279}
\hypertarget{autotoc_md264_autotoc_md280}{}\section{cleanup}\label{autotoc_md264_autotoc_md280}
kdb rm -\/r /tests/hosts sudo kdb umount /tests/hosts ```\hypertarget{autotoc_md264_autotoc_md281}{}\subsection{Limitations}\label{autotoc_md264_autotoc_md281}

\begin{DoxyItemize}
\item host names are not validated, see \href{https://issues.libelektra.org/2185}{\tt https\+://issues.\+libelektra.\+org/2185}
\item duplicates, where a host names is the same as an alias name, are not rejected, see \href{https://issues.libelektra.org/3461}{\tt https\+://issues.\+libelektra.\+org/3461}
\item keys that do not confirm to the hierarchy are ignored 
\end{DoxyItemize}