TSF – Giải pháp IT toàn diện cho doanh nghiệp SMB | HCM

Install SQL server 2022 standard on Ubuntu 22.04

The guide covers adding Microsoft repositories, installing the SQL package, and setting up the SQL Server service on Linux. You will see how to configure authentication, open firewall ports, and connect using SQL Server Management Studio (SSMS). This setup ensures a stable and secure database environment optimized for performance. We also explain how to verify the installation, enable remote access, and manage SQL services. Whether you are a developer, DBA, or IT admin, this guide will help you deploy SQL Server 2022 efficiently on Ubuntu. Following these steps ensures compatibility and reliability for your production workloads. By the end of this tutorial, you will have a fully working SQL Server 2022 Standard instance running on Ubuntu 22.04.

Step 1: Install mssql-server

Download the public key, convert it from ASCII to GPG format and write it to the required location:
curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | gpg –dearmor | sudo tee /etc/apt/keyrings/microsoft-prod.gpg > /dev/null

Re-add the repo with the new key
echo “deb [arch=amd64 signed-by=/etc/apt/keyrings/microsoft-prod.gpg] https://packages.microsoft.com/ubuntu/22.04/mssql-server-2022 jammy main” | sudo tee /etc/apt/sources.list.d/mssql-server-2022.list

Install sql-server

sudo apt-get update

sudo apt-get install -y mssql-server

After installation, you can choose the options to install the SQL version
sudo /opt/mssql/bin/mssql-conf setup

Enter key 5

Administrator user for this SQL is: sa/Abc@1234 (system administrator)
Check the SQL status again
systemctl status mssql-server –no-pager

Step 2: Install the command line tool sqlcmd to connect

# 1. Create a folder to save the key (if not yet available)
sudo mkdir -p /etc/apt/keyrings

# 2. Download the new Microsoft key (copy the entire command)
curl -sSL https://packages.microsoft.com/keys/microsoft.asc \
| gpg –dearmor \
| sudo tee /etc/apt/keyrings/microsoft-prod.gpg > /dev/null

# 3. Add mssql-tools18 repo (correct for Ubuntu 22.04) (copy the whole command)
echo “deb [arch=amd64,arm64 signed-by=/etc/apt/keyrings/microsoft-prod.gpg] https://packages.microsoft.com/ubuntu/22.04/prod jammy main” \
| sudo tee /etc/apt/sources.list.d/msprod.list

# 4. Update apt
sudo apt-get update

# 5. Install SQL Server CLI tool
sudo apt-get install -y mssql-tools18 unixodbc-dev

Add sqlcmd to PATH (for quick access)
echo ‘export PATH=”$PATH:/opt/mssql-tools18/bin”‘ >> ~/.bashrc

source ~/.bashrc

Step 3: Disbale user sa, create new admin user

Why Microsoft recommends disabling SA
• SA is the default login, everyone knows the name, so if you open SQL Server to the internet or LAN, hackers only need to guess/find the password to get in.
• If you want to be safe, you should:
• Log in with SA for the first time.
• Create a new admin user (unguessable name).
• Assign sysadmin rights to that user.
• Disable SA.

Safety Procedure

#1: Login with SA:
Because ODBC Driver 18 requires TLS encryption by default when connecting, but your SQL Server is running on a local machine and using a self-signed certificate (not trusted by CA), it reports a verify failed error. Add the parameter -No to remove encryption verify or -C

sqlcmd -S localhost -U SA -P ‘Abc@1234’ -C
#2: Create a new user:
CREATE LOGIN admin_sql WITH PASSWORD = ‘Abc@1234’;
ALTER SERVER ROLE sysadmin ADD MEMBER admin_sql;
GO

#3: Disable SA:
ALTER LOGIN SA DISABLE;
GO
exit

Step 4: Turn on firewall – Remote

#1 Turn on firewall and open port
In production environments, IP restrictions should still be configured.
sudo ufw enable
sudo ufw allow 1433/tcp
sudo ufw allow openssh
sudo ufw reload

#2Check port 1433
By default SQL Server on Linux listens on 1433. You can check:
sudo ss -ltnp | grep 1433

If you see the line LISTEN 0 1433 with the process sqlservr, it’s ok.

#3Allow SQL Server to receive connections from remote
By default, SQL Server on Linux is bound on 0.0.0.0 (any IP).
To make sure:
sudo /opt/mssql/bin/mssql-conf set network.ipaddress 0.0.0.0
sudo systemctl restart mssql-server