I have tried this solution in a VM
Update for Ubuntu 24.04
Ubuntu 24.04 has a new Remote Login feature under Settings > System > Remote Access, along with the old Desktop Sharing.
Remote Login allows remotely logging into the Ubuntu 24.04 using RDP protocol if no one is logged in locally. For example, if the computer is restarted and automatic login is disabled, you can use this feature to login remotely.
Unfortunately if you get disconnected or close the RDP session, the remote session will be lost, in my limited testing. When you try to connect again you will get the login screen and login to a new session, rather than the existing remote session you disconnected from.
Original Answer for Ubuntu 22.04
- Disable the built-in remote desktop feature and install some other RDP/VNC server and manually configure it, but would that conflict with
the built-in one? Not sure.
The solution I propose here is to not to use the native desktop sharing and use xrdp server instead.
Install xrdp
The setting above did not work in my fresh install of Ubuntu 22.04 until I installed xrdp. You have to either physically access the headless computer or use ssh for this step.
Open a terminal by Ctrl+Alt+T and enter:
sudo apt install xrdp
You will be prompted to enter your password. As you enter your password the cursor will not move. This is normal.
If you have ufw (Uncomplicated Firewall) enabled you will have to open the port 3389 with this command:
sudo ufw allow 3389.
Verify that the xrdp server is up and running with this command:
sudo systemctl status xrdp
Remember to logout
Logging out (locally) is the most important part. If you login by physically going to the computer and connecting keyboard, mouse, monitor etc. then xrdp won't work until you logout.
A Note about xrdp local and remote login
Do not use Automatic login to the desktop when the computer starts!
Unlike RDP in Windows xrdp only works if you are not logged in to the remote computer locally. This means you physically go to the remote computer and login to it, and then leave the computer without logging out, xrdp will not work.
Similarly, while remotely logged in via xrdp you may disconnect without logging out and the session will continue. However, in this case, you won't be able to login locally until you log out remotely.
This is not a problem for a headless computer as you never login locally.
Two points for the XRDP/RDP Client
1. Session, User and Password
When you try to access the remote computer using (X)/RDP, you may see this dialog box on your RDP/XRDP client:

- Set Session to XOrg, the default.
- Use your remote Ubuntu Computer's username
- Use the remote Ubuntu computer user's login password. Do not use the randomly generated password when you set up the (x)RDP client.
You won't see this dialog if you use Remmina and choose to store your username and password with the connection configuration.
2. Desktop may look different
When I used Remmina to xrdp into the Ubuntu22.04 in the VM, I didn't get the default Ubuntu desktop. Instead I got the Gnome 42 desktop that looks like this:

There may be some way to change it back to the Ubuntu desktop, but I didn't explore that.
To change the appearance of the desktop from the default gnome to Ubuntu, create the hidden file /home/$USER/.xsessionrc with the following content:
export GNOME_SHELL_SESSION_MODE=ubuntu
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
export XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
Source: How to login to the Ubuntu (not GNOME) session on a remote server using xRDP?
Thanks to Torres and AgentRev for bringing this solution to my notice.
hope this helps