Linux SSH Cheat Sheet – Basics

Linux Distros

Understanding SSH is fundamental to server management, and being able to quickly fire off commands to move around, view/edit files, update configurations, etc is essential to effectively investigating issues you are presented with. We hope this linux ssh cheat sheet will help you get the basics.

A popular free SSH client used by many of the world’s administrators is Putty, however there are other great tools out there that can make your SSH lif much simpler. One great combination is SecureCRT and SecureFX, which together provide you with a fully customisable, tabbed SSH interface, as well as a file transfer tool that can be easily switched to for SFTP transfers using the same login session as your existing SSH connection. On this page, we have compiled a list of some of the basic commands everyone should at least know before logging into a server.

Navigate Directories and View Files

cd   #change directory will allow you to navigate through directories in a number of ways allowing you to quickly get to the desired directory

cd ~   #go to your home directory.
cd –    #go to the last directory you were in.
cd ..   #go up a directory.
cd /example/directory/name    #will take you directly to the specified directory with the absolute root folder being the starting folder.
cd example/directory/name    #will take you into the specified directory with your current directory being the starting point.

ls   #list files/directories within the directory you are currently in.

ls -la   #shows all files (including ones that start with a period), directories, and details attributes for each file.

cat   #print a file’s contents to the screen

cat filename.txt    #this will show the contents of filename.txt on your screen. You can also specify a file within a different directory simple by adding the directory location in front of the filename.

File Permissions

chmod   # changes file access permissions. The numbers represent whether a file is read | write | executable for the USER – GROUP – EVERYONE

0 = —  No permission 1 = –X  Execute only 2 = – W –  Write only 3 = – W X  Write and execute 4 = R –  Read only 5 = R – X  Read and execute 6 = R W –  Read and write 7 = R W X  Read, write and execute
Usage: chmod {permissions_value} {filename}
Examples:
chmod 000 filename.txt
  #No one can access this file, including root.
chmod 644  filename.html   #Often used for HTML and most common website files.
chmod 755  foldername   #Used for some kinds of script files but also commonly used for a user’s web folders on a web server.

chown   #changes file ownership permissions in the order of USER – GROUP

chown root filename.txt   #This will change the owner of the specified file to root
chown root.root filename.txt   #This will change the owner amd group of the specified file to root

tail   #like cat, but only reads the end of the file

tail /var/log/messages   #see the last 20 (by default) lines of /var/log/messages
tail -f /var/log/messages   #watch the file continuously, while it’s being updated
tail -200 /var/log/messages   #print the last 200 lines of the file to the screen

more   #like cat, but opens the file one screen at a time rather than all at once

more /etc/userdomains   #browse through the userdomains file. hit Spaceto go to the next page, q to quit

pico (or nano)   # This is a very basic, user friendly, easy to use file editor sometimes used for more simple editing tasks

pico /home/burst/public_html/index.html   #edit the index page for the user’s website.

File Editing with Vim

vim   #Linux distros come with a variety of text editor options (e.g. pico, nano, vi). VIM is an enhanced version of vi that offers a huge amount of functions and flexibility, including code colour highlighting. There are so many functions to mention, we have another cheat sheet dedicated to this. However, below are the basic commands to get you started.

vim /home/user/public_html/index.html   #this will open a file for editing in Vim.

When Vim first opens a file, it will be in command mode, which allows you to easily navigate around a file, search for specific content, remove specific content, etc.  The following are usefull when in command mode:

:q!   #This force quits the file without saving and exits vim
:w   #This writes the file to disk, saves it
:wq   #This saves the file to disk and exists vi
:linenumber  (e.g. :25)   #Takes you to line 25 within the file
:$   #Takes you to the last line of the file
:0   #Takes you to the first line of the file

View Info, Stats, File Content

grep   #a tool to search for specific content/patterns within files

grep root /etc/passwd   #shows all matches of root in /etc/passwd
grep -v root /etc/passwd   #shows all lines that do not match root

last   #shows which users logged in and when

last -20   #shows only the last 20 logins
last -20 -a   #shows last 20 logins, with the hostname in the last field

who   #shows who is logged into the server in shell.

w   #similar to who but with more information about the login status.

netstat   #shows current network connections.

netstat -an   #shows all connections to the server, the source and destination ips and ports.
netstat -rn   #shows routing table for all ips bound to the server.

top   #shows current system processes, memory information, uptime and other useful server info. This is your first port of call when looking into your server’s status and possible causes of performance issues.

top – then – [shift] m to sort by memory usage
top – then – [shift] p to sort by CPU usage

ps   #shows current running processes and their PID without the additional of the top command. A process ID is a unique number that identifies each process, with which you can then kill or terminate a running program on your server if it is causing issues (see kill command).

ps U username   #shows processes for a certain user
ps aux   #shows all system processes
ps aux –forest   #shows all system processes like the above but organizes in a hierarchy that’s very useful!

kill   #terminate a system process. Use top or ps aux to get system PIDs (Process IDs)

kill PID   #kills PID
kill -9 PID   #kills PID

File and Folder Management

ln   #create links between files and directories

ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf   #this creates a symlink (shortcut) to the httpd.conf file within the /etc/ folder. You can access the file by using either the symlink or the the original file location.

touch   #creates an empty file

touch /home/{USER}/public_html/test.html  #this would create an empty file called test.html in the directory /home/{USER}/public_html/

file   #attempts to guess what type of file a file is by looking at it’s content.

file *   #prints out a list of all files/directories in a directory

du   #shows disk usage information.

du -sh   #shows a summary (in human-readable form) of total disk space used in the current directory, including subdirectories.
du -sh *   #similar to above, but shows information for each file and directory which can be useful when finding large files.

wc   #shows the word count of a file.

wc -l filename.txt   #tells how many lines are in filename.txt

cp   #copy a file

cp filename filename.bak   #copies filename to filename.bak
cp -a /home/{USER}/directoryone/* /home/{USER}/directorytwo/   #copies all files from one directory to another. File permissions remain the same for the copied files.
cp -av * ../newdirectory   #recursively copies all files and directories in the current directory into newdirectory.

mv   #move a file.

mv filename newfilename   #move filename to newfilename…this is essentially the way you rename a file in Linux.
mv directoryname newdirectoryname   #move directoryname to newdirectoryname…this is essentially the way you rename a directory in Linux.
mv filename /new/directory/   #move filename to /new/directory/
mv filename /new/directory/newfilename    #move filename to /new/directory/ and rename it newfilename.

rm   #delete a file

rm filename.txt    #deletes filename.txt, will more than likely ask if you really want to delete it
rm -f filename.txt   #deletes filename.txt, will not ask for confirmation before deleting.
rm -rf tmp/   #recursively deletes the directory tmp, and all files in it, including subdirectories. BE VERY CAREFULL WITH THIS COMMAND!!!

tar   #application for creating and extracting .tar.gz and .tar files.

tar -zxvf file.tar.gz   #extracts the tar.gz file.
tar -xvf file.tar   #extracts the tar file.
tar -cf archive.tar contents/   #takes everything from contents/ and puts it into archive.tar

zip   #extract .zip files from shell.

unzip file.zip

Apache Shell Commands

httpd -v   #outputs the build date and version of the Apache server.
httpd -l   #lists compiled in Apache modules.
httpd status   #only works if mod_status is enabled and shows a page of active connections service.
httpd restart   #restarts Apache

MySQL Shell Commands

mysqladmin processlist   #shows active mysql connections and queries.
mysqladmin drop databasenamehere   #drops/deletes the selected database.
mysqladmin create databasenamehere   #creates a mysql database

Restore MySQL Database Shell Command

mysql -u username -p password databasename < databasefile.sql   #restores a MySQL database from databasefile.sql

Backup MySQL Database Shell Command

mysqldump -u username -p password databasename > databasefile.sql   #backup MySQL database to databasefile.sql