惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Ittavern.com

Wimage - Hosting Open-Source Image Uploader with Podman and external S3 Storage Switching from Hugo to picopaper Encryption using SSH Keys with age in Linux ETag in nginx - Simple Resource Caching Sending nginx Logs to Loki with Grafana Alloy How to: Cisco ISE backup to SFTP repository with public key authentication Dummy IP & MAC Addresses for Documentation & Sanitization Deploying ISSO Commenting System for Static Content using Docker Generate a Vanity v3 Hidden Service Onion Address with mkp224o ssh-audit Primer - Audit your SSH Server mtr - More Detailed Traceroute - Network Troubleshooting My Personal Backup Strategy - August 2024 iperf3 - User Authentication with Password and RSA Public Keypair Adding a trash can to Linux with trash-cli Bandwidth Measurement using netcat on Linux Getting started with rsync - Comprehensive Guide Cron Jobs on Linux - Comprehensive Guide with Examples SSH Server Hardening Guide v2 Port Knocking with knockd and Linux - Server Hardening Getting started with rclone - Data transmission Getting started with dig - DNS troubleshooting Getting started with Fail2Ban on Linux Getting started with netcat on Linux with examples URL explained - The Fundamentals Troubleshooting Asking The Right Questions Getting started with tcpdump - Ittavern.com Curl on Linux - Reference Guide Getting started with nmap scripts My Offsite Backup - March 2023 Getting started with iperf3 - Network Troubleshooting ICMP echo requests on Linux and Windows - Reference Guide Simulate an unreliable network connection with tc and netem on Linux Detecting Rogue DHCP Server - Ittavern.com Basics of the Linux Bash Command History with Examples Getting started with GNU screen - Beginners Guide Basics of Power over Ethernet (PoE) Difference between RSS and Atom SSH Troubleshooting Guide - Ittavern.com Backup Guide - how to secure crucial data SSH - run script or command at login Linux - unmount a busy target safely Visual guide to SSH tunneling and port forwarding Guide to Wireshark display filters Online Security Guide - Ittavern.com My IT EDC tool kit v2212 10 prompts - 1000 AI generated images - openAI Dall-E SSH - How to use public key authentication on Linux Ways to support open-source projects Getting started with nmap - Ittavern.com nginx - simple and native authentication function Linux - How to work with complex commands EICAR test file - riskless method to test your antivirus and firewall solution Linux - connect to a serial port with screen Podman / Docker - expose port only to the localhost of the host machine Tmux - reload .tmux.conf configuration file My use cases for CyberChef Nginx - simple permanent or temporary redirects Getting started with tmux - Ittavern.com Tmux - synchronize the input of all panes within a window Nginx - check your public IP CyberChef - How to remove empty lines
Create tmux layouts using bash scripts
2023-06-16 · via Ittavern.com

I am a big fan of tmux, but there is - without adding plugins - a way to save and restore sessions or layouts. For this reason, I've decided to work on a bash script that restores and builds my favorite tmux layout at the start of the session. I've been testing it for the last couple of days, and it works well and saved me a ton of time.

Everyone has different requirements. I explain the commands so you can rebuild your favorite layout or session. And please remember that there are multiple ways to solve this.

The demo layout can be found at the bottom of this post.

The start

I wanted to create a new tmux session for every project I'd start working on. Let us begin with the following framework.

tmux-project-x.sh

#!/bin/bash

session_name='project-x'

tmux has-session -t $session_name
if [ $? != 0 ]
then

    ### Create session ###
    tmux new-session               -ds $session_name

fi

tmux attach -t $session_name

In short, we define the session's name as a variable, check if this session exists, and if it does not, we create it. In the last step, we try to attach the tmux session.

Within this script, we can create our layout. Don't forget to make the script executable sudo chmod +x tmux-project-x.sh.

Before we start - choose the right window and panel

The creation of the script involves a lot of trial and error. I hope I can provide you with some tips to make things easier.

Side note: just in case, here is a link to the tmux primer.

Session and window overview #

Get an overview of all tmux sessions, windows and panes by pressing the Prefix + w shortcut. This allows you to get a quick overview and move fastly within your tmux environment.

To get a quick overview of the panes of the current window, press Prefix + q to get the panes numbered like this:

tmux-overview

Syntax of the tmux commands #

Just to provide you with a quick explanation of the syntax of the following commands.

Run a command in a specific pane:
tmux send-keys -t $session_name:1.0 'cd /random/path/' C-m C-l
tmux send-keys # this is the tmux option, in this case it sends keystrokes
-t $session_name # selects the session, in this case, the provided variable in this script
:0.2 # choses the window and pane, in this case, the first window and pane number 2
'cd /random/path/' # the keystrokes, in this case a simple command - at this point, it would not run the command
C-m # which means CTRL + m - this runs the previously sent command
C-l # send the final keystroke, which means CTRL + l - to clear the current command line - looks way cleaner, but is optional

Just to give you an idea of how a simple command can look and what everything means.

The essential commands

Create a new window:
tmux new-window -t $session_name:1
Rename a specific window:
tmux rename-window -t $session_name:1 GENERAL
Select a specific window:
tmux select-window -t $session_name:1
Select a specific pane on the current window:
tmux select-pane -t 0

Spliting windows #

Split current pane horizontally:
tmux split-window -h -p 50 -t $session_name:0
-p 50 # means 50% of the way
 ┌────────────────┐      ┌────────────────┐
 │                │      │                │
 │                │      │                │
 │                ├───►  ├────────────────┤
 │                │      │                │
 │                │      │                │
 └────────────────┘      └────────────────┘
Split current pane vertically:
tmux split-window -v -p 50 -t $session_name:0
-p 50 # means 50% of the way
 ┌────────────────┐      ┌───────┬────────┐
 │                │      │       │        │
 │                │      │       │        │
 │                ├───►  │       │        │
 │                │      │       │        │
 │                │      │       │        │
 └────────────────┘      └───────┴────────┘

Side note: Since there is a lot of trial and error involved, you can kill a tmux session with Prefix + :kill-session.

Send keystrokes to pane

There are many things you could do with this one. Toy around and see what works for you. Changing directories, creating temp files, open specific files, running commands, starting scripts or programs, and so on.

Some examples:
tmux send-keys -t $session_name:1.2 'cd /random/path/' C-m # change to another directory
tmux send-keys -t $session_name:0.2 '~/random-script/' # without C-m the command will not be executed and only send to the terminal
tmux send-keys -t $session_name:2.2 'htop' C-m # start htop in the third window (starts with 0) and pane number 2

Design / customization

You can use color names like red or hex color codes like #ff1900

Background color of status bar:
tmux set -g status-style bg=red
tmux set -g status-style bg=#dc23a6
Set default border color of pane:
tmux set -g pane-border-style fg=magenta
Set color of currently active pane in the window:
tmux set -g pane-active-border-style "bg=default fg=magenta"
Set background color of panes:
tmux set -g window-style "fg=#E4E2E1 bg=#332E33"
Set the background color of the currently active pane:
tmux set -g window-active-style "fg=white bg=black"

Demo

tmux-demo-layout

demo.sh

#!/bin/bash

session_name='demo'

tmux has-session -t $session_name
if [ $? != 0 ]
then

    # SESSION

    cd ~
    tmux new-session               -ds $session_name
    tmux set-window-option          -t $session_name     allow-rename off
    tmux rename-window              -t $session_name:0   random-overview

    # GENERAL OPTIONS / DESIGN

    tmux set                        -g status-style bg=red
    tmux set                        -g status-style bg=red
    tmux set                        -g pane-border-style "fg=white"
    tmux set                        -g pane-active-border-style "bg=default fg=red"
    tmux set                        -g window-style "fg=#E4E2E1 bg=#332E33"
    tmux set                        -g window-active-style "fg=white bg=black"

    # FIRST WINDOW

    tmux send-keys                  -t $session_name:0.0 'htop' C-m
    tmux split-window      -v -p 25 -t $session_name:0
    tmux send-keys                  -t $session_name:0.1 'echo hello' C-m

    # SECOND WINDOW

    tmux new-window                 -t $session_name:1
    tmux set-window-option          -t $session_name:1   allow-rename off
    tmux rename-window              -t $session_name:1   another-overview

    tmux split-window      -h -p 50 -t $session_name:1
    tmux split-window      -v -p 50 -t $session_name:1
    tmux split-window      -v -p 50 -t $session_name:1

    tmux select-pane                -t 0
    tmux split-window      -v -p 50 -t $session_name:1

    # SELECT DEFAULT PANE AFTER OPENING

    tmux select-window              -t $session_name:1
    tmux select-pane                -t 0
fi

tmux attach -t $session_name

Conclusion

As I mentioned before, there are multiple ways to do it. From the config file to random plugins. I am still using it since it provides me with a lot of flexibility and per-project customizability. If you have any questions or tips, feel free to reach out.