<p style='text-indent:20px;'>Applied in consumer electronics, communication, data storage system, secret sharing, authentication codes, association schemes, and strongly regular graphs, linear codes attract much interest. We consider the construction of linear codes with two or three weights. Let <inline-formula><tex-math id="M2">\begin{document}$ m_1,\ldots, m_t $\end{document}</tex-math></inline-formula> be <inline-formula><tex-math id="M3">\begin{document}$ t $\end{document}</tex-math></inline-formula> positive integers and <inline-formula><tex-math id="M4">\begin{document}$ T = \mathbb{F}_{q_1}\times \cdots \times \mathbb{F}_{q_t} $\end{document}</tex-math></inline-formula>, where <inline-formula><tex-math id="M5">\begin{document}$ q_i = p^{m_i} $\end{document}</tex-math></inline-formula> for <inline-formula><tex-math id="M6">\begin{document}$ 1\leq i\leq t $\end{document}</tex-math></inline-formula> and <inline-formula><tex-math id="M7">\begin{document}$ p $\end{document}</tex-math></inline-formula> is an odd prime. A linear code <p style='text-indent:20px;'><disp-formula> <label/> <tex-math id="FE1"> \begin{document}$ \begin{equation*} \mathcal{C}_D = \{ \mathbf{c}(\mathbf{a}): \mathbf{a} = (a_1,\ldots,a_t)\in T\}, \end{equation*} $\end{document} </tex-math></disp-formula> <p style='text-indent:20px;'>can be constructed by a defining set <inline-formula><tex-math id="M8">\begin{document}$ D $\end{document}</tex-math></inline-formula>, where <inline-formula><tex-math id="M9">\begin{document}$ D $\end{document}</tex-math></inline-formula> is a subset of <inline-formula><tex-math id="M10">\begin{document}$ T $\end{document}</tex-math></inline-formula> and <inline-formula><tex-math id="M11">\begin{document}$ \mathbf{c}(\mathbf{a}) = (\sum_{i = 1}^{t}\mathrm{Tr}_1^{m_i}(a_ix_i))_{ \mathbf{x} = (x_1,\ldots,x_t)\in D} $\end{document}</tex-math></inline-formula>. We construct linear codes with two or three weights from the following three defining sets: <p style='text-indent:20px;'>● <inline-formula><tex-math id="M12">\begin{document}$ D_0 = \{\mathbf{x}\in T\backslash \{\mathbf{0}\}: \sum_{i = 1}^t\mathrm{Tr}_1^{m_i}(x_i^2) = 0\} $\end{document}</tex-math></inline-formula>, <p style='text-indent:20px;'>● <inline-formula><tex-math id="M13">\begin{document}$ D_{SQ} = \{\mathbf{x}\in T: \sum_{i = 1}^t\mathrm{Tr}_1^{m_i}(x_i^2)\in SQ\} $\end{document}</tex-math></inline-formula>, <p style='text-indent:20px;'>● <inline-formula><tex-math id="M14">\begin{document}$ D_{NSQ} = \{\mathbf{x}\in T: \sum_{i = 1}^t\mathrm{Tr}_1^{m_i}(x_i^2)\in NSQ\} $\end{document}</tex-math></inline-formula>, <p style='text-indent:20px;'>where <inline-formula><tex-math id="M15">\begin{document}$ SQ $\end{document}</tex-math></inline-formula> is the set of all the squares in <inline-formula><tex-math id="M16">\begin{document}$ \mathbb{F}_p^* $\end{document}</tex-math></inline-formula> and <inline-formula><tex-math id="M17">\begin{document}$ NSQ $\end{document}</tex-math></inline-formula> is the set of all the nonsquares in <inline-formula><tex-math id="M18">\begin{document}$ \mathbb{F}_p^* $\end{document}</tex-math></inline-formula>. We also determine the weight distributions of these codes. The punctured codes of codes from the defining set <inline-formula><tex-math id="M19">\begin{document}$ D_0 $\end{document}</tex-math></inline-formula> contain optimal codes meeting certain bounds. This paper generalizes results of [<xref ref-type="bibr" rid="b22">22</xref>].