I still remember the first time I rented a VPS.
I logged in as root, typed a few commands I found in a tutorial, then suddenly…
sudo: command not found and later user is not in the sudoers file.
I thought I broke the server.
If that sounds familiar, this article is for you.
This is a practical, step-by-step walkthrough of an initial Ubuntu server setup flow based on that classic “Initial Server Setup with Ubuntu 12.04” style tutorial, plus the real problems people hit in the comments:
- You created a user… but
sudois “not allowed” visudosaysPermission denied- New user login gives
connection refused
I’ll walk you through what’s going on and what to check, slowly and safely.
Who This Is For (And What We’re Fixing)
This guide is for you if:
- You’re a new VPS user (maybe your first server)
- You logged in as root, made a new user, and tried to give it sudo
- But when you switch to that user and run
sudo, you get errors like: not allowed to run sudouser is not in the sudoers filevisudo: /etc/sudoers: Permission denied- Or your SSH login as the new user says
connection refused
We’ll go over the usual flow of that initial Ubuntu setup:
- Log in as root
- Change root password
- Create a new user
- Give that user root/sudo privileges safely using
visudo - Test the new user connection and fix common errors
I’ll focus on the logic and troubleshooting, not fancy extras.
Step 1 – Log In as Root and Change the Root Password
Most basic VPS setups still drop you in as root the first time.
If you’re new, remember: root can do anything.
A typo as root can delete your whole system.
So we want to use root only for initial setup, then switch to a normal user with sudo.
Typical first login is something like:
ssh root@your_server_ip
Once you’re in, change the root password to something strong and unique:
passwd
Why this matters: If someone guesses or steals your root password, your server is gone.
This is the first simple layer of safety.
Step 2 – Create a New User Account
After securing root, the usual tutorial flow is to create a non-root user.
This user will be your daily driver.
From the root shell:
adduser newuser
You’ll be asked for a password and some optional info (you can skip most of it).
Safety tip:
– Use a different password from root
– Don’t reuse your email or social media passwords
Once created, you should be able to switch to that user with:
su - newuser
If that works, the account exists and can log in locally.
We still need to give it sudo powers.
Step 3 – Give the New User Sudo Access via visudo
This is where many people get stuck.
You follow a tutorial, add your user to sudoers, then… sudo still doesn’t work.
The correct and safe way to edit the sudo configuration is:
visudo
Why visudo?
From the tutorial’s own explanation in the source: the visudo command:
- Locks the sudoers file against multiple edits
- Does basic sanity checks
- Checks for parse errors
In plain language: it helps prevent you from breaking sudo completely.
When visudo opens /etc/sudoers, the common pattern tutorials suggest is to add something like this line (example format):
newuser ALL=(ALL:ALL) ALL
The exact syntax may vary by tutorial, but the idea is:
newuser– your usernameALL=(ALL:ALL) ALL– can run all commands as any user using sudo
Save and exit.
Important: make sure you did this step as root, not as the new user (yet).
Step 4 – Test Sudo as the New User (and Fix “Not Allowed” Errors)
Now log out from root and log in as the new user.
Either via SSH:
ssh newuser@your_server_ip
or locally from a root shell:
su - newuser
Then test sudo with something harmless:
sudo whoami
If everything is correct, you should see:
root
But if you’re here, you probably see something like:
newuser is not allowed to run sudo on this server- or you can’t use
sudoat all
Based on the problems mentioned in the original comments, here are the usual causes and how to think about them:
- The user wasn’t actually added to sudoers
-
Double-check: did you really save the change in
visudoas root? -
The sudoers file edit failed sanity checks
-
If
visudodetected an error, it may have refused to save changes. -
You’re trying to run
visudoas the new user before it has sudo - This leads to
Permission deniederrors (we’ll handle this next).
At this point, if sudo does not work for the new user, go back to root and re-check your sudoers file.
Step 5 – Fixing “visudo: /etc/sudoers: Permission denied”
One specific comment from the source was:
hi, after login as root and update visudo for a newuser, i logout root, login with newuser do visudo command again but visudo say: /etc/sudoers: Permission denied any idea what newuser doesn’t have the root permision??
There’s a key detail hidden there:
visudoedits/etc/sudoers/etc/sudoersis owned by root- Only root (or a user allowed to use
sudo) can edit it
So if you:
- Edit sudoers as root
- Log out
- Log in as the new user
- Try to run
visudowithout sudo
You’ll get:
/etc/sudoers: Permission denied
That does not automatically mean the user doesn’t have sudo.
It just means you tried to edit a root-owned file as a regular user.
To test properly:
- Log in as
newuser - Run:
bash
sudo whoami
- If sudo is working, then:
bash
sudo visudo
If sudo itself fails (before visudo), then yes, the user does not have sudo privileges yet.
In that case, follow this recovery path:
-
SSH back in as root
bash
ssh root@your_server_ip -
Run
visudoagain as root
bash
visudo -
Carefully verify the line you added for
newuseris present and correctly formatted -
Save and exit, then re-test with
newuser
Key idea: you never run plain visudo as a normal user.
You either:
- run
visudoas root, or - run
sudo visudoas a user that already has sudo.
Step 6 – Handling “After Setting Root Privileges I Still Can’t Run sudo”
Another complaint in the source was basically:
After setting root privileges to my user I still can’t run sudo (not allowed)
When that happens, here’s a simple mental checklist you can walk through.
You don’t need deep Linux knowledge, just go step by step.
-
Confirm the user exists
As root:
bash
id newuser
If it saysno such user, youradduserstep failed. -
Confirm your sudoers change is really saved
As root, open via:
bash
visudo
Scroll and check: is there actually a line fornewusergiving permissions? -
Make sure you didn’t create a typo in the username
newuservsnewUservsnew_user—sudoersis strict. -
Test a basic sudo command as the user
bash
sudo -l
This lists allowed commands. If even this fails, sudo isn’t wired up yet.
If all that matches what your tutorial told you to do but it still doesn’t work, go back to root and carefully reapply the instructions exactly.
Step 7 – Fixing “Connection Refused” When Logging in as the New User
Another user in the source said this:
After following the steps, I’m in the final step of checking if I can connect with the newly created user but I’m getting an error saying “connection refused”.
Important detail: connection refused usually means SSH itself is not accepting the connection at all, not a wrong password.
In the context of following a basic initial setup tutorial, the most common simple explanations are:
- You’re trying to connect with the wrong username or port, and the SSH service isn’t listening where you expect
- The SSH service is not reachable from your network (firewall, network issue)
But staying strictly within the source context, here’s how to reason about it at a beginner level:
-
Check that logging in as root still works
Ifssh root@your_server_ipalso getsconnection refused, then the problem is not the new user; it’s the SSH connection itself. -
If root login works but newuser fails
- You might actually be getting a different error (like
Permission denied (publickey,password)), notconnection refused -
Double-check the actual error text; tiny differences matter
-
Confirm the username
Make sure you are using:
bash
ssh newuser@your_server_ip
and not still trying withroot@.
If you get true connection refused only intermittently, it’s often network or provider-side.
If it happens only for the new user, re-check the exact error message carefully.
Step 8 – Extra Note: Swap Space Was Mentioned, But Stay Focused
One comment in the source said this:
You should also mention the steps to add swap storage, as explained else on your site: https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04
That’s a good idea in general; swap can help small VPSes.
But adding swap is a separate tutorial.
For this article, we stay focused on the initial root/new-user/sudo setup and the common permission problems.
Once your user and sudo are working correctly, then you can follow a dedicated swap setup guide on your provider’s docs.
Wrap-Up: What You Should Have Working Now
By now, if you slowly followed the logic, you should be able to:
- Log in as root when needed
- Use
passwdto change the root password - Create a new user with
adduser - Safely edit
/etc/sudoersusingvisudoas root - Log in as the new user and successfully run:
bash
sudo whoami - Understand why
visudocan sayPermission deniedwhen run without sudo - Have a mental checklist for when sudo or SSH “just doesn’t work”
You don’t need to know everything about Linux to manage a small VPS.
You just need a safe process and a bit of patience.
If this saved you time, bookmark CrushEdge for more fixes.
No Comments