Setting Up .NET on Ubuntu Server
This guide walks you through installing .NET on Ubuntu Server, configuring your development environment, and implementing security best practices.
1. Prerequisites
Before installing .NET, ensure your system is up to date:
sudo apt update
sudo apt upgrade -y2. Installing .NET SDK
Add Microsoft package repository
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.debInstall .NET SDK
sudo apt update
sudo apt install -y apt-transport-https
sudo apt install -y dotnet-sdk-9.0 # Change version as neededVerify installation
dotnet --version3. Development Environment Setup
Install essential tools
sudo apt install -y gitConfigure VS Code Remote Development (Optional)
If developing remotely:
- Install VS Code on your local machine
- Install the "Remote Development" extension pack
- Connect to your Ubuntu server via SSH
- Install C# extension in the remote VS Code instance
Creating your first .NET project
mkdir ~/dotnet-projects
cd ~/dotnet-projects
dotnet new console -n MyFirstApp
cd MyFirstApp
dotnet run4. Installing .NET for Production Environments
When deploying to production servers, you should install only the .NET Runtime instead of the full SDK. This reduces the attack surface and resource usage.
Understanding Runtime vs SDK
- SDK: Includes everything needed for development (compiler, tools, libraries)
- Runtime: Contains only components needed to run applications
Add Microsoft package repository (if not already added)
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.debInstall .NET Runtime
For console or worker applications:
sudo apt update
sudo apt install -y apt-transport-https
sudo apt install -y dotnet-runtime-9.0 # Change version as neededFor web applications (ASP.NET Core):
sudo apt update
sudo apt install -y apt-transport-https
sudo apt install -y aspnetcore-runtime-9.0 # Change version as neededVerify installation
dotnet --list-runtimesRunning applications in production
cd /path/to/application
dotnet YourApplication.dllFor production deployment, consider using a service manager like systemd:
sudo nano /etc/systemd/system/your-app.serviceExample service file:
[Unit]
Description=.NET Web Application
After=network.target
[Service]
WorkingDirectory=/path/to/application
ExecStart=/usr/bin/dotnet /path/to/application/YourApplication.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=your-app
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.targetEnable and start the service:
sudo systemctl enable your-app.service
sudo systemctl start your-app.service
sudo systemctl status your-app.service5. Security Best Practices
Secure your application
Keep .NET updated:
bashsudo apt update sudo apt upgradeUse HTTPS in production:
- Always configure SSL/TLS certificates
- Use Let's Encrypt for free certificates
Implement proper authentication:
- Use ASP.NET Core Identity for user management
- Consider OAuth/OpenID Connect for enterprise applications
Data protection:
- Use Entity Framework parameterized queries to prevent SQL injection
- Implement proper input validation
- Use Data Protection API for sensitive information
Secure Docker containers (if using containerization):
bash# Run containers with limited privileges docker run --security-opt=no-new-privileges my-dotnet-appNetwork security:
- Configure firewall rules with UFW
bashsudo ufw allow ssh sudo ufw allow 443/tcp # HTTPS sudo ufw enable
Secret management
Use user secrets during development:
bashdotnet user-secrets init dotnet user-secrets set "ConnectionStrings:DefaultConnection" "your-connection-string"In production, use environment variables or a vault solution:
- Azure Key Vault
- HashiCorp Vault
- Docker secrets
5. Development Guidelines
Code organization
- Follow the Model-View-Controller (MVC) pattern for web applications
- Use Clean Architecture principles
- Organize code into small, focused classes with single responsibilities
Testing
# Create a test project
dotnet new xunit -n MyApp.Tests
# Add a reference to your main project
dotnet add MyApp.Tests/MyApp.Tests.csproj reference MyApp/MyApp.csprojCI/CD setup
- Consider using GitHub Actions or Azure DevOps for CI/CD pipelines
- Automate testing, building, and deployment processes
Performance optimization
- Use async/await for I/O operations
- Implement caching where appropriate
- Profile your application to identify bottlenecks
6. Additional Resources
7. Troubleshooting
Common issues and solutions:
- SSL certificate issues: Install
libnss3-toolspackage - Database connection problems: Check firewall settings and connection strings
- Permission issues: Review file ownership and permissions in your project directory