{"id":99,"date":"2019-10-15T17:03:01","date_gmt":"2019-10-15T15:03:01","guid":{"rendered":"http:\/\/blog.remyblom.nl\/?p=99"},"modified":"2026-03-11T15:54:49","modified_gmt":"2026-03-11T14:54:49","slug":"random-parts-of-command-line-foo-that-make-your-life-really-easy-but-i-have-to-google-everytime-because-i-am-still-a-nix-n00b","status":"publish","type":"post","link":"https:\/\/blog.remyblom.nl\/?p=99","title":{"rendered":"Random parts of command line foo that make your life really easy but I have to google everytime because I am still a *nix n00b"},"content":{"rendered":"\n<p>Although I worked with Ubuntu for quite some years I still have to google some of the simpliest task, just because I hardly ever need them. The most simple examples being how to add a user to a group, or see how much space I have left on my disks&#8230;<\/p>\n\n\n\n<p>Instead of googling everything again and again, I am going to collect them in this post:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Users<\/h2>\n\n\n\n<p>Add a user to a group:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo usermod -aG sudo remy<\/code><\/pre>\n\n\n\n<p>To copy a user&#8217;s credentials from one machine to the other, on source machine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo getent shadow remy\n$ sudo cat \/home\/remy\/.ssh\/authorized_keys<\/code><\/pre>\n\n\n\n<p>On the target machine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo usermod -p 'HASH' remy\n$ sudo install -d -m 700 -o remy -g remy \/home\/remy\/.ssh\n$ echo 'PUBKEY' | tee -a \/home\/remy\/.ssh\/authorized_keys >\/dev\/null\n$ chown remy:remy \/home\/remy\/.ssh\/authorized_keys\n$ chmod 600 \/home\/remy\/.ssh\/authorized_keys<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Diskspace<\/h2>\n\n\n\n<p>See how much space the subdirectories of your <code>pwd<\/code> are taking up:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ du -sk *<\/code><\/pre>\n\n\n\n<p>Or when you want to look one level deeper, which comes in handy when I want to see which domains and subdomains I have in my <code>\/var\/www\/vhosts<\/code> directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ du -kd 1 *<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Prompt<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># cool colored version:\ncoolcolor='\\033&#91;33m' # with the 2nd 33 being the colorcode &#91;30-37]\nnormal='\\033&#91;0m'\nexport PS1=\"\\u@\\&#91;$coolcolor\\]\\h\\&#91;$normal\\]:\\w \\&#91;$coolcolor\\]$\\&#91;$normal\\] \"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">OpenSSL<\/h2>\n\n\n\n<p>Generate new private key and certificate signing request:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl req -out server.csr -new -newkey rsa:4096 -nodes -keyout private.key<\/code><\/pre>\n\n\n\n<p>Generate csr for existing private key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl req -out server.csr -key private.key -new<\/code><\/pre>\n\n\n\n<p>Check a certificate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl x509 -noout -text -in certificate.crt<\/code><\/pre>\n\n\n\n<p>Get the fingerprint of a certificate,  possible flags are <code>-sha1<\/code> <code>-sha256<\/code> or <code>-md5<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl x509 -noout -fingerprint -sha1 -in certificate.crt<\/code><\/pre>\n\n\n\n<p>Remember: transporting .csr of .crt can be done in the clear without any fear, they are useless without the corresponding private.key<\/p>\n\n\n\n<p>Generate Diffie-Hellman parameters: (takes a long time)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl dhparam -out dhparam.pem 4096<\/code><\/pre>\n\n\n\n<p>Check an SSL connection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl s_client -connect www.example.com:443<\/code><\/pre>\n\n\n\n<p>Create a pfx or pkcs#12 file that contains both private key and certificate and is password protected:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl pkcs12 -export -out key_and_cert.pfx -inkey privkey.pem -in fullchain.pem<\/code><\/pre>\n\n\n\n<p>To check the content of a pkcs12 file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ openssl pkcs12 -info -in key_and_cert.pfx<\/code><\/pre>\n\n\n\n<p>More <a href=\"https:\/\/www.sslshopper.com\/article-most-common-openssl-commands.html\">useful stuff on openssl-commands<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SSH keys<\/h2>\n\n\n\n<p>Generate Ed25519 key pair:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ssh-keygen -o -a 100 -t ed25519 -f ~\/.ssh\/id_ed25519 -C \"foo@bar.com\"<\/code><\/pre>\n\n\n\n<p>Although I prefer ed25519, you might want old fashion rsa keys:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ssh-keygen -t rsa -b 4096 -f ~\/.ssh\/id_rsa -C \"foo@bar.com\"<\/code><\/pre>\n\n\n\n<p>To add your key(s) to your server, use this nifty little tool:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ssh-copy-id user@host<\/code><\/pre>\n\n\n\n<p>Or when you just want to add one specific key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ssh-copy-id -i ~\/.ssh\/id_rsa user@host<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add one file to the other<\/h2>\n\n\n\n<p>Okay, this is like basic stuff, but getting it wrong can be painful, so to append file1 to file2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat file1 &gt;&gt; file2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Compressing and decompressing files<\/h2>\n\n\n\n<p>Creating a simple zip from a directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ zip -r filename.zip \/path\/to\/directory<\/code><\/pre>\n\n\n\n<p>Unzip it with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ unzip filename.zip<\/code><\/pre>\n\n\n\n<p>Or you want to password protect it?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ zip -e filename.zip \/path\/to\/directory<\/code><\/pre>\n\n\n\n<p>Since the encryption used by zip is weak, use 7z instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ 7z a -p filename.7z \/path\/to\/directory<\/code><\/pre>\n\n\n\n<p>Lots of archive on the net will be <code>.tar.gz<\/code> which you extract with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ tar -xf archive.tar.gz<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Reboot server tonight<\/h2>\n\n\n\n<p>Sometimes I want to reboot a machine but keep the impact from the downtime as low as possible, so I schedule the reboot at 06:00 in the morning, hoping the number of users currently using any of its services is close to 0 and in the case something went wrong, I can fix it when at the office at 09:00 (This does indeed imply that we are okay with the 3 hours downtime)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo at 06:00\nwarning: commands will be executed using \/bin\/sh\nat&gt; \/sbin reboot\n&#91;ctrl-d]<\/code><\/pre>\n\n\n\n<p>You can check the queue of scheduled jobs with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo atq\n1\tTue Jan 14 10:00:00 2020 a root<\/code><\/pre>\n\n\n\n<p>And remove a job with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo atrm 1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Send a test mail <\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mail -s \"TEST!\" remy.blom@hku.nl &lt; \/dev\/null<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Although I worked with Ubuntu for quite some years I still have to google some of the simpliest task, just because I hardly ever need them. The most simple examples being how to add a user to a group, or see how much space I have left on my disks&#8230; Instead of googling everything again &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.remyblom.nl\/?p=99\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Random parts of command line foo that make your life really easy but I have to google everytime because I am still a *nix n00b&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,2],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-snippets","category-vps"],"_links":{"self":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":20,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":392,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/392"}],"wp:attachment":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}