Essential Linux Commands and SSH for Web Developers

Linux ~ 74 minutes

# Welcome to the Linux terminal

Hello and welcome! Most web servers in the world run Linux. So, if you want to publish a website, you need to know a few Linux commands. Do not worry. You do not need to learn hundreds of them. A small group of commands is enough to start.

In this tutorial, we will practice the most useful commands. After that, we will learn SSH. SSH is a safe way to connect to a remote server from your own computer.

What you will learn

  • How to find your way around folders
  • How to create, copy, move and delete files
  • How file permissions work
  • How to search text and install software
  • How to connect to a server with SSH keys

Please open a terminal on Linux or macOS. On Windows, you can use WSL or Git Bash. Let us begin!

Show the current folder
Code Copied to Clipboard

# Step 1: Where am I?

The command pwd means print working directory. It shows the folder where you are right now. You will see a path like /home/ali. A path is just the address of a folder.

In Linux, folders are called directories. The top of everything is /, called the root. Your personal folder is called the home directory, and it has a short name: ~. Now, let us see what is inside this folder.

List files with details
Code Copied to Clipboard

Understanding ls -la

The ls command lists files. The -l flag shows a long list with sizes and dates. The -a flag shows hidden files too. Hidden files start with a dot, like .bashrc or .ssh.

You will use ls -la very often. It is a good friend, so please remember it.

# Step 2: Create a folder

Let us make a small practice area. The mkdir command creates a directory. The -p flag creates parent folders too, if they do not exist. This is very helpful, and it does not give an error if the folder already exists.

Create practice folders
Code Copied to Clipboard

# Move into the folder

Now, please enter the new folder. The cd command means change directory. Use cd .. to go one level up, and cd ~ to go back home. Press the Tab key to complete long folder names. It saves time and avoids spelling mistakes.

Enter the demo folder
Code Copied to Clipboard

# Step 3: Create and read files

Let us create a text file. The echo command prints a message. With the > sign, we send that message into a file instead of the screen. If the file does not exist, Linux creates it for us.

Write text into a file
Code Copied to Clipboard
Be careful with the > sign

The single > sign replaces everything inside the file. If the file already had text, that text is gone. To add a line at the end of a file, use two signs: >>.

Linux does not ask "Are you sure?" for many actions. So please read your command twice before you press Enter.

Read the file
Code Copied to Clipboard

# Step 4: Copy, move and delete

The cat command prints the file on your screen. For long files, you can use less instead, and press q to quit.

Now, let us copy a file. The cp command needs two things: the source and the new name. After the command, you will have two files with the same text.

Copy a file
Code Copied to Clipboard

# Rename a file

The mv command moves a file. It is also the way to rename a file, because renaming is moving to a new name. Let us rename our backup file.

Rename a file
Code Copied to Clipboard

Deleting is permanent

The rm command removes a file. Linux has no recycle bin in the terminal. A deleted file is gone for good. Please never run rm -rf on a folder unless you are 100 percent sure about the path. A small spelling mistake can delete important data.

Delete a file
Code Copied to Clipboard

# Step 5: File permissions

Every file in Linux has permissions. They say who can read, write or run the file. This is very important on a web server, because a wrong permission can break your site or open a security hole.

Let us look at the permissions of our file first. Run the command below and look at the first column, such as -rw-r--r--.

Show permissions
Code Copied to Clipboard

# Reading the permission letters

The letters come in three groups: the owner, the group and others. Each group can have three powers.

The three powers

  • r means read, and its number is 4
  • w means write, and its number is 2
  • x means execute, and its number is 1

You add the numbers to make one digit. For example, read plus write is 6. So 644 means: the owner can read and write (6), the group can read (4), and others can read (4). This is a good setting for normal files.

Change permissions
Code Copied to Clipboard

Never use 777

You may see tutorials that say chmod 777 to fix every problem. Please do not do this. The number 777 lets everyone read, write and run the file. On a real server, this is dangerous. Use 644 for files and 755 for folders instead. You can read more about ownership with the chown command in the Linux man pages.

# Step 6: Search inside files

Searching is a daily job for developers. The grep command finds text inside files. In the next command, -r searches inside all folders, and -n shows the line number. We search for the word Hello in the current folder, which is marked with a dot.

Search for text
Code Copied to Clipboard

# Step 7: Install software

On Ubuntu and Debian, you install software with apt. Because it changes the system, you need admin power. You get it by writing sudo before the command. The word sudo means "run this as the administrator".

It is a good habit to refresh the list of available software first. Please run the command below.

Refresh the package list
Code Copied to Clipboard
Use sudo with care

With sudo, you can change or remove anything on the server. So, please follow these simple rules.

Safe habits

  • Do not copy and paste commands you do not understand
  • Never use sudo when a normal command is enough
  • Work as a normal user, not as root, whenever possible

After apt update, you can install a program with sudo apt install package-name. For example, sudo apt install nginx installs the Nginx web server.

# Step 8: Connect with SSH

Now, let us learn SSH. It means Secure Shell. It lets you open a terminal on a far away server, and all the data between you and the server is encrypted.

You can log in with a password, but a key pair is safer and more comfortable. A key pair has two parts: a private key that stays on your computer, and a public key that you put on the server. Let us create the keys.

Create an SSH key pair
Code Copied to Clipboard

# About the key questions

The tool will ask where to save the key. Press Enter to use the default place, which is ~/.ssh/id_ed25519. Then it asks for a passphrase. A passphrase is an extra password for your key. We suggest you set one.

When it finishes, you will have two files. The file id_ed25519 is your private key. The file id_ed25519.pub is your public key. Next, we copy the public key to the server. Please change the user name and the IP address to your own server details.

Copy your public key to the server
Code Copied to Clipboard

# Log in to the server

The server asks for your password one last time. After that, your key is saved on the server. Now, you can log in without typing the password. Let us try it.

Connect to the server
Code Copied to Clipboard
Protect your private key

Your private key is like the key to your house. Never share it, never send it by email, and never upload it to GitHub. Only the public key, the file that ends with .pub, can be shared.

If SSH says that the key file is "too open", fix it with chmod 600 ~/.ssh/id_ed25519. SSH refuses to use a private key that other people can read.

# Make SSH shorter

Typing the full user name and IP address is tiring. You can save a short name in the file ~/.ssh/config. Then you only type ssh myserver. Here is an example. Please change the values to match your server.

~/.ssh/config
Code Copied to Clipboard

# Send files with scp

One more useful tool is scp. It copies files between your computer and the server over SSH. Now that we have a short name, the command becomes very simple. The next command sends notes.txt to the home folder of the server.

Upload a file with scp
Code Copied to Clipboard

Leaving the server

When you finish your work on the server, type exit and press Enter. You will come back to your own computer. Learn more about SSH in the OpenSSH manual.

# Summary and next steps

Excellent work! You have learned the most important Linux and SSH skills for web developers. Let us repeat the main points.

What we learned

  • pwd, ls, cd and mkdir help you move around
  • cp, mv and rm manage your files, and rm is permanent
  • chmod sets permissions, and 644 and 755 are safe choices
  • grep searches text, and sudo apt installs software
  • SSH keys give a safe and easy login to your server

Where to go next

  • Learn the nano editor to edit files on a server
  • Set up a firewall with ufw
  • Follow our tutorial on deploying a Laravel app with Nginx

Please practice these commands every day. Soon they will feel natural. Good luck!