Skip to main content

45 posts tagged with "linux"

View All Tags

· One min read
Hreniuc Cristian-Alexandru

Remove unused pacakges:

pacman -Rsn $(pacman -Qdtq)

List all unused packages:

pacman -Qdtq

· One min read
Hreniuc Cristian-Alexandru

This is how you can download an archive using curl:

curl -L -O https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz
  • -L - Follow redirects
  • -O - Write output to a file named as the remote file

· One min read
Hreniuc Cristian-Alexandru

Copy files from a different server and preserv file owners, permissions.

rsync -avprog user@dns:/path/scripts destination
  • a - Archive, don't know what it does
  • v - Verbose
  • p - Preserve file permissions
  • r - Reccursive
  • o - Preserve file owners Needs root
  • g - Preserve file group Needs root

· One min read
Hreniuc Cristian-Alexandru

On the server side:

tcpdump -n -i interface -n "src host client-host.com and dst port server-port"

You can get the client host using lsof:

lsof -itcp -a -p server_pid

This will print all active tcp connections to the server app with the specific pid.

On the client side:

tcpdump -n -i interface -n "dst host server-host.com and dst port server-port"

interface can be taken via ip addr

· One min read
Hreniuc Cristian-Alexandru

Let's say we have this diagram of servers:

A:9104 -> B -> C:9104

Connect from server A to server C via server B:

# We are here on server A and we run the following command:
ssh -N -f -L \*:9104:C:9104 user@B
# -N do not run any command on remote server
# -f run in background
# -L local forward
# \* - the 9014 port will be publically visible from outside of server A

Now to access C:9104, you will be able to do it from A like this A:9104.

Why would you want to do this? When you can't access directly the server C from server A, but you can access it from server B. And from server A you can access B. Voila!

· One min read
Hreniuc Cristian-Alexandru

Print lines that matches a regex starting from a line that matches another regex until an until a line that matches a different regex:

cat file | awk '/startling_regex_excluded/{next} /printable_regex/{print} /until_regex/{exit}'

Having this file:

text
text2
startling_regex_excluded
printable_regex
printable_regex
printable_regex
until_regex
test
printable_regex
printable_regex

The output will be:

printable_regex
printable_regex
printable_regex

AWK CheatSheet

Get all lines starting from a position:

awk '/start line/{flag=1;next}flag' file.log

· 3 min read
Hreniuc Cristian-Alexandru

Debian host - ubuntu docker:

# Allow x11 forwarding
xhosts +

docker run --rm=true --name="container_name" -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix -it image_name /bin/bash
# This is the most important part from the command:
# -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix

Run GUI apps on docker without xserver installed on host

We have three layers: A -> My desktop PC**B -> Host server(runs docker) C -> docker container that runs on BHow to connect to C and run a GUI app from A? Without having installed xserver on B** (no -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix options given to the docker run command). I did it this way:

  1. Create a docker image with ssh running on it: Here
  FROM ubuntu:16.04

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE in users profile
RUN echo export VISIBLE=now >> /etc/profile

EXPOSE 22
CMD [/usr/sbin/sshd, -D]
  1. Build the image: docker build -t ubuntu_sshd .

  2. Run the container: docker run -d -P --name test_sshd ubuntu_sshd

  3. Get port of the docker container: docker port test_sshd 22

  4. Now connect to it using ssh:

    ssh root@ip -p port
    # The password is ``screencast``.
    root@f38c87f2a42d:/#
  5. Enable X11Forwarding : Add X11Forwarding yes to /etc/ssh/sshd_config

  6. Disable X11UseLocalhost : Add X11UseLocalhost no to /etc/ssh/sshd_config

  7. Reload ssh config: /etc/init.d/ssh reload

  8. Exit from the ssh session

  9. All those commands were made from the B machine(from 5 - 8 were run on the docker container)

  10. Now go to your desktop machine A and connect via ssh on the docker machine(also enable x forwarding on your destktop PC), using the ip of B and the port you got from the command at step 4:

  # Allow xserver forward(allow clients to connect to your x server)
$ xhost +
# Connect via ssh and forward the xserver `-X`
$ ssh -X root@B_host_ip -p docker_port
# The password is ``screencast``.
root@f38c87f2a42d:/# apt-get install x11-apps -y
root@f38c87f2a42d:/# xeyes
# Magic happens!!!

Debian:

# Debian with sshd
FROM debian
RUN apt-get update

RUN apt-get install -y openssh-server

RUN echo 'root:password' | chpasswd

RUN mkdir -p /var/run/sshd && sed -i s/UsePrivilegeSeparation.*//g /etc/ssh/sshd_config \
&& sed -i 's/PermitRootLogin without-password//' /etc/ssh/sshd_config \
&& touch /root/.Xauthority \
&& true

RUN sed -i 's/PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config

RUN echo X11UseLocalhost no >> /etc/ssh/sshd_config
RUN echo PermitRootLogin yes >> /etc/ssh/sshd_config
RUN echo UsePrivilegeSeparation no >> /etc/ssh/sshd_config

EXPOSE 22

CMD [/usr/sbin/sshd, -D]

· 4 min read
Hreniuc Cristian-Alexandru

Resources: - Getting started with tmux - Youtube turorial 1 - Youtube turorial 2 Install tmux on Debian

sudo apt-get install tmux

You can also install tmuxinator, which is easier to use, you can create session easily. Tmux has a lot of keybindings, those keybindings are in this format CTRL + B [command]. The CTRL + B is called the prefix, it like letting tmux know that the next key pressed is a command for it. You press CTRL + B release and then press a letter(command). Each tmux session has a number of windows, and each window has a number of panes. Create a new tmux session:

tmux new -s name

Commands for windows: CTRL + B [command]- c - Creates a new window.

  • ,(comma) - Gives you the possibility to rename the window.
  • p - Go to the previous window.
  • n - Go to the next window.
  • w - List all windows(scrollable list of windows).
  • & - Kill current window.
  • f - Search for a window by name.

Commands for panes: CTRL + B [command]- % - Split vertically the current pane in two panes.

  • **** - Split horizontally the current pane in two panes.
  • o - Go to the next pane.
  • { - Move pane to left with one position.
  • } - Move pane to right with one position.
  • q - Show pane numbers + if you press a number, the pane with that number will become active(will have the cursor)
  • x - Kill current pane.
  • z - Zoom in the current pane.(It will enlarge it to the size of the whole terminal, to go back do the combination of keys again)

Move between panes : CTRL + B left-arrow/right-arrow/up-arrow/down-arrowResize current pane: CTRL + B left-arrow/right-arrow/up-arrow/down-arrow(but do not release CTRL) Set a predefined layout: CTRL + B Spacebar**Sessions:- CTRL + B** D - Detach from the session.

List all sessions:

tmux list-sessions
# or
tmux ls

Attach to a session:

tmux attach -t name_of_session

Running commands that do not have keybindings: CTRL + B :(two dots) [text-command]:- split-window - Splits the window horizontally

  • setw synchronize-panes on/off - Write the same thing on all panes.
  • lock-client - follow the leader All clients connected to tmux session will be on the same page, when one goes to another page, all go there.
  • lock-client off - Disable follow the leader

To scroll through output history: > Ctrl-b then [ then you can use your normal navigation keys to scroll around (eg. Up Arrow or PgDn). Press q to quit scroll mode. Alternatively you can press Ctrl-b PgUp to go directly into copy mode and scroll one page up (which is what it sounds like you will want most of the time)

More keybindings can be found here: http://tmuxcheatsheet.com/Tips:- To navigate through command history, use the mouse scroll

Change the config of tmux:- Create a config file in the home directory:

touch ~/.tmux.conf
  • Add a keybinding to reload configuration while running tmux:
# reload config file (change file location to your the tmux.conf file)                                                         
bind-key r source-file ~/.tmux.conf\; display-message Config reloaded.
  • Add everything you want there, layout design, keybindings, etc.

Content of .tmux.conf:

# Create tmux work-session or attach to it if it already exist

######################### Script tmux_start.sh begin ###########################
#!/bin/bash
# new-window command or use alias neww

# send-keys 'cd ~/data/project' C-m \; \
# Sends the command from '' and C-m will execute it.

docker_container_running=$(docker ps | grep container_name_2 | wc -l)
SESSIONNAME="work_session"
tmux has-session -t $SESSIONNAME &> /dev/null
if [ $? -ne 0 ]
then
tmux new-session -s $SESSIONNAME\; \
send-keys 'cd ~/data/project' C-m \; \
rename-window "project" \; \
new-window -c ~/data/project1 -n 'project1' \; \
new-window -c ~/data/project2 -n "project2" \; \
new-window -c ~/data/project3 -n 'project3' \; \
new-window -c ~ -n 'home' \; \
new-window -n 'qserver_qtcreator' 'xhost +; if [ $(docker ps | grep container_name | wc -l) -eq 0 ]; then docker start -i container_name; else docker exec -it container_name /bin/bash; fi' \; \
split-window -h -c ~/data/project/source_1 \; \
split-window -v -c ~/data/project/source_1 'xhost +; if [ $(docker ps | grep container_name | wc -l) -eq 0 ]; then docker start container_name; fi; docker exec -it -e QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb container_name qtcreator' \; \
select-window -t "project_qtcreator"
else
tmux a -t $SESSIONNAME
fi
################################### End Script #################################

# Add alias command work-session in ~/.bashrc
alias work-session='bash ~/data/tmux_start.sh'

# Reload configuration
source ~/.bashrc