Best Practices For Configuring Interactive Shell Sessions

Customizing Your Shell Environment

The shell environment constitutes the user interface through which users interact with the Linux operating system. Customizing the shell allows users to optimize their workflow and boost productivity. Users can set their preferred shell, configure startup scripts, and tailor settings to their specific needs.

Setting your default shell (chsh command, editing /etc/passwd file)

The default shell dictates the command line environment users are placed into upon login. The chsh command allows changing the default shell interactively by invoking the command and specifying the new shell path:

chsh -s /bin/zsh

This will set the Z shell (zsh) as the new default shell. The other method involves directly editing the /etc/passwd file and replacing the existing shell path with the new one.

Choosing your preferred shell (bash, zsh, fish, etc.)

Linux offers various shell options including the Bourne Again SHell (bash), the Z SHell (zsh), the Friendly Interactive SHell (fish) and others. Bash remains the most popular default, providing cross-compatibility across most Linux distributions. Zsh offers additional features like autocompletion and theming options. Fish uses syntax highlighting and autosuggestions to enhance usability. The choice comes down to personal preference and workflow requirements.

Configuring your shell startup scripts (.bashrc, .zshrc)

Startup scripts like .bashrc and .zshrc allow customizing the environment at login. These scripts get invoked when a new shell session spawns. Typical customizations include setting aliases, enabling completion, adding directories to the PATH, configuring the prompt and more. Keeping these configurations in a startup script centralizes the changes and eliminates having to manually apply settings on every shell invocation.

Improving Productivity

Built-in shell capabilities exist to boost user productivity during interactive sessions. Understanding and properly utilizing these features quickens common tasks and minimizes redundant typing.

Enabling tab completion (examples for bash and zsh)

Tab completion automatically fills out partial commands or file paths by tapping the tab key. Bash enables this feature by default while Zsh offers enhanced completion Backtracked to populate suggestions based on history and heuristics. Users can tweak completion further by installing third-party modules and plugins.

# Bash completion
complete -W "start stop status" service 

# Zsh completion
autoload -Uz compinit
compinit

Setting aliases for common commands (alias examples)

Aliases provide abbreviated substitutions for commonly invoked commands. They come in handy for long, complex or frequent sequences that benefit from shortening.

# Long command
git checkout --recurse-submodules --progress --force

# Alias  
alias gcf='git checkout --recurse-submodules --progress --force'

Using history for faster command repeats (history settings)

The history mechanism logs previously entered commands and enables quick access via the up arrow or history search. Users can tweak settings like the size of the log, frequency of writing to disk and more.

# Bash
HISTSIZE=10000
HISTFILESIZE=20000

# Zsh
setopt INC_APPEND_HISTORY # To save all lines, not just commands
setopt SHARE_HISTORY # Share history between running shells

Enhancing Appearance and Readability

Visual customizations help reduce eye strain and improve context when working on the command line over extended periods.

Changing the prompt style (PS1 examples, right prompt)

The typically sparse prompt can embed useful information like current directory, Git branch, time and more with some basic PS1 code:

PS1='[\u@\h \W]\$ '
# [\user@host dir]$

Zsh enables configuring the RPROMPT for displaying data on the right.

Setting the terminal color scheme (LS_COLORS, dircolors)

Color coding system output helps distinguish file types, status indicators, errors and other meaningful signals:

LS_COLORS=$LS_COLORS:'di=0;35' ; export LS_COLORS
dircolors -p > ~/.dircolors # To save schemes

Configuring full-screen editing (vim, emacs)

Shells integrate with full-powered editors like Vim and Emacs for working with code and prose. Useful settings include:

# Vim 
set nu rnu ai hls is ei sts=2 sw=2 ts=2

# Emacs
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)

Managing Multiple Sessions

Developers often juggle multiple shell sessions for organizing workspaces. Understanding session tools enables saner context switching between tasks.

Opening new terminal sessions (gnome-terminal, tabs)

Graphical terminal emulators streamline spawning extra sessions through tabs and splits. The Ctrl+Shift+T keyboard shortcut creates a new tab in many emulators.

Switching between sessions (tmux, screen)

Terminal multiplexers enable multiple shell instances inside a single terminal viewport. Tmux and GNU Screen allow detaching and reattaching sessions.

# Tmux
tmux new -s mysession
tmux attach -t mysession

# Screen 
screen -S mysession
screen -r mysession

Connecting remote sessions (ssh, mosh)

Secure Shell (ssh) facilitates encrypted connections to remote servers for command line access and file transfers.

ssh user@host
# More reliable 
mosh user@host

Mosh offers improvements over ssh like roaming connections, intermittent networking support and local echo of typed characters.

Securing Your Sessions

Shell environments tend to end up with elevated privileges so hardening sessions is imperative.

Avoiding buffer overflow attacks (restricted shells)

Restricted shells limit exposure by selectively disabling potentially dangerous utilities like reboot(), mail() and Python.

rssh -l limitedshell username
# Or
scponly username

Detecting session hijacks (history monitoring)

Monitoring bash history file access times detects tampering. The shell enzy script takes this further by storing a hash of history contents and sending alerts upon changes.

Enabling two-factor authentication (Google Authenticator, YubiKey)

Two-factor authentication fortifies sign-on by requiring a randomly generated access token from a separate device in addition to the user’s credentials.

google-authenticator -t -d -f -r 3 -R 30 -W

Leave a Reply

Your email address will not be published. Required fields are marked *