My personal quicklist of Linux server commands.
Just FYI, these commands are mostly for Apache/LiteSpeed servers on CentOS. I don’t do as much stuff with NGINX and Ubuntu. Will add more over time.
hostnamectl– see operating system and version, reference link
hostname– see hostname
hostname server.domain.com– change hostname to any desired domain
whoami– shows your user name (useful for knowing if you’re executing commands as root user or another user)
sudo -i– switch to root user if you haven’t already. “su -” is probably more proper since it creates a login shell with new environment.
passwd– change password for current user
yum update– update server packages (useful before doing new software installs)
- Auto completion – hit the [TAB] key while typing commands to auto complete names of directories and files.
- Connect to ssh
ssh user@ip -p 2222(the
-pand port number isn’t needed if using default port 22)
/etc/ssh/sshd_config– editing SSHD, changing SSH port number, allowing/disabling SSH or password authenthication, etc.
- getting SSH port
grep Port /etc/ssh/sshd_config(may need “sudo” in front)
systemctl restart sshd.service– restart sshd. Link.
SSH key – generate on Macbook terminal:
- generate SSH key
ssh-keygen -t rsa
- choose private key save location or leave empty for default
/home/user/.ssh/id_rsa, choose passphrase for private key if you want (I usually leave empty)
cat /home/user/.ssh/id_rsa.pubto see public key, copy and import it to where you need
ssh-add /home/user/.ssh/id_rsato load private key in terminal
Navigating around command line (full guide):
ls -a– list files in directory https://www.lostsaloon.com/technology/how-to-show-hidden-files-in-linux/
ls -l– list files but also show permissions, # of hardlinks, file owner and group, size and modification time.
cd– return to default working directory in linux (ideally, the root but often not the case)
pwd– shows path to current directory
- CTRL + L, to clear the screen
Files & Directories (create, delete, move, copy, archive):
mkdir test– make directory called “test”
rm test– delete file or directory called “test”
rm -rf test– deletes “test” directory without prompting you for every file
rm -rf *test*– deletes all files/directories with the string “test” in the name.
rm -fv *.txtremoves all files in current directory with “.txt” extension.
cp test /location– copy “test” file or directory to “/location” directory. Other options.
cp oldname.txt newname.txtcopies file to new name in same directory.
'cp' -R -rf file locationuse this to do recursive overwrite without any prompt
mv test /location– move “test” to “/location” directory. Other options.
tar -czvf folder.tar.gz folder– archive “folder” directory into folder.tar.gz file. Other compression commands.
tar -xzvf folder.tar.gz– extract archive in current working directory. Other options.
- Hide files and show hidden files
Files & Directories (ownership, permissions):
- Change file ownership –
chown USER:GROUP FILEor
chown -R USER: GROUP FILEfor recursive. Useful after migrating files from another server and they don’t work. Another link.
- change permissions – (Read, Write, Execute.)
- save command output to a file https://askubuntu.com/questions/420981/how-do-i-save-terminal-output-to-a-file
wget https://address.com/to/file.zip– download externalfile to current working directory
curl -0 https://addres.com/to/file.zipcan also work if wget doesn’t (other alternatives to wget)
rsync– will expand later
sftp user@serverIP_or_hostname– do this from destination server. (Use
sftp -oPort=1234 user@serverIP_or_hostnameif there’s a custom SFTP port other than 22.)
lscommands to navigate around the remote computer.
get filename.zip– to download file local.
- Reference link
VI text editor:
vi filename.txt– open any file up in vi editor
press[ESC]– to switch to normal mode
:i– insert (editing mode)
dd(from normal mode) – deletes the line under cursor. Other delete commands.
:q!– quit without saving
:wq– quit with saving
Disks, usage & space:
- Check available space –
df -h(friendly KB/MB/GB format),
df -l(local size only)
du -sh *– check sizes within current directory
sudo du -a /home/ | sort -n -r | head -n 20– lists largest files in “/home” directory.
- find large files
- unmount –
umount /path/to/mount(removes from /etc/fstab)
- view mounts –
- disk space commands and more du commands
du -hsx /* | sort -rh | head -10
- Check for listening ports
sudo lsof -i -P -n | grep LISTEN
- kill processes –
pkill 12345, replacing “12345” with actual process ID
Databases (MySQL & MariaDB):
- restart MariaDB –
systemctl start mariadb
- export (aka “dump”) mysql database into a file –
mysqldump -u dbuser -p dbname > dbfile.sql, you will be prompted for password
- import sql file into db (assuming db’s and users already created) –
mysql -u dbuser -p dbname < dbfile.sql, you will be prompted for password
cat /root/.my.cnf– recover mysql root pass, or reset it
- managing databases and users from SSH, nice video and explanation
- creating databases and users from SSH
- curious about trying non-default mysql configs? Try this.
MySQL commands (for MySQL shell/prompt):
mysql -u user -plogs you in,
exitlogs you out
SHOW DATABASES;list all databases
CREATE DATABASE database_name;– creates DB
DROP DATABASE database_name;– drops DB
SELECT user, host FROM mysql.user;– list all DB users
CREATE USER 'database_user'@'localhost' IDENTIFIED BY 'user_password';– creates DB user
DROP USER 'database_user'@'localhost';– deletes DB user
GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';– grant all privileges to specified user for specified database
GRANT ALL PRIVILEGES ON *.* TO 'database_user'@'localhost';– grant all privileges to specified user for all databases
REVOKE ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost';– revoke privileges
SHOW GRANTS FOR 'database_user'@'localhost';– see all user privileges
LiteSpeed web server:
- installing LS
- reset LS console pass –
cd /usr/local/lsws/admin/miscand then
- version check –
- start LS –
- restart LS –
- upgrade OLS –
yum update', then 'yum upgrade openlitespeed
- enable crawler (cPanel) –
vi /etc/apache2/conf.d/includes/pre_main_global.confand add
- view logs
- More LS license commands
- refresh disk quota
- license check
- force run backups –
/usr/local/cpanel/bin/backup --force(more info)
- update WHM
/scripts/upcp --force(if it’s already updated)
- Reset max deferred email limit – delete `rm /var/cpanel/email_send_limits/max_deferfail_thedomain.com`
- Error logs –
cat /home/cyberpanel/error-logs.txt(log files on CyberPanel)
- Cron schedules –
/etc/crontabclear out unnecessary cronjobs eating up server resources (backups)
- CSF firewall – CSF firewall installation and basic commands
- ports – closing ports
iptables -I INPUT 1 -m tcp -p tcp --dport 111 -j DROP,
iptables -I INPUT 1 -m udp -p udp --dport 111 -j DROP(replacing “111”) as needed
sudo dmesg -n 1– disable annoying kernel messages
- Security log-scanning commands, please see Recovering from HACKED server
systemctl status firewalld– check status
systemctl start firewalld– start it
systemctl enable firewalld– enables it
– see open ports, alternate:
for s in firewall-cmd --list-services; do firewall-cmd --permanent --service "$s" --get-ports; done;`
- open port, `firewall-cmd –permanent –add-port=1234/tcp` (using whichever port number you need) , then `firewall-cmd –reload`
systemctl stop firewalld– stops it
systemctl disable firewalld– disables it
Configuration files (common locations):
Logs (locations and common commands):
tail /location/of/logshows last 10 lines of log file
tail -n 100 /location/of/logshows last 100 lines
tail -f /location/of/logkeeps watching last 10 lines of log file
- You can also use “less +F” (but generally not for big files)
grep "abc" /file/nameto find lines with the string “abc” in them. See other grep examples.
Disks (format, partition, mount):
df -Thshow mounted disks/partitions and file systems
lsblkshow attached storage disks
- Partition & format disk –
sudo fdisk /dev/disknamereplace “diskname” with what you want (usually sda1/vdb1). From partition command line,
nfollow defaults, then
ato make it bootable (if needed),
pto check that it partitioned correctly, and
wto write these partition changes. Try
lsblkafterwards to check everything worked.
sudo mkfs.ext4 /dev/partitionnamepartition name is usually disk name with a partition number (sda1, sda2, etc). You can also switch
ext4file system to something else like
- Mount new disk –
sudo mkdir /disk1to create new “disk1” directory in your root (use another name if you want).
sudo mount /dev/partitionname /disk1mounts partition to the directory.
- https://upcloud.com/community/tutorials/adding-removing-storage-devices/ for more info on automatically mounting at boot, etc.