Published on August 03, 2024By DeveloperBreeze

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.

Comments

Please log in to leave a comment.

Continue Reading:

Tailwind Browser Mockup

Published on January 26, 2024

Simple and Clean Tailwind Buttons

Published on January 26, 2024

Tailwind Buttons with Arrow Icon

Published on January 26, 2024

AI Interactive Chat Interface

Published on January 26, 2024

AI Chat Interface with Online Assistant

Published on January 26, 2024

CSS Grid and Flexbox: Mastering Modern Layouts

Published on August 03, 2024

csshtml

Creating a Simple REST API with Flask

Published on August 03, 2024

python

Building a Real-Time Chat Application with WebSockets in Node.js

Published on August 03, 2024

javascriptcsshtml

JavaScript Code Snippet: Fetch and Display Data from an API

Published on August 04, 2024

javascriptjson

Python Code Snippet: Simple RESTful API with FastAPI

Published on August 04, 2024

jsonpython