Introduction to GNU Screen
GNU Screen is a terminal multiplexer that allows you to manage multiple shell sessions from a single SSH connection. It's particularly useful for running long-running processes like MicroK8s dashboard proxies, as sessions persist even when you disconnect from the server.
Key Benefits:
- Session Persistence: Processes continue running after SSH disconnection
- Multi-window Management: Run multiple sessions in a single terminal
- Session Sharing: Multiple users can attach to the same session
- Process Recovery: Reconnect to running processes after network interruption
MicroK8s Dashboard Use Case
When exposing the MicroK8s dashboard via kubectl proxy, the process must remain active. Using Screen ensures the proxy continues running in the background, allowing you to disconnect from SSH while maintaining dashboard accessibility.
Create screen session
1screen -S dashboard-proxyThis command creates a new named screen session called "dashboard-proxy". The -S flag specifies the session name, making it easier to identify and manage multiple sessions.
Common session naming conventions:
- Use descriptive names:
dashboard-proxy,k8s-monitor,log-watcher - Avoid spaces in session names
- Use hyphens or underscores for multi-word names
After creating the session, you can start your MicroK8s dashboard proxy:
1microk8s kubectl proxy --address='0.0.0.0' --accept-hosts='.*'This proxy command makes the Kubernetes dashboard accessible from external networks by binding to all interfaces and accepting all host headers.
Detach from screen session
1# Press Ctrl+A and then press DDetach Sequence Breakdown:
- Hold
Ctrland pressA(this is Screen's command prefix) - Release both keys
- Press
Dto detach
The session will continue running in the background. You'll see a message like: [detached from 12345.dashboard-proxy]
Alternative detach method:
1# Detach from outside the session2screen -d dashboard-proxyThis command detaches the session programmatically, useful in scripts or when the session is attached elsewhere.
List all screen sessions
1screen -lsThis displays all active screen sessions with their IDs and states (Attached/Detached). Example output:
There is a screen on:
12345.dashboard-proxy (Detached)
1 Socket in /run/screen/S-username.
Attach to screen session
1screen -r dashboard-proxyThe -r flag stands for "reattach". This reconnects you to the specified screen session, allowing you to view and interact with the running process.
If the session is already attached elsewhere:
1# Force reattach and detach other connections2screen -dr dashboard-proxyThe -d flag detaches any other attached terminals first, then reattaches to your current terminal. This is useful when you have a stuck SSH connection.
Attach to session by ID:
1# When you know the session ID (e.g., 12345)2screen -r 12345View session without attaching
1# Multi-display mode - view session read-only2screen -x dashboard-proxyThe -x flag allows multiple terminals to attach to the same session simultaneously. All users see the same output in real-time, useful for collaborative debugging or monitoring.
Delete screen session
1screen -X -S dashboard-proxy quitThis command terminates the screen session completely, killing all processes running within it.
Command breakdown:
-X: Send a command to the screen session-S dashboard-proxy: Specify the session namequit: The command to terminate the session
Alternative termination methods:
1# From inside the session: type 'exit' or press Ctrl+D2exit3
4# Kill all screen sessions at once (use with caution)5killall screen6
7# Kill specific session by ID8screen -S 12345 -X quitWarning: Terminating a screen session will also stop the MicroK8s proxy running inside it, making the dashboard inaccessible.
Advanced Screen Commands
Screen Window Management
1# Create a new window within current session (Ctrl+A, then C)2# Navigate between windows (Ctrl+A, then N for next)3# Navigate to previous window (Ctrl+A, then P)4# List all windows (Ctrl+A, then ")5# Rename current window (Ctrl+A, then A)Screen Logging
1# Enable logging for current window2# Press Ctrl+A, then H3# This creates a screenlog.0 file in the current directoryThis is invaluable for capturing output from long-running processes like kubectl proxy logs.
Screen Configuration
Create a .screenrc file in your home directory for persistent settings:
1# Example ~/.screenrc configuration2cat > ~/.screenrc << 'EOF'3# Disable startup message4startup_message off5
6# Increase scrollback buffer7defscrollback 100008
9# Show window list at the bottom10hardstatus alwayslastline11hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'12
13# Enable mouse scrolling14termcapinfo xterm* ti@:te@15
16# Auto-detach on hangup17autodetach on18EOFComplete MicroK8s Dashboard Workflow
Step-by-step deployment with Screen:
1# 1. Install Screen if not present2sudo apt update && sudo apt install screen -y3
4# 2. Verify MicroK8s is running5microk8s status6
7# 3. Enable the dashboard addon8microk8s enable dashboard9
10# 4. Create a new screen session11screen -S dashboard-proxy12
13# 5. Start the proxy (this runs in the foreground)14microk8s kubectl proxy --address='0.0.0.0' --accept-hosts='.*' --port=800115
16# 6. Detach from screen (Ctrl+A, then D)17
18# 7. Verify session is running19screen -ls20
21# 8. Access dashboard via browser22# http://your-server-ip:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/Getting Dashboard Token
1# Create a service account token for dashboard access2microk8s kubectl create token default -n kube-system3
4# Or get existing token (pre-1.24)5microk8s kubectl -n kube-system describe secret $(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)Troubleshooting
Session not found
If screen -r dashboard-proxy returns "There is no screen to be resumed":
- Check if the session exists:
screen -ls - Verify the session name spelling
- Ensure you're logged in as the correct user (screen sessions are user-specific)
Screen session appears dead
1# Clean up dead sessions2screen -wipe3
4# List sessions again5screen -lsPort already in use
1# Check if proxy is already running2sudo netstat -tulpn | grep 80013
4# Or with ss command5ss -tulpn | grep 80016
7# Kill the process using the port8sudo kill -9 <PID>Security Considerations
Warning: The command microk8s kubectl proxy --address='0.0.0.0' --accept-hosts='.*' exposes your Kubernetes dashboard to all network interfaces without authentication beyond the token.
Production recommendations:
- Use SSH tunneling instead:
bash1ssh -L 8001:localhost:8001 user@server2microk8s kubectl proxy
- Implement firewall rules to restrict access
- Use kubectl port-forward for specific services
- Consider using a proper ingress controller with TLS
Additional Resources
- GNU Screen Manual: https://www.gnu.org/software/screen/manual/
- MicroK8s Documentation: https://microk8s.io/docs
- Kubernetes Dashboard: https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/