Остання активність 1767374640

aptupdate Неформатований
1#!/bin/sh
2sudo apt update
3sudo DEBIAN_FRONTEND=noninteractive apt -y upgrade
4sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
5sudo apt -y autoremove
6sudo apt -y autoclean
7sudo apt -y clean
firstroot.sh Неформатований
1#!/usr/bin/env bash
2# Root shell setup for Ubuntu
3# Run this script as root.
4set -euo pipefail
5
6if [ "$(id -u)" -ne 0 ]; then
7 echo "Please run as root." >&2
8 exit 1
9fi
10
11# =========================
12# Settings
13# =========================
14PREFER_EDITOR="${PREFER_EDITOR:-vim}"
15PACKAGES=(bash-completion vim less)
16
17msg() { printf "\033[1;32m[OK]\033[0m %s\n" "$*"; }
18warn() { printf "\033[1;33m[WARN]\033[0m %s\n" "$*"; }
19err() { printf "\033[1;31m[ERR]\033[0m %s\n" "$*" >&2; }
20
21backup_file() {
22 local f="$1"; [ -f "$f" ] || return 0
23 local ts; ts="$(date +%Y%m%d-%H%M%S)"
24 cp -a "$f" "${f}.bak.${ts}"
25 msg "Backup: ${f}.bak.${ts}"
26}
27
28append_once() {
29 # $1=file, $2=unique tag line, $3=payload
30 local file="$1" tag="$2" payload="$3"
31 grep -Fqx "$tag" "$file" 2>/dev/null && return 0
32 printf "\n%s\n%s\n" "$tag" "$payload" >>"$file"
33}
34
35# =========================
36# 0) Ensure base packages (no sudo needed)
37# =========================
38if command -v apt-get >/dev/null 2>&1; then
39 export DEBIAN_FRONTEND=noninteractive
40 apt-get update -y || true
41 apt-get install -y "${PACKAGES[@]}" || warn "Some packages failed to install; continuing."
42else
43 warn "apt-get not found; skipping package install."
44fi
45
46# =========================
47# 1) Target files
48# =========================
49ROOT_HOME="/root"
50BASHRC="${ROOT_HOME}/.bashrc"
51INPUTRC="${ROOT_HOME}/.inputrc"
52[ -f "$BASHRC" ] || touch "$BASHRC"
53[ -f "$INPUTRC" ] || touch "$INPUTRC"
54backup_file "$BASHRC"
55backup_file "$INPUTRC"
56
57# =========================
58# 2) force_color_prompt=yes
59# =========================
60if grep -Eq '^[[:space:]]*#?[[:space:]]*force_color_prompt[[:space:]]*=' "$BASHRC"; then
61 sed -i '0,/^[[:space:]]*#\{0,1\}[[:space:]]*force_color_prompt[[:space:]]*=.*/s//force_color_prompt=yes/' "$BASHRC"
62else
63 sed -i '1iforce_color_prompt=yes' "$BASHRC"
64fi
65msg "Enabled force_color_prompt=yes for root."
66
67# =========================
68# 3) bash-completion
69# =========================
70COMPLETION_TAG="# >>> chatgpt-root: bash-completion enable >>>"
71COMPLETION_BLOCK=$(cat <<'EOF'
72# >>> chatgpt-root: bash-completion enable >>>
73if [ -n "$BASH_VERSION" ] && ! shopt -oq posix; then
74 if [ -f /usr/share/bash-completion/bash_completion ]; then
75 . /usr/share/bash-completion/bash_completion
76 elif [ -f /etc/bash_completion ]; then
77 . /etc/bash_completion
78 fi
79fi
80# <<< chatgpt-root: bash-completion enable <<<
81EOF
82)
83append_once "$BASHRC" "$COMPLETION_TAG" "$COMPLETION_BLOCK"
84msg "Ensured bash-completion is sourced for root."
85
86# =========================
87# 4) History: timestamps + merge + safer defaults
88# =========================
89HIST_TAG="# >>> chatgpt-root: history settings >>>"
90HIST_BLOCK=$(cat <<'EOF'
91# >>> chatgpt-root: history settings >>>
92# Timestamps (YYYY-MM-DD HH:MM:SS)
93export HISTTIMEFORMAT='%F %T '
94# Safer history: ignore duplicates & commands starting with a space; remove duplicate older entries
95export HISTCONTROL=ignoredups:ignorespace:erasedups
96export HISTSIZE=300000
97export HISTFILESIZE=600000
98# Merge history across concurrent shells
99shopt -s histappend
100if [[ -n "${PROMPT_COMMAND:-}" ]]; then
101 case "$PROMPT_COMMAND" in
102 *'history -a; history -c; history -r'*) : ;;
103 *) PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND" ;;
104 esac
105else
106 PROMPT_COMMAND='history -a; history -c; history -r'
107fi
108# <<< chatgpt-root: history settings <<<
109EOF
110)
111append_once "$BASHRC" "$HIST_TAG" "$HIST_BLOCK"
112msg "Enabled history timestamps and merging for root."
113
114# =========================
115# 5) Prompt, aliases, shopt, editor/pager (root-flavored)
116# =========================
117QOL_TAG="# >>> chatgpt-root: QoL shell block >>>"
118QOL_BLOCK=$(cat <<'EOF'
119# >>> chatgpt-root: QoL shell block >>>
120# Minimal Git branch helper (fast, no heavy libs)
121__chatgpt_git_branch() {
122 local b
123 b=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || return 0
124 [ "$b" = "HEAD" ] && b=$(git rev-parse --short HEAD 2>/dev/null)
125 [ -n "$b" ] && printf ' (%s)' "$b"
126}
127
128# Prompt for root: show exit code when non-zero; root@host in red; cwd in magenta; git branch
129__chatgpt_set_ps1_root() {
130 local exit="$?"
131 local red="\[\e[31m\]" green="\[\e[32m\]" yellow="\[\e[33m\]" blue="\[\e[34m\]" magenta="\[\e[35m\]" cyan="\[\e[36m\]" reset="\[\e[0m\]"
132 local code=""
133 [ "$exit" -ne 0 ] && code="${red}↯${exit}${reset} "
134 PS1="${code}${red}\u${reset}@${blue}\h${reset}:${magenta}\w${reset}\$(__chatgpt_git_branch)${cyan}# ${reset}"
135}
136case "${PROMPT_COMMAND:-}" in
137 *"__chatgpt_set_ps1_root"*) : ;;
138 "") PROMPT_COMMAND="__chatgpt_set_ps1_root" ;;
139 *) PROMPT_COMMAND="__chatgpt_set_ps1_root; $PROMPT_COMMAND" ;;
140esac
141
142# Useful shopt toggles
143shopt -s checkwinsize cdspell autocd globstar
144
145# Root-friendly aliases
146alias ls='ls --color=auto -F --group-directories-first'
147alias ll='ls -lah'
148alias la='ls -A'
149alias grep='grep --color=auto'
150alias df='df -h'
151alias free='free -h'
152alias watchd='watch -n1 -d'
153alias cp='cp -i'
154alias mv='mv -i'
155alias rm='rm -I' # prompt once if >3 files
156alias ..='cd ..'
157alias ...='cd ../..'
158
159# Prefer modern tools if present
160command -v batcat >/dev/null 2>&1 && alias bat='batcat'
161command -v bat >/dev/null 2>&1 && alias cat='bat --paging=never'
162command -v lsd >/dev/null 2>&1 && alias ls='lsd'
163
164# Defaults
165export EDITOR="${EDITOR:-vim}"
166export VISUAL="$EDITOR"
167export PAGER="${PAGER:-less}"
168export LESS='-R'
169export GPG_TTY="$(tty 2>/dev/null || true)"
170
171# Security hygiene: stricter umask for root
172umask 027
173# <<< chatgpt-root: QoL shell block <<<
174EOF
175)
176append_once "$BASHRC" "$QOL_TAG" "$QOL_BLOCK"
177msg "Added root QoL prompt/aliases/shopt/editor block."
178
179# Ensure the preferred editor is set explicitly
180if grep -Fq 'export EDITOR=' "$BASHRC"; then
181 sed -i "0,/export EDITOR=.*/s//export EDITOR=\"${PREFER_EDITOR}\"/" "$BASHRC"
182else
183 echo "export EDITOR=\"${PREFER_EDITOR}\"" >> "$BASHRC"
184fi
185msg "Default EDITOR for root set to ${PREFER_EDITOR}"
186
187# =========================
188# 6) Readline tweaks for root
189# =========================
190INPUT_TAG="# >>> chatgpt-root: readline tweaks >>>"
191INPUT_BLOCK=$(cat <<'EOF'
192# >>> chatgpt-root: readline tweaks >>>
193set show-all-if-ambiguous on
194set menu-complete-display-prefix on
195set colored-stats on
196set mark-symlinked-directories on
197set completion-ignore-case on
198set completion-map-case on
199"\e[A": history-search-backward
200"\e[B": history-search-forward
201# <<< chatgpt-root: readline tweaks <<<
202EOF
203)
204append_once "$INPUTRC" "$INPUT_TAG" "$INPUT_BLOCK"
205msg "Improved /root/.inputrc completion/behavior."
206
207# =========================
208# Done
209# =========================
210msg "Root shell setup complete. Open a NEW root shell or run: source /root/.bashrc"
211
firstrun Неформатований
1#!/bin/sh
2cd ~
3# sudo apt-mark hold grub* ssh*
4sudo DEBIAN_FRONTEND=noninteractive apt-get -y update
5sudo DEBIAN_FRONTEND=noninteractive apt-get -y upgrade
6sudo apt -y install gdebi
7
8
9sudo apt update
10sudo apt -y upgrade
11sudo apt -y autoremove
12sudo apt -y autoclean
13sudo apt -y clean
14
15# sudo apt-mark unhold grub* ssh*
16sudo curl -L https://gist.yais.me/yaisme/util/raw/HEAD/aptupdate -o /tmp/aptupdate
17sudo mv /tmp/aptupdate /usr/bin/aptupdate
18sudo chmod +x /usr/bin/aptupdate
19sudo apt remove -y unattended-upgrades
20
firstrunv2.sh Неформатований
1#!/usr/bin/env bash
2set -euo pipefail
3
4# =========================
5# Settings (adjust freely)
6# =========================
7PREFER_EDITOR="${PREFER_EDITOR:-nano}"
8PACKAGES=(
9 bash-completion git curl wget ca-certificates gnupg
10 nano vim tmux htop tree unzip zip
11 ripgrep fd-find fzf
12 build-essential gdebi-core
13)
14
15APTUPDATE_PATH="/usr/bin/aptupdate"
16APTUPDATE_URL="https://gist.yais.me/yaisme/util/raw/HEAD/aptupdate"
17
18# =========================
19# Helpers
20# =========================
21msg() { printf "\033[1;32m[OK]\033[0m %s\n" "$*"; }
22warn() { printf "\033[1;33m[WARN]\033[0m %s\n" "$*"; }
23err() { printf "\033[1;31m[ERR]\033[0m %s\n" "$*" >&2; }
24
25backup_file() {
26 local f="$1"; [ -f "$f" ] || return 0
27 local ts; ts="$(date +%Y%m%d-%H%M%S)"
28 cp -a "$f" "${f}.bak.${ts}"
29 msg "Backup: ${f}.bak.${ts}"
30}
31
32# Upsert a tagged block (replace if exists, append if missing)
33# - start_marker and end_marker must be exact full lines.
34upsert_block() {
35 local file="$1" start_marker="$2" end_marker="$3" payload="$4"
36
37 local payload_tmp; payload_tmp="$(mktemp)"
38 printf "%s\n" "$payload" > "$payload_tmp"
39
40 if grep -Fqx "$start_marker" "$file" 2>/dev/null; then
41 awk -v start="$start_marker" -v end="$end_marker" -v tmp="$payload_tmp" '
42 BEGIN{inblk=0}
43 $0==start {
44 while ((getline line < tmp) > 0) print line
45 close(tmp)
46 inblk=1
47 next
48 }
49 inblk && $0==end { inblk=0; next }
50 !inblk { print }
51 ' "$file" > "${file}.tmp"
52 mv "${file}.tmp" "$file"
53 else
54 printf "\n%s\n" "$payload" >>"$file"
55 fi
56
57 rm -f "$payload_tmp"
58}
59
60ensure_local_bin() {
61 mkdir -p "$HOME/.local/bin"
62 case ":${PATH:-}:" in
63 *":$HOME/.local/bin:"*) : ;;
64 *) echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.profile" ;;
65 esac
66}
67
68# =========================
69# 0) System update/upgrade
70# =========================
71cd "$HOME"
72
73export DEBIAN_FRONTEND=noninteractive
74sudo apt-get -y update
75sudo apt-get -y upgrade || true
76
77# Keep your style (and keep unattended-upgrades removed)
78sudo apt -y update
79sudo apt -y upgrade || true
80sudo apt -y autoremove
81sudo apt -y autoclean
82sudo apt -y clean
83sudo apt remove -y unattended-upgrades || true
84msg "Base update/upgrade complete (unattended-upgrades removed)."
85
86# =========================
87# 1) Install /usr/bin/aptupdate
88# (download; if fails, write a built-in fallback)
89# =========================
90tmpfile="$(mktemp)"
91if command -v curl >/dev/null 2>&1 && curl -fsSL "$APTUPDATE_URL" -o "$tmpfile"; then
92 sudo mv "$tmpfile" "$APTUPDATE_PATH"
93 msg "Installed aptupdate from gist."
94else
95 warn "Could not fetch aptupdate; installing fallback script."
96 cat >"$tmpfile" <<'EOF'
97#!/bin/sh
98sudo apt update
99sudo DEBIAN_FRONTEND=noninteractive apt -y upgrade
100sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
101sudo apt -y autoremove
102sudo apt -y autoclean
103sudo apt -y clean
104EOF
105 sudo mv "$tmpfile" "$APTUPDATE_PATH"
106fi
107sudo chmod +x "$APTUPDATE_PATH"
108
109# =========================
110# 2) Install common packages
111# =========================
112sudo apt-get update -y
113sudo DEBIAN_FRONTEND=noninteractive apt-get install -y "${PACKAGES[@]}" || warn "Some packages failed to install; continuing."
114
115# Map Ubuntu's fdfind → fd for consistency
116if command -v fdfind >/dev/null 2>&1 && ! command -v fd >/dev/null 2>&1; then
117 ensure_local_bin
118 ln -sf "$(command -v fdfind)" "$HOME/.local/bin/fd"
119 msg "Linked fdfind → fd at ~/.local/bin/fd"
120fi
121
122# =========================
123# 3) Shell customization
124# =========================
125BASHRC="${HOME}/.bashrc"
126INPUTRC="${HOME}/.inputrc"
127[ -f "$BASHRC" ] || touch "$BASHRC"
128[ -f "$INPUTRC" ] || touch "$INPUTRC"
129backup_file "$BASHRC"
130backup_file "$INPUTRC"
131
132# 3a) force_color_prompt=yes (ensure near top for Ubuntu-style .bashrc)
133if grep -Eq '^[[:space:]]*#?[[:space:]]*force_color_prompt[[:space:]]*=' "$BASHRC"; then
134 sed -i '0,/^[[:space:]]*#\{0,1\}[[:space:]]*force_color_prompt[[:space:]]*=.*/s//force_color_prompt=yes/' "$BASHRC"
135else
136 sed -i '1iforce_color_prompt=yes' "$BASHRC"
137fi
138msg "Enabled force_color_prompt=yes."
139
140# 3b) bash-completion
141COMPLETION_START="# >>> chatgpt: bash-completion enable >>>"
142COMPLETION_END="# <<< chatgpt: bash-completion enable <<<"
143COMPLETION_BLOCK=$(cat <<'EOF'
144# >>> chatgpt: bash-completion enable >>>
145if [ -n "$BASH_VERSION" ] && ! shopt -oq posix; then
146 if [ -f /usr/share/bash-completion/bash_completion ]; then
147 . /usr/share/bash-completion/bash_completion
148 elif [ -f /etc/bash_completion ]; then
149 . /etc/bash_completion
150 fi
151fi
152# <<< chatgpt: bash-completion enable <<<
153EOF
154)
155upsert_block "$BASHRC" "$COMPLETION_START" "$COMPLETION_END" "$COMPLETION_BLOCK"
156msg "Ensured bash-completion is sourced when available."
157
158# 3c) History: timestamps + safe/merged history
159HIST_START="# >>> chatgpt: history settings >>>"
160HIST_END="# <<< chatgpt: history settings <<<"
161HIST_BLOCK=$(cat <<'EOF'
162# >>> chatgpt: history settings >>>
163# Timestamps (YYYY-MM-DD HH:MM:SS)
164export HISTTIMEFORMAT='%F %T '
165# Safer history
166export HISTCONTROL=ignoredups:ignorespace:erasedups
167export HISTSIZE=200000
168export HISTFILESIZE=400000
169# Merge history across concurrent shells
170shopt -s histappend
171if [[ -n "${PROMPT_COMMAND:-}" ]]; then
172 case "$PROMPT_COMMAND" in
173 *'history -a; history -c; history -r'*) : ;;
174 *) PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND" ;;
175 esac
176else
177 PROMPT_COMMAND='history -a; history -c; history -r'
178fi
179# <<< chatgpt: history settings <<<
180EOF
181)
182upsert_block "$BASHRC" "$HIST_START" "$HIST_END" "$HIST_BLOCK"
183msg "Enabled history timestamps and merging."
184
185# 3d) QoL: prompt, aliases, shopt, editor/pager (Ubuntu-like prompt colors)
186QOL_START="# >>> chatgpt: QoL shell block >>>"
187QOL_END="# <<< chatgpt: QoL shell block <<<"
188QOL_BLOCK=$(cat <<'EOF'
189# >>> chatgpt: QoL shell block >>>
190# Minimal Git branch helper
191__chatgpt_git_branch() {
192 local b
193 b=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || return 0
194 [ "$b" = "HEAD" ] && b=$(git rev-parse --short HEAD 2>/dev/null)
195 [ -n "$b" ] && printf ' (%s)' "$b"
196}
197
198# Prompt (Ubuntu-like): user@host (bright green) : cwd (bright blue) + (git)
199# Also shows exit code when non-zero.
200__chatgpt_set_ps1() {
201 local exit="$?"
202 local red="\[\e[1;31m\]"
203 local green="\[\e[1;32m\]"
204 local blue="\[\e[1;34m\]"
205 local reset="\[\e[0m\]"
206
207 local code=""
208 [ "$exit" -ne 0 ] && code="${red}↯${exit}${reset} "
209
210 PS1="${code}${green}\u@\h${reset}:${blue}\w${reset}\$(__chatgpt_git_branch)\$ "
211}
212case "${PROMPT_COMMAND:-}" in
213 *"__chatgpt_set_ps1"*) : ;;
214 "") PROMPT_COMMAND="__chatgpt_set_ps1" ;;
215 *) PROMPT_COMMAND="__chatgpt_set_ps1; $PROMPT_COMMAND" ;;
216esac
217
218# shopt toggles
219shopt -s checkwinsize cdspell autocd globstar
220
221# Common aliases
222alias ls='ls --color=auto -F --group-directories-first'
223alias ll='ls -lah'
224alias la='ls -A'
225alias grep='grep --color=auto'
226alias df='df -h'
227alias free='free -h'
228alias watchd='watch -n1 -d'
229alias cp='cp -i'
230alias mv='mv -i'
231alias rm='rm -I' # prompt once if >3 files
232
233# Prefer modern tools if present
234command -v batcat >/dev/null 2>&1 && alias bat='batcat'
235command -v bat >/dev/null 2>&1 && alias cat='bat --paging=never'
236command -v lsd >/dev/null 2>&1 && alias ls='lsd'
237
238# Defaults
239export EDITOR="${EDITOR:-nano}"
240export VISUAL="$EDITOR"
241export PAGER="${PAGER:-less}"
242export LESS='-R'
243export GPG_TTY="$(tty 2>/dev/null || true)"
244# <<< chatgpt: QoL shell block <<<
245EOF
246)
247upsert_block "$BASHRC" "$QOL_START" "$QOL_END" "$QOL_BLOCK"
248msg "Added/updated prompt/aliases/shopt/editor QoL block."
249
250# 3e) Ensure desired EDITOR (force to your preference)
251if grep -Fq 'export EDITOR=' "$BASHRC"; then
252 sed -i "0,/export EDITOR=.*/s//export EDITOR=\"${PREFER_EDITOR}\"/" "$BASHRC"
253else
254 echo "export EDITOR=\"${PREFER_EDITOR}\"" >> "$BASHRC"
255fi
256msg "Default EDITOR set to ${PREFER_EDITOR}"
257
258# 3f) ~/.inputrc readline tweaks
259INPUT_START="# >>> chatgpt: readline tweaks >>>"
260INPUT_END="# <<< chatgpt: readline tweaks <<<"
261INPUT_BLOCK=$(cat <<'EOF'
262# >>> chatgpt: readline tweaks >>>
263set show-all-if-ambiguous on
264set menu-complete-display-prefix on
265set colored-stats on
266set mark-symlinked-directories on
267set completion-ignore-case on
268set completion-map-case on
269"\e[A": history-search-backward
270"\e[B": history-search-forward
271# <<< chatgpt: readline tweaks <<<
272EOF
273)
274upsert_block "$INPUTRC" "$INPUT_START" "$INPUT_END" "$INPUT_BLOCK"
275msg "Improved interactive completion in ~/.inputrc."
276
277# =========================
278# 4) Wrap up
279# =========================
280msg "First-run setup complete. Open a NEW terminal or run: source ~/.bashrc"
281
getpubkey.sh Неформатований
1#!/bin/bash
2
3# Prompt the user for their GitHub username
4read -p "Enter your GitHub username: " USERNAME
5
6# Get the GitHub public key URL
7KEY_URL="https://github.com/$USERNAME.keys"
8
9pushd ~/.ssh/
10
11# Download the public key
12wget -q -O - "$KEY_URL" > ~/.ssh/authorized_keys
13
14# Set appropriate permissions for the authorized_keys file
15chmod 600 ~/.ssh/authorized_keys
16
17# Check if the .ssh directory exists and create it if it doesn't
18if [ ! -d ~/.ssh ]; then
19 mkdir -p ~/.ssh
20 chmod 700 ~/.ssh
21fi
22
23echo "Public key successfully added to authorized_keys"