TITLE: Milnor's exercise: for any manifold $M$, $\mathrm{Hom}(C^\infty(M,\mathbb{R}),\mathbb{R})\cong M$
QUESTION [10 upvotes]: I am studying some chapters of Kolar-Michor-Slovak, Natural Operations in Differential Geometry, in particular the one on Weil bundles, where they present the famous "Milnor's exercise". They give two similar proofs, one much shorter than the other, supposedly because of omitted details. I would like to understand it better.
So we have to prove the:

Theorem For any manifold $M$, $\mathrm{Hom}(C^\infty(M,\mathbb{R}),\mathbb{R})\cong M$

Where $\mathrm{Hom}$ refers to morphisms of commutative unital algebras and $\cong$ to bijection of sets (I suppose).
Now the short proof goes like this:
Consider an homomorphism of algebras $C^\infty(M,\mathbb{R}\xrightarrow{\phi}\mathbb{R})$, by the usual short exact sequence argument, $\ker(\phi)\leq C^\infty(M,\mathbb{R})$ is an ideal of codimension $1$ (notice that such an homomorphism cannot be the zero map, since $\phi(1_\mathbb{R})=1\in\mathbb{R}$).
Now, consider the following system of subsets of $M$, where $V(f)=f^{-1}(0)$:
$$\{V(f)\mid (M\xrightarrow{f}\mathbb{R})\in \ker(\phi)\}$$
this is clearly a filter of closed sets of $M$, since $V(f^2+g^2)=V(f)\cap V(g)$.
Find a function $M\xrightarrow{f^*}\mathbb{R}\in \ker(\phi)$ which is unbounded on each non-compact closed subset of $M$ (for instance a positive proper function, such as the square of the geodesic distance w.r.t. any Riemannian metric).  Then $V(f^*)$ is a compact set contained in  $\bigcap_{f\in \ker(\phi)} V(f)$, which is then nonempty.
Let $x_0$ be one of its elements.
Then for any $f\in C^\infty(M,\mathbb{R})$ $f-\phi(f)1$ is in $\ker(\phi)$ so $(f-\phi(f)1)(x_0)=0$, and $\phi(f)=f(x_0)$.
So $x_0$ completely determines $\phi$ and the map $\phi\mapsto x_0$ is the desired bijection.
I have fetched the italicised details. What I am having trouble understanding is the bold passage. Moreover I don't see where we use the fact that the given system of sets is a filter and that $\ker(\phi)$ has codimension 1: I suspect these are relevant for the bold passage also.
Any help would be much appreciated.

REPLY [2 votes]: This is a proof of the "hard part" mentioned in  @Elad's answer.
Lemma.  A smooth manifold $M$ admits a smooth real valued function $f$ such that
$$
  \lim_{x\to \infty }f(x) = \infty ,
  $$
(in the sense that for every $a>0$, there exists a compact subset $K\subseteq M$, such that $f(x)>a$, for every $x\in M\setminus
K$)
if and only if $M$ is $\sigma $-compact.
Proof. For the  "only if" part notice that,  given $f$ as above, one has that $f^{-1}((-\infty,a])$ is compact for every real
number $a$, and
$$
  M= \bigcup_{n\in \mathbb N}f^{-1}((-\infty,n]).
  $$
Conversely, let $\{K_n\}_{n\in \mathbb N}$ be a family of compact subsets of $M$  such that   $M= \bigcup_{n\in \mathbb N}K_n$.  By a
well known trick, based on the local compactness of $M$, we may assume WLOG that each $K_n$ is contained in the interior
of $K_{n+1}$.
By Lemma (1.3.2) in https://www.math.ucla.edu/~petersen/manifolds.pdf (Smooth Urysohn Lemma), for each $n$ in $\mathbb
N$ we may choose a nonegative smooth function $f_n$ vanishing on $K_n$ and taking on the value 1 on $M\setminus \text{int}(K_{n+1})$ (and
hence also on $M\setminus K_{n+1}$).
Defining
$$
  f(x) = \sum_{n=1}^\infty  f_n(x), \quad \forall x\in  M,
  $$
we claim that, for every $x$ in $M$,  there exists   a neighborhood $V$ of $x$,  and a natural number $m$, such that
$f_n=0$ on $V$, for every $n\geq m$.
To see this, find some $i$ such that $x\in  K_i$.   It then suffices to take
$V=\text{int}(K_{i+1})$ and $m=i+1$.
This said, it is clear that the  above function $f$ is well defined and smooth.
To see that  $\lim_{x\to \infty }f(x) = \infty $, observe that if $x\not\in  K_{m+1}$, then  surely $x\not\in K_{n+1}$, for  every $n\leq m$,  whence
$$
  f(x) \geq   \sum_{n=1}^m f_n(x) = m.
  $$
This completes the proof of the Lemma.  QED

Back to the question, let us assume that  $M$ is $\sigma $-compact, so we may pick  $f$  as in the
Lemma.
Setting
$$
  f^*=f-\phi(f)1,
  $$
one has  that $f^*\in \text{Ker}(\phi)$, and clearly also   $\lim_{x\to \infty }f^*(x) = \infty $, so we have that
$$
  V(f^*) =   {f^*}^{-1}(0)
  $$
is compact, as desired.