Mostrando postagens com marcador Bash. Mostrar todas as postagens
Mostrando postagens com marcador Bash. Mostrar todas as postagens

Fixing -bash: /dev/null: Permission denied

 This should fix the issue (as root):

rm /dev/null
mknod /dev/null c 1 3
chmod 666 /dev/null


What these commands are doing:

rm is removing the bogus file that has been created because the expected one was missing;
mknod is creating a character device named /dev/null with the appropriate major and minor numbers for a Linux kernel;
chmod is setting the permissions for all users to be able to read and write to /dev/null.

Harden bash_history and bash configuration files

 

Harden bash_history and bash configuration files by making them append-only:

chattr +a /home/user/.bash_history
chattr +a /home/user/.bash_profile
chattr +a /home/user/.bash_login
chattr +a /home/user/.profile
chattr +a /home/user/.bash_logout
chattr +a /home/user/.bashrc

Harden env variables by adding the following lines to /home/user/.bashrc:

shopt -s histappend
readonly PROMPT_COMMAND="history -a"
readonly HISTFILE
readonly HISTFILESIZE
readonly HISTSIZE
readonly HISTCMD
readonly HISTCONTROL
readonly HISTIGNORE

histappend tells bash to append the last $HISTSIZE lines to the $HISTFILE file when an interactive shell exits. PROMPT_COMMAND executes the given command prior to issuing each prompt. history -a appends the command typed just before the current one to $HISTFILE.

Disable access to other shells:

chmod 750 csh
chmod 750 tcsh
chmod 750 ksh

 

 

Run local script on remote host via SSH

 

ssh root@<remoteServer> "bash -s" < /opt/script.sh
Run in multiple hosts: 
for i in `cat hosts.txt` ; do echo $i && ssh root@$i "bash -s" < script.sh ; done 

Source: https://unix.stackexchange.com/questions/87405/how-can-i-execute-local-script-on-remote-machine-and-include-arguments

Find files that ends with tilde

Find files:

find . -type f -name '*~'

To delete the files:

find . -type f -name '*~' -exec rm -f '{}' \;

Shell get lines interval from a text file

 

cat file | head -n 16482 | tail -n 258

using sed:

sed -n '16224,16482p' in.sql > out.sql

using awk:

awk 'NR>=16224&&NR<=16482' in.sql > out.sql

Mysql shell script without password exposure

Suppose your username is "db_user". Running from the shell prompt:

mysql_config_editor set --login-path=local --host=localhost --user=db_user --password

It prompts for the password. Once you enter it, the user/pass are saved encrypted in your home/system_username/.mylogin.cnf

write the password between "" if contains spacial character, like "#" or "." ex: "Passw#rd"

Of course, change "system_username" to your username on the server.

Change your bash script from this:

mysqldump -u db_user -pInsecurePassword my_database | gzip > db_backup.tar.gz

to this:

mysqldump --login-path=local my_database | gzip > db_backup.tar.gz


mysql --login-path=local -D DATABASE -e "SELECT * FROM table LIMIT 10"

Bash remote script execution


bash <(curl -s https://github.com/dominicpratt/install-zabbix-agent/blob/master/install-zabbix-agent.sh)

Shell History with date


Include in /root/.bashrc

# History with date
HISTTIMEFORMAT="%F-%T "
 

 or

HISTTIMEFORMAT="%d/%m/%y %T "

 
Optional: Clear history
history -c 

Logoff and login again.




Get first and last line of file


bash way
(head -1; tail -1) < /etc/passwd

sed way
sed -n '1p; $p' /etc/passwd

awk way
awk 'NR==1; END { print }' /etc/passwd

Test if param is not empty

test if param is not empty bash

Test if $1 has value
[ -n "$1" ] && echo $1 || echo param is empty

Creating columns bash


create 1 column
seq 10

create 2 columns
printf %s\\n ID-{1..3}' '{10..25..5}

create 3 or more columns
seq 12 | pr -3t | column -t

Remove computer from Azure Arc

   To fully remove a computer from Azure Arc-enabled servers, follow the official steps in the Uninstall the Azure Connected Machin...

Mais vistos