Última atividade 1760251296

Revisão eeca36c21910da7fbe909e8d69a0d0d811c5e58e

aptupdate Bruto
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 Bruto
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 Bruto
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 Bruto
1#!/usr/bin/env bash
2set -euo pipefail
3
4# =========================
5# Settings (adjust freely)
6# =========================
7PREFER_EDITOR="${PREFER_EDITOR:-vim}"
8PACKAGES=(
9 bash-completion git curl wget ca-certificates gnupg
10 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
32append_once() {
33 # $1=file, $2=tag_line(unique), $3=payload
34 local file="$1" tag="$2" payload="$3"
35 grep -Fqx "$tag" "$file" 2>/dev/null && return 0
36 printf "\n%s\n%s\n" "$tag" "$payload" >>"$file"
37}
38
39ensure_local_bin() {
40 mkdir -p "$HOME/.local/bin"
41 case ":${PATH:-}:" in
42 *":$HOME/.local/bin:"*) : ;;
43 *) echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$HOME/.profile" ;;
44 esac
45}
46
47# =========================
48# 0) System update/upgrade
49# =========================
50cd "$HOME"
51
52export DEBIAN_FRONTEND=noninteractive
53sudo apt-get -y update
54sudo apt-get -y upgrade || true
55
56# Keep your style (and keep unattended-upgrades removed)
57sudo apt -y update
58sudo apt -y upgrade || true
59sudo apt -y autoremove
60sudo apt -y autoclean
61sudo apt -y clean
62sudo apt remove -y unattended-upgrades || true
63msg "Base update/upgrade complete (unattended-upgrades removed)."
64
65# =========================
66# 1) Install /usr/bin/aptupdate
67# (download; if fails, write a built-in fallback)
68# =========================
69tmpfile="$(mktemp)"
70if command -v curl >/dev/null 2>&1 && curl -fsSL "$APTUPDATE_URL" -o "$tmpfile"; then
71 sudo mv "$tmpfile" "$APTUPDATE_PATH"
72 msg "Installed aptupdate from gist."
73else
74 warn "Could not fetch aptupdate; installing fallback script."
75 cat >"$tmpfile" <<'EOF'
76#!/bin/sh
77sudo apt update
78sudo DEBIAN_FRONTEND=noninteractive apt -y upgrade
79sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" dist-upgrade
80sudo apt -y autoremove
81sudo apt -y autoclean
82sudo apt -y clean
83EOF
84 sudo mv "$tmpfile" "$APTUPDATE_PATH"
85fi
86sudo chmod +x "$APTUPDATE_PATH"
87
88# =========================
89# 2) Install common packages
90# =========================
91sudo apt-get update -y
92sudo DEBIAN_FRONTEND=noninteractive apt-get install -y "${PACKAGES[@]}" || warn "Some packages failed to install; continuing."
93
94# Map Ubuntu's fdfind → fd for consistency
95if command -v fdfind >/dev/null 2>&1 && ! command -v fd >/dev/null 2>&1; then
96 ensure_local_bin
97 ln -sf "$(command -v fdfind)" "$HOME/.local/bin/fd"
98 msg "Linked fdfind → fd at ~/.local/bin/fd"
99fi
100
101# =========================
102# 3) Shell customization
103# =========================
104BASHRC="${HOME}/.bashrc"
105INPUTRC="${HOME}/.inputrc"
106[ -f "$BASHRC" ] || touch "$BASHRC"
107[ -f "$INPUTRC" ] || touch "$INPUTRC"
108backup_file "$BASHRC"
109backup_file "$INPUTRC"
110
111# 3a) force_color_prompt=yes (ensure near top for Ubuntu-style .bashrc)
112if grep -Eq '^[[:space:]]*#?[[:space:]]*force_color_prompt[[:space:]]*=' "$BASHRC"; then
113 sed -i '0,/^[[:space:]]*#\{0,1\}[[:space:]]*force_color_prompt[[:space:]]*=.*/s//force_color_prompt=yes/' "$BASHRC"
114else
115 sed -i '1iforce_color_prompt=yes' "$BASHRC"
116fi
117msg "Enabled force_color_prompt=yes."
118
119# 3b) bash-completion
120COMPLETION_TAG="# >>> chatgpt: bash-completion enable >>>"
121COMPLETION_BLOCK=$(cat <<'EOF'
122# >>> chatgpt: bash-completion enable >>>
123if [ -n "$BASH_VERSION" ] && ! shopt -oq posix; then
124 if [ -f /usr/share/bash-completion/bash_completion ]; then
125 . /usr/share/bash-completion/bash_completion
126 elif [ -f /etc/bash_completion ]; then
127 . /etc/bash_completion
128 fi
129fi
130# <<< chatgpt: bash-completion enable <<<
131EOF
132)
133append_once "$BASHRC" "$COMPLETION_TAG" "$COMPLETION_BLOCK"
134msg "Ensured bash-completion is sourced when available."
135
136# 3c) History: timestamps + safe/merged history
137HIST_TAG="# >>> chatgpt: history settings >>>"
138HIST_BLOCK=$(cat <<'EOF'
139# >>> chatgpt: history settings >>>
140# Timestamps (YYYY-MM-DD HH:MM:SS)
141export HISTTIMEFORMAT='%F %T '
142# Safer history
143export HISTCONTROL=ignoredups:ignorespace:erasedups
144export HISTSIZE=200000
145export HISTFILESIZE=400000
146# Merge history across concurrent shells
147shopt -s histappend
148if [[ -n "${PROMPT_COMMAND:-}" ]]; then
149 case "$PROMPT_COMMAND" in
150 *'history -a; history -c; history -r'*) : ;;
151 *) PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND" ;;
152 esac
153else
154 PROMPT_COMMAND='history -a; history -c; history -r'
155fi
156# <<< chatgpt: history settings <<<
157EOF
158)
159append_once "$BASHRC" "$HIST_TAG" "$HIST_BLOCK"
160msg "Enabled history timestamps and merging."
161
162# 3d) QoL: prompt, aliases, shopt, editor/pager (no Kubernetes)
163QOL_TAG="# >>> chatgpt: QoL shell block >>>"
164QOL_BLOCK=$(cat <<'EOF'
165# >>> chatgpt: QoL shell block >>>
166# Minimal Git branch helper
167__chatgpt_git_branch() {
168 local b
169 b=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || return 0
170 [ "$b" = "HEAD" ] && b=$(git rev-parse --short HEAD 2>/dev/null)
171 [ -n "$b" ] && printf ' (%s)' "$b"
172}
173
174# Prompt: exit code + user@host:cwd + (git)
175__chatgpt_set_ps1() {
176 local exit="$?"
177 local red="\[\e[31m\]" green="\[\e[32m\]" yellow="\[\e[33m\]" blue="\[\e[34m\]" magenta="\[\e[35m\]" cyan="\[\e[36m\]" reset="\[\e[0m\]"
178 local code=""
179 [ "$exit" -ne 0 ] && code="${red}↯${exit}${reset} "
180 PS1="${code}${green}\u${reset}@${blue}\h${reset}:${magenta}\w${reset}\$(__chatgpt_git_branch)${cyan}# ${reset}"
181}
182case "${PROMPT_COMMAND:-}" in
183 *"__chatgpt_set_ps1"*) : ;;
184 "") PROMPT_COMMAND="__chatgpt_set_ps1" ;;
185 *) PROMPT_COMMAND="__chatgpt_set_ps1; $PROMPT_COMMAND" ;;
186esac
187
188# shopt toggles
189shopt -s checkwinsize cdspell autocd globstar
190
191# Common aliases
192alias ls='ls --color=auto -F --group-directories-first'
193alias ll='ls -lah'
194alias la='ls -A'
195alias grep='grep --color=auto'
196alias df='df -h'
197alias free='free -h'
198alias watchd='watch -n1 -d'
199alias cp='cp -i'
200alias mv='mv -i'
201alias rm='rm -I' # prompt once if >3 files
202
203# Prefer modern tools if present
204command -v batcat >/dev/null 2>&1 && alias bat='batcat'
205command -v bat >/dev/null 2>&1 && alias cat='bat --paging=never'
206command -v lsd >/dev/null 2>&1 && alias ls='lsd'
207
208# Defaults
209export EDITOR="${EDITOR:-vim}"
210export VISUAL="$EDITOR"
211export PAGER="${PAGER:-less}"
212export LESS='-R'
213export GPG_TTY="$(tty 2>/dev/null || true)"
214# <<< chatgpt: QoL shell block <<<
215EOF
216)
217append_once "$BASHRC" "$QOL_TAG" "$QOL_BLOCK"
218msg "Added prompt/aliases/shopt/editor QoL block."
219
220# 3e) Ensure desired EDITOR
221if grep -Fq 'export EDITOR=' "$BASHRC"; then
222 sed -i "0,/export EDITOR=.*/s//export EDITOR=\"${PREFER_EDITOR}\"/" "$BASHRC"
223else
224 echo "export EDITOR=\"${PREFER_EDITOR}\"" >> "$BASHRC"
225fi
226msg "Default EDITOR set to ${PREFER_EDITOR}"
227
228# 3f) ~/.inputrc readline tweaks
229INPUT_TAG="# >>> chatgpt: readline tweaks >>>"
230INPUT_BLOCK=$(cat <<'EOF'
231# >>> chatgpt: readline tweaks >>>
232set show-all-if-ambiguous on
233set menu-complete-display-prefix on
234set colored-stats on
235set mark-symlinked-directories on
236set completion-ignore-case on
237set completion-map-case on
238"\e[A": history-search-backward
239"\e[B": history-search-forward
240# <<< chatgpt: readline tweaks <<<
241EOF
242)
243append_once "$INPUTRC" "$INPUT_TAG" "$INPUT_BLOCK"
244msg "Improved interactive completion in ~/.inputrc."
245
246# =========================
247# 4) Wrap up
248# =========================
249msg "First-run setup complete. Open a NEW terminal or run: source ~/.bashrc"
250
getpubkey.sh Bruto
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"