  The $\mu\nu$SSM solves the $\mu$ problem of the MSSM and generates correct
neutrino masses by simply using right-handed neutrinos. This mechanism implies
that only dimensionless trilinear terms, breaking R-parity, are present in the
superpotential. We present an extension of the $\mu\nu$SSM with an extra U(1)
gauge symmetry. We use the extra U(1) charges of the matter fields to forbid
the presence in the superpotential of renormalizable and non-renormalizable
baryon number violating operators, the trilinear operator producing a domain
wall problem, and bilinear operators such as the $\mu$ term and the Majorana
masses. We apply the anomaly cancellation conditions associated to the extra
U(1), to constrain the values of the U(1) charges. We find that six assignments
of the U(1) charges to the matter fields are viable, once extra matter is
introduced. In particular, three generations of vector-like color triplets and
$SU(2)_L$ doublets, as well as six Standard Model singlets are necessary.
Electroweak symmetry breaking is viable in the model, with wide regions of the
parameter space fulfilling the experimental constraints on the existence of a
new gauge boson $Z'$. Neutrinos and the extra gaugino mix with the MSSM
neutralinos, producing a generalized see-saw matrix that can reproduce the
experimental results on neutrino masses. Finally, we have estimated the
tree-level upper bound on the lightest Higgs mass, finding that it can be as
large as about 120 GeV.
