
# 如何使用非root用户安装OpsAgent？
#### 问题描述
安装OpsAgent是纳管主机的必要操作，一般使用root用户进行安装，如果想使用普通用户账号安装，需要将OpsAgent的所有权从root转交给该用户，同时该用户需要申请sudo免密权限。
#### 操作步骤
1. 使用root用户远程登录已安装OpsAgent的主机，并执行如下命令新增普通用户，请将命令中gandalf修改为实际用户名。如已新增普通用户此步骤可跳过。 
   ```
   useradd -m gandalf
   usermod -G wheel gandalf 
   passwd gandalf
   ```
   
   
2. 执行如下命令为普通用户添加sudo免密权限。 
   ```
   echo "gandalf ALL=(root) NOPASSWD: ALL" >> /etc/sudoers
   ```
   
   
3. 安装OpsAgent，具体操作请参见[为Linux主机安装OpsAgent](https://support.huaweicloud.com/usermanual-wiseeye/appstage_04_0327.html#section3)。
4. 转移OpsAgent的所有权。 
   使用root用户远程登录已安装OpsAgent的主机，并执行如下命令，将OpsAgent权限转移给新增的普通用户。
   ```
   username="gandalf" && \
   sudo systemctl stop opsagent && sleep 3 && \
   sudo chown -R "$username":"$username" /usr/local/opsagent && \
   sudo chown -R "$username":"$username" /etc/opsagent && \
   if ! sudo grep -q "^Group=$username$" /usr/lib/systemd/system/opsagent.service; then \
     sudo sed -i "/$Service$/a Group=$username" /usr/lib/systemd/system/opsagent.service; \
   fi && \
   if ! sudo grep -q "^User=$username$" /usr/lib/systemd/system/opsagent.service; then \
     sudo sed -i "/$Service$/a User=$username" /usr/lib/systemd/system/opsagent.service; \
   fi && \
   sudo systemctl daemon-reload && sleep 2 && \
   sudo systemctl restart opsagent && sleep 2 && \
   ps -ef | grep opsagent
   ```
   
   
