MASSMEDIUMS - Responsive Web Design Cincinnati MASSMEDIUMS - Responsive Web Design Cincinnati

Cheatsheet for SSH & Linux Shell commands

Let's face it, SSH is one of the most important tools any good web developer has. If efficiency is important to you (as it should be), SSH should be your primary means of interacting with your servers, whether working locally or on a remote server. Linux Shell commands allow you to quickly and easily perform system-wide tasks such as installing & updating software, executing GIT commands, site deployment , etc. SSH allows you to remotely connect to your server and command it via the shell. This page designed to help beginners starting out with SSH, by providing a usable SSH cheatsheet of common commands you will use commonly while working with your server.

Basic SSH Commands

ls : list files/directories in a directory.
ls -al : shows all files including hidden files, directories and details for each file.

cd : change directory
cd /path/to/directory : go to /path/to/directory
cd ~ : go to your home directory
cd - : go to the last directory you were in
cd .. : go up a directory

cat : print file contents to the screen
cat filename.txt : cat the contents of filename.txt to your screen

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.

more : hit space to go to the next page, q to quit

File Editing via SSH commands

pico : friendly, easy to use file editor
pico /home/burst/public_html/index.html : edit the index page for the user's website.

vi : another editor, tons of features
vi /home/burst/public_html/index.html : edit the index page for the user's website.

grep : looks for patterns in 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

touch : create an empty file
touch /home/burst/public_html/404.html : create an empty file called 404.html in the directory /home/burst/public_html/

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.

Use extreme caution with rm. important content can be deleted that can never be recovered.

cp : copy a file
cp sample sample.backup : copies sample to sample.backup
cp -a /home/backup/* /home/public_html/ : copies all files ∓ permissions to another directory.
find * -type d|xargs -i cp --verbose php.ini {} : copies php.ini into all directories recursively.

wc : word count
wc -l filename.txt : tells how many lines are in filename.txt

last : shows who 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

ln : create's "links" between files and directories
ln -s /home/username/tmp/webalizer webstats : Now you can display to show your webalizer stats online.

ln : you can delete the symlink (webstats) and it will not delete the original stats on the server

Permissions and Ownership via SSH

chmod: changes file access permissions (USER – GROUP – EVERYONE)

0 = —    No permission
1 = –X   Execute only
2 = -W-  Write only
3 = -WX  Write and execute
4 = R–   Read only
5 = R-X  Read and execute
6 = RW-  Read and write
7 = RWX  Read, write and execute

chmod 000 : No one can access
chmod 644 : Usually for files, such as HTML, PHP
chmod 755 : Usually for Directories and CGI scripts

chown : changes file ownership permissions (USER – GROUP)
chown root myfile.txt : Changes the owner of the file to root
chown root.root myfile.txt : Changes the owner and group of the file to root

Server Utilities via SSH

w : shows who is currently logged in and where they are logged in from.

netstat : shows all 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 live system processes in a nice table, memory information, uptime and other useful info.
top Shift + M : sort by memory usage
top Shift + P : sort by CPU usage/p>

ps : ps is short for process status. It's used to show currently running processes and their PID.
ps U username : shows processes for a certain user
ps aux : shows all system processes
ps aux --forest : shows all system processes but organizes into a very useful hierarchy

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.
du -sh : shows readable summary of total disk space used in current directory, including subdirectories.
du -sh * : same thing, but for each file and directory. helpful when finding large files taking up space.

kill : terminate a system process
kill -9 PID EG : kill -9 431
kill PID EG : kill 10550

Copying & Moving Files via SSH

Often you will need to move one or more files/folders or copy them to a different location. You can do so easily using an SSH connection. The commands which you would need to use are mv (short for ‘move’) and cp (short for ‘copy’).

The mv command syntax looks like this:

mv configuration.php-dist configuration.php

By issuing the above command we will move (rename) the file configuration.php-dist to configuration.php.

You can also use mv to move a whole directory and its content:

mv includes/* ./

This will move all files (and folders) in the includes/ directory to the current working directory.

In some cases however, we will need to only update the files and move only files that were changed, which we can do by passing ‘-u’ as argument to the command:

mv -u includes/* admin/includes

The copy cp command works the same way as mv, but instead of moving the files/folders it copies them. For example:

cp configuration.php-dist configuration.php

The command will copy the configuration.php-dist file to configuration.php and will preserve the original file (the file will NOT be removed after it is copied).

cp also accepts various arguments:

cp -R includes/ includes_backup/

-R instructs cp to copy files recursively (for example, a whole directory). To overwrite already existing files you should use the -f argument:

cp -Rf includes/ admin/includes/

Putting SSH Commands Together

Often you will find you need to use different commands on the same line. Here are some examples. Note that the | character is called a pipe, it takes date from one program and pipes it to another.
>   : means create a new file, overwriting any content already there.
>> : means tp append data to a file, creating a newone if it doesn not already exist.
<   : send input from a file back into a command.

grep User /usr/local/apache/conf/httpd.conf | more

This will dump all lines that match User from the httpd.conf, then print the results to your screen one page at a time.

last -a > /root/lastlogins.tmp

This will print all the current login history to a file called lastlogins.tmp in /root/

tail -10000 /var/log/exim_mainlog | grep | more

This will grab the last 10,000 lines from /var/log/exim_mainlog, find all occurances of 
(the period represents 'anything', comment it out with a so it will be interpretted literally), then send it to your screen page by page.

netstat -an | grep :80 | wc -l

Show how many active connections there are to apache (httpd runs on port 80)

mysqladmin processlist | wc -l

Show how many current open connections there are to mysql

mysqldump -u username -p dbname > file.sql

MySQL Dump

mysql -u username -p database_name <file.sql

Importing MySQL database

tar -zxvf file.tar.gz

UnTAR file

  • Home
  • Blog
  • How to manually add an SPF record via WHM
Ready to get started? Let us take your website to the next level. LET'S TALK
MASSMEDIUMS has been essential in the growth of our company, delivering high-impact marketing tools and a first-rate web presence

, VP, Ziegler Practice Transitions