How to Set a Default Directory When SSHing into Your VPS

5 min read
bash

How to Set a Default Directory When SSHing into Your VPS

When you log into your VPS via SSH, you might want to start in a specific directory rather than your home directory. This can be useful for developers and system administrators who frequently work in a particular project folder. In this tutorial, we'll guide you through the steps to set a default directory when you SSH into your VPS.

Prerequisites

  • Access to your VPS via SSH

  • Basic knowledge of the command line

  • A text editor installed on your VPS (such as nano or vim)

Step-by-Step Guide

Step 1: SSH into Your VPS

First, log into your VPS using your preferred SSH method. You can do this from your terminal by running:

ssh your-username@your-vps-ip

Replace your-username with your SSH username and your-vps-ip with the IP address of your VPS.

Step 2: Determine Your Shell

Before editing your shell configuration files, determine which shell you are using. Most systems use bash by default, but you might be using zsh or another shell.

You can check your current shell with the following command:

echo $SHELL

If the output includes /bash, you're using bash. If it includes /zsh, you're using zsh.

Step 3: Edit the Shell Configuration File

Depending on your shell, you'll edit a different configuration file. Here are the common files:

  • Bash: ~/.bashrc or ~/.bash_profile

  • Zsh: ~/.zshrc

For this tutorial, we will assume you are using bash and will edit the .bashrc file. If you are using zsh, replace .bashrc with .zshrc in the following steps.

Open the .bashrc File

Use a text editor to open the .bashrc file in your home directory. You can use nano or vim:

nano ~/.bashrc

or

vim ~/.bashrc

Add the cd Command

Scroll to the end of the file and add the following line:

cd /home/examplewebsite/htdocs/examplewebsite.com

This command will automatically change the directory to /home/examplewebsite/htdocs/examplewebsite.com when you log in.

Save and Exit

  • If using nano: Press CTRL + O to save the changes, press Enter to confirm, and then press CTRL + X to exit.

  • If using vim: Press ESC, then type :wq and press Enter to save and exit.

Step 4: Apply the Changes

To apply the changes immediately without logging out, source the .bashrc file:

source ~/.bashrc

Alternatively, you can log out and log back in to apply the changes.

Step 5: Verify the Configuration

Log out and SSH back into your VPS:

exit
ssh your-username@your-vps-ip

You should now start in the /home/examplewebsite/htdocs/examplewebsite.com directory.

Troubleshooting

  • Permission Issues: Ensure you have the necessary permissions to access the directory you specified.

  • Directory Existence: Double-check that the directory /home/examplewebsite/htdocs/examplewebsite.com exists.

  • Shell Type: Ensure you are editing the correct configuration file for your shell.

Conclusion

By following these steps, you can set a default directory when SSHing into your VPS, streamlining your workflow and saving time navigating to your project folders. This simple configuration change can greatly enhance your efficiency when working on your server.

Discussion (0)

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!