Introduction
As a web developer, having the right set of tools is crucial. When it comes to Ubuntu Commands for Web Servers, knowing the right commands can save you time and system resources. This article aims to provide you with the most useful Ubuntu commands for effective web server management.
Understanding sudo
The sudo
command allows you to run programs with the security privileges of another user, typically the superuser (root). For example:
sudo apt-get update
This command updates the package list on your system, a task usually reserved for the superuser.
File and Directory Operations
Navigating and manipulating the file system is a daily task for any web developer. Here are the essential commands to perform these operations.
List directory contents
ls
Change directories
cd
Copy files or directories
cp
Move or rename files and directories
mv
Remove files or directories
rm
Create empty files
touch
Create directories
mkdir
Change file permissions
chmod
Change file ownership
chown
These commands are essential for navigating and manipulating your server’s file system.
System Information
Monitoring your system’s health is crucial for effective web development. These commands will help you keep an eye on your system.
Display system information
uname
Report file system disk space usage
df
Estimate file space usage
du
Display the amount of free and used memory
free
Display dynamic real-time system status
top
Keep an eye on your system’s health with these useful commands.
Package Management
For software package management, the following commands are indispensable. They offer a way to add, remove, and manage the software on your system.
APT package handling utility
apt-get
Query the APT cache
apt-cache
Package manager for Debian
dpkg
Efficiently manage your software packages with these commands.
Network Operations
Network operations are critical for any web server. Use these commands to monitor and manage them.
Check network connectivity
ping
Network statistics
netstat
Display or configure network interfaces
ifconfig
These are your go-to commands for monitoring and managing network operations.
MySQL Operations
Database management is a key aspect of web development. These MySQL commands allow you to efficiently manage your databases.
Log into MySQL
mysql -u root -p
Create new database (schema)
create database [dbname]
Create a new MySQL user
create user '[username]'@'localhost' identified by '[password]'
Grant privileges to the new user
grant all privileges on [dbname].* to '[username]'@'localhost'
List all databases, show all databases
show databases
Select a database
use [database]
List tables in the selected database
show tables
Exit MySQL
exit
Manage your MySQL databases efficiently with these commands.
SSH Operations
Secure your server and optimize remote operations with these SSH commands.
Connect to a Server via SSH
Connect to a server via SSH, where [username] is root or your own SSH user and [hostname] is typically your server’s IP address:
ssh [username]@[hostname]
example:
ssh root@yourserverIPaddress
Create a New SSH User
Once connected with a superuser, you can create a new SSH user:
adduser [username]
Generate SSH Keys for the New User
Now, switch to the new user’s account and generate SSH keys:
su - [username]
ssh-keygen
Follow the prompts to complete the key generation. This will create a new SSH key, using the provided email as a label.
Disable the SSH Root User
For security reasons, it is recommended to disable your SSH root user:
sudo passwd -l root
Restart SSH Service
After disabling the root user, it’s a good practice to restart the SSH service:
sudo systemctl restart sshd
UFW Cheat Sheet = Firewall Operations
UFW is an abbreviation for Uncomplicated Firewall.
The below commands are not necessarily based on a step by step guide.
Check the Firewall Status: To see the current rules, you can use:
sudo ufw status
Enable UFW
sudo ufw enable
This will list all the rules that are currently active.
Allow SSH Connections
sudo ufw allow ssh
Deny All Incoming Connections by Default
sudo ufw default deny incoming
Allow All Outgoing Connections by Default
sudo ufw default allow outgoing
Allowing Specific IPs: If you want to only allow SSH traffic from a specific IP address, you can do:
sudo ufw allow from YOUR_IP_ADDRESS to any port 22
Delete the general OpenSSH rule: You’ll first need to identify the rule number. To do this, run:
sudo ufw status numbered
Delete the Rule: Use the rule number to delete it.
sudo ufw delete [rule_number]
Deny All Other SSH: To deny all other SSH traffic, you can use:
sudo ufw deny 22
Enable/Disable Rules: Any changes you make won’t be active until you enable them:
sudo ufw enable
If you ever need to disable the firewall for troubleshooting, you can do:
sudo ufw disable
Reload UFW: For the changes to take effect.
sudo ufw reload
Please make sure you are very careful when altering firewall rules, especially over SSH, as it’s possible to lock yourself out of your own server if not done correctly.
Systemctl Operations
Start a service
systemctl start [service]
Stop a service
systemctl stop [service]
Restart a service
systemctl restart [service]
Check the status of a service
systemctl status [service]
Manage system services and units effectively with these systemctl
commands.
Conclusion
Having this cheat sheet of Ubuntu commands can significantly ease your web server management tasks. For more tips like this, feel free to follow me on Twitter.
Something missing?
If you think I missed something or looking for something specific, feel free to comment on the article and let me know.