You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Laravel Forge中以root用户登录?附配方执行用户说明

How to Log in as Root on Laravel Forge Servers

Yep, Laravel Forge disables direct root SSH access by default for security reasons—so your "Permission denied (publickey)" error makes total sense. Here are a few reliable ways to get root access, depending on what you need:

This is the safest and most straightforward method since it keeps direct root access disabled:

  • First, log into your server normally using the forge user:
    ssh forge@your-server-ip
    
  • Once logged in, switch to the root user with one of these commands:
    # Option 1: Switch to root with a full environment
    sudo su -
    # Option 2: Shortcut to get a root shell (works if Forge set up passwordless sudo for forge)
    sudo -i
    
    Forge typically configures the forge user to have passwordless sudo access, so you might not even need to enter a password here. If you do, use the server password you set up in Forge when creating the server.

Only do this if you absolutely need direct root login, as it increases security risks. Here's how:

  • Log in as the forge user first, then edit the SSH daemon config:
    sudo nano /etc/ssh/sshd_config
    
  • Find the line starting with PermitRootLogin and change it to:
    PermitRootLogin yes
    
    If you want to use SSH keys instead of a password (safer than password login), set it to PermitRootLogin prohibit-password instead.
  • Save the file (Ctrl+O, then Enter in nano) and exit (Ctrl+X).
  • Restart the SSH service to apply changes:
    sudo systemctl restart sshd
    
  • If you're using password login, set a root password:
    sudo passwd root
    
  • Now you can log in directly with ssh root@your-server-ip. Remember to revert the PermitRootLogin setting back to no once you're done to keep your server secure.

3. Run Root Commands via Forge Recipes (No Login Needed)

Since you mentioned Forge lets you choose the execution user for recipes, you don't even need to log in as root to run root-level tasks:

  • When creating or editing a recipe in Forge, look for the "Run As" dropdown menu and select root as the user.
  • Any commands in that recipe will execute with root privileges automatically, so you can handle tasks that require root access without ever logging into the server as root.

Important Note

Sticking with the first method (switching from forge to root) is always the best practice for security. Direct root SSH access is a common attack vector, so avoid enabling it unless you have a specific, temporary need.

内容的提问来源于stack exchange,提问作者Fredrik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:00:10