Using WinSCP is one of the most popular ways to transfer files to a remote Linux server. In this post, I will explain how to connect to an Ubuntu server as a root using WinSCP. First, it is important to note that the root user is not directly accessible with a login operation in Ubuntu and other Linux distributions. Using an unprivileged user as an intermediate login is the first step.
Background
Windows customers can download WinSCP for free and use it as an SFTP client, FTP client, WebDAV client, S3 client, or SCP client. Its primary function is to transfer files between local and remote computers. Additionally, WinSCP supports scripting and basic file management.
If you are using WinSCP to access the root folder, create a new folder (directory), or simply transfer files, you might see one of the following errors if you don’t access it using the root account.
This error happens due to the current user doesn’t have the root rights and permissions will be denied.
Keep in mind, that for accounts such as root, the direct login is typically disabled by default for security reasons. So when enabling it, have security in mind. And also, you can’t change to a root account in WinSCP when you are already logged into a session, as you can do in the Linux shell using su.
Note: This solution also works for AWS EC2 instances.
Connect to an Ubuntu server as a root using WinSCP
Before continuing, we need to make sure about some obvious things in our Ubuntu server:
- You have installed the SSH server.
- You have created a root account.
Option 1
This is the most common option that you’ll see around…
@Your Ubuntu Server
- Log into your Ubuntu server, and using the terminal (Shell), become root via su or sudo.
- Run the command visudo to update the /etc/sudoers.tmp file. Then, configure this file to allow other users to become root without a password. Add the following line:
user ALL = (ALL) NOPASSWD: ALL
- Replace “user” with the user you want to grant access to the root account. Your regular unprivileged username.
@Local Machine WinSCP
- In Win SCP, open a new session. Change the File Protocol option from SFTP (default) to SCP. Input your server credentials, host name, user name, and password. Then hit Advanced…
- In advanced. Go to Environment / SCP/Shell. Under Shell, type:
sudo su -
- Leave the rest as default. Click OK.
- On the main login window, click Login.
- You should be able to access the root folder now. Or copy and create directories.
Option 2
With option 1, you might get another error:
Error skipping startup message. Your shell is probably incompatible with the application (BASH is recommended)
This could be your case, or you just can’t manage to get option 1 to provide root access to your server. In any case, try this option 2.
@Your Ubuntu Serve
- Run the command visudo again. This time, add the following line at the end of the file:
ALL=NOPASSWD: /usr/lib/openssh/sftp-server
- Replace “user” with the user you want to grant access to the root account. Your regular unprivileged username.
@Local Machine WinSCP
- In WinSCP, open a new session. In File Protocol, leave SFTP as default. Input your server credentials, host name, user name, and password. Click Advanced…
- In advanced. Go to Environment / SFTP. Under Shell, type:
sudo /usr/lib/openssh/sftp-server
- Leave the rest as default. Click OK.
- On the main login window, click Login.
- This time, you might not able to log in automatically, but after inputting the root account password, you’d be good to go.
- Now, you should be able to access the root folder. Or copy and create directories.
There are many workarounds to connect to an Ubuntu server as a root, using WinSCP. Here I am sharing two general options based on my experience, but consider that these configurations can vary depending on your scenario, Linux version, and more options.
Note: Take a look at a similar issue using VMware and WinSCP. Unable to Access the VCSA 6.7 via WinSCP
Potentially dumb question regarding #2, if I put a user (for example testuser ALL=NOPASSWD: /usr/lib/openssh/sftp-server) can I still log in with that account normally via SSH and do TTY password sudo with the testuser, or is this account now locked down to only SFTP?