Ubuntu Remote Access
Introduction
Remote access allows you to connect to and control your Ubuntu system from a different location or device. This capability is essential for server administration, working from different locations, and managing headless systems (computers without monitors). In this guide, we'll explore various methods to establish secure remote connections to Ubuntu systems.
Remote access solutions in Ubuntu typically fall into two categories:
- Command-line access: Perfect for server management and running commands remotely
- Graphical access: Ideal when you need to interact with the Ubuntu desktop environment remotely
SSH: Secure Shell
SSH (Secure Shell) is the foundation of secure remote access on Ubuntu systems. It provides encrypted command-line access to your system.
Installing SSH Server
Ubuntu desktop doesn't come with SSH server installed by default. To install it:
sudo apt update
sudo apt install openssh-server
After installation, the SSH service should start automatically. You can verify its status with:
sudo systemctl status ssh
Output:
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-07-10 15:23:18 UTC; 5s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 4312 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 4313 (sshd)
Tasks: 1 (limit: 4621)
Memory: 1.1M
CPU: 34ms
CGroup: /system.slice/ssh.service
└─4313 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
Connecting via SSH
To connect to your Ubuntu system from another computer:
ssh username@ip_address
For example:
The first time you connect to a server, you'll see a fingerprint verification prompt:
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
ED25519 key fingerprint is SHA256:abcdefghijklmnopqrstuvwxyz123456789.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes
to continue. This adds the server to your known hosts file.
Basic SSH Configuration
SSH configuration is stored in /etc/ssh/sshd_config
. Here are some important settings you might want to change:
sudo nano /etc/ssh/sshd_config
Common configuration changes:
# Change the default SSH port (more secure)
Port 2222
# Disable root login
PermitRootLogin no
# Allow specific users only
AllowUsers username1 username2
# Disable password authentication (require key-based auth)
PasswordAuthentication no
After making changes, restart the SSH service:
sudo systemctl restart ssh
SSH Key-Based Authentication
Using SSH keys instead of passwords significantly improves security.
- Generate SSH keys on your client machine:
ssh-keygen -t ed25519 -C "[email protected]"
- Copy your public key to the server:
ssh-copy-id [email protected]
Alternatively, you can manually add the key:
# On your local machine
cat ~/.ssh/id_ed25519.pub
# Copy the output
# On the remote machine
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys
# Paste the key and save
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
- Now you can connect without a password:
SSH Tunneling
SSH tunneling allows you to securely forward network traffic through an encrypted SSH connection.
Local port forwarding (access remote service locally):
ssh -L 8080:localhost:80 [email protected]
This forwards your local port 8080 to port 80 on the remote server. You can access the remote web server by visiting http://localhost:8080
in your browser.
Remote port forwarding (expose local service to remote machine):
ssh -R 8080:localhost:3000 [email protected]
This makes your local port 3000 accessible on the remote machine at port 8080.
Remote Desktop Access
When you need graphical access to your Ubuntu desktop, several options are available.
VNC (Virtual Network Computing)
VNC allows you to share your desktop across the network.
- Install a VNC server:
sudo apt update
sudo apt install tigervnc-standalone-server
- Set a VNC password:
vncpasswd
- Create a VNC startup script:
mkdir -p ~/.vnc
nano ~/.vnc/xstartup
Add the following content:
#!/bin/sh
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
startxfce4 &
Make it executable:
chmod +x ~/.vnc/xstartup
- Start the VNC server:
vncserver -localhost no
Output:
New 'X' desktop is your_hostname:1
Starting applications specified in /home/username/.vnc/xstartup
Log file is /home/username/.vnc/your_hostname:1.log
- Connect using a VNC client like RealVNC Viewer, TigerVNC, or Remmina from another computer to
ip_address:5901
For security, it's recommended to use VNC over SSH tunnel:
# On client machine
ssh -L 5901:localhost:5901 [email protected]
Then connect your VNC client to localhost:5901
.
X2Go
X2Go is a more efficient remote desktop solution that works well even on slower connections.
- Install X2Go server:
sudo apt update
sudo apt install x2goserver x2goserver-xsession
- Install a desktop environment if needed (e.g., XFCE for lighter usage):
sudo apt install xfce4
-
On your client computer, install the X2Go client:
- For Ubuntu:
sudo apt install x2goclient
- For Windows/Mac: Download from x2go.org
- For Ubuntu:
-
Create a new session in the X2Go client:
- Host: Your server's IP address
- Login: Your username
- Session type: XFCE (or your installed desktop environment)
-
Connect to your remote desktop!
XRDP (Remote Desktop Protocol)
XRDP provides Remote Desktop Protocol support, making it easy to connect from Windows computers.
- Install XRDP:
sudo apt update
sudo apt install xrdp
- Start and enable the service:
sudo systemctl enable --now xrdp
- Check status:
sudo systemctl status xrdp
- Connect using the Remote Desktop client:
- On Windows: Use the built-in Remote Desktop Connection app
- On macOS: Install Microsoft Remote Desktop from the App Store
- On Linux: Use Remmina or another RDP client
Enter your Ubuntu machine's IP address and your login credentials when prompted.
Remote File Access
SFTP (SSH File Transfer Protocol)
SFTP is built into SSH and provides secure file transfer capabilities.
If SSH is already set up, you can use SFTP without additional configuration:
sftp [email protected]
Common SFTP commands:
pwd # Print working directory
ls # List files
cd directory # Change directory
get file # Download file
put file # Upload file
mkdir directory # Create directory
rm file # Remove file
exit # Exit SFTP
You can also use graphical SFTP clients like FileZilla by connecting to sftp://ip_address
.
Samba (Windows File Sharing)
To share files with Windows computers:
- Install Samba:
sudo apt update
sudo apt install samba
- Configure Samba:
sudo nano /etc/samba/smb.conf
Add a share at the end of the file:
[SharedFolder]
path = /home/username/shared
browseable = yes
read only = no
create mask = 0755
directory mask = 0755
- Create a Samba user and password:
sudo smbpasswd -a username
- Restart Samba:
sudo systemctl restart smbd
- Access from Windows by typing
\\ip_address
in File Explorer.
Security Considerations
Remote access inherently introduces security risks. Here are some best practices:
- Use strong passwords or key-based authentication
- Change default ports for services like SSH (22) to reduce automated attacks
- Use a firewall to limit connection attempts:
sudo ufw allow 22/tcp
sudo ufw enable
- Implement fail2ban to block repeated login attempts:
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
- Keep your system updated:
sudo apt update && sudo apt upgrade
- Use VPN for an additional layer of security when connecting to sensitive systems
Automation with SSH
Running Remote Commands
You can run commands on a remote system without logging in:
ssh [email protected] "ls -la /var/log"
Automating File Transfers
Copy files easily with SCP (Secure Copy):
# Copy local file to remote
scp file.txt [email protected]:/path/to/destination/
# Copy remote file to local
scp [email protected]:/path/to/file.txt ./
Creating SSH Config File
Create ~/.ssh/config
for easier connections:
Host myserver
HostName 192.168.1.100
User username
Port 22
IdentityFile ~/.ssh/id_ed25519
Then simply connect with:
ssh myserver
Remote Server Monitoring
Basic Monitoring Commands
Monitor your system's resources remotely:
# Check system load
ssh myserver "uptime"
# Check disk space
ssh myserver "df -h"
# Check memory usage
ssh myserver "free -m"
# Check running processes
ssh myserver "ps aux | grep nginx"
Setting Up Remote Monitoring Tools
For more comprehensive monitoring, consider installing tools like:
- Netdata - Real-time performance monitoring:
ssh myserver "bash <(curl -Ss https://my-netdata.io/kickstart.sh)"
Access via: http://server-ip:19999
- Glances - System monitoring tool:
ssh myserver "sudo apt install glances"
ssh -L 61208:localhost:61208 myserver "glances -w"
Access via: http://localhost:61208
Remote Access Troubleshooting
Common SSH Issues
Connection refused:
# Check if SSH service is running
sudo systemctl status ssh
# Verify firewall settings
sudo ufw status
# Check SSH config for errors
sudo sshd -t
Permission denied:
# Verify username and password
# Check key permissions
chmod 600 ~/.ssh/id_ed25519
chmod 700 ~/.ssh
# Check authorized_keys file
cat ~/.ssh/authorized_keys
VNC/Remote Desktop Issues
Black screen or connection issues:
# Check if display manager is running
sudo systemctl status lightdm # or gdm3
# Check VNC server logs
cat ~/.vnc/*.log
# Restart VNC server
vncserver -kill :1
vncserver
Summary
Remote access is a fundamental skill for Ubuntu system administration. In this guide, we covered:
- Setting up and securing SSH for command-line access
- Implementing key-based authentication for improved security
- Configuring VNC, X2Go, and XRDP for graphical remote access
- Transferring files securely with SFTP and Samba
- Implementing security best practices to protect your systems
- Automating tasks and monitoring servers remotely
With these tools and techniques, you can efficiently manage your Ubuntu systems from anywhere in the world.
Additional Resources and Exercises
Exercises
- Set up SSH key-based authentication and disable password login
- Configure port forwarding to access a web server running on your remote machine
- Create a backup script that runs remotely via SSH
- Set up a VNC server and connect through an SSH tunnel
- Configure fail2ban to protect your SSH server
Further Learning
- Man pages:
man ssh
,man sshd_config
,man scp
- Ubuntu documentation: The official Ubuntu documentation
- SSH Mastery book by Michael W. Lucas for in-depth SSH knowledge
- CompTIA Linux+ and LPIC-1 certifications cover remote access topics
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)